ffs_vfsops.c revision 1.162 1 /* $NetBSD: ffs_vfsops.c,v 1.162 2005/03/04 21:45:29 christos 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.162 2005/03/04 21:45:29 christos Exp $");
36
37 #if defined(_KERNEL_OPT)
38 #include "opt_ffs.h"
39 #include "opt_quota.h"
40 #include "opt_softdep.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/proc.h>
47 #include <sys/kernel.h>
48 #include <sys/vnode.h>
49 #include <sys/socket.h>
50 #include <sys/mount.h>
51 #include <sys/buf.h>
52 #include <sys/device.h>
53 #include <sys/mbuf.h>
54 #include <sys/file.h>
55 #include <sys/disklabel.h>
56 #include <sys/ioctl.h>
57 #include <sys/errno.h>
58 #include <sys/malloc.h>
59 #include <sys/pool.h>
60 #include <sys/lock.h>
61 #include <sys/sysctl.h>
62 #include <sys/conf.h>
63
64 #include <miscfs/specfs/specdev.h>
65
66 #include <ufs/ufs/quota.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ufs/inode.h>
69 #include <ufs/ufs/dir.h>
70 #include <ufs/ufs/ufs_extern.h>
71 #include <ufs/ufs/ufs_bswap.h>
72
73 #include <ufs/ffs/fs.h>
74 #include <ufs/ffs/ffs_extern.h>
75
76 /* how many times ffs_init() was called */
77 int ffs_initcount = 0;
78
79 extern struct lock ufs_hashlock;
80
81 extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
82 extern const struct vnodeopv_desc ffs_specop_opv_desc;
83 extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
84
85 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
86 &ffs_vnodeop_opv_desc,
87 &ffs_specop_opv_desc,
88 &ffs_fifoop_opv_desc,
89 NULL,
90 };
91
92 struct vfsops ffs_vfsops = {
93 MOUNT_FFS,
94 ffs_mount,
95 ufs_start,
96 ffs_unmount,
97 ufs_root,
98 ufs_quotactl,
99 ffs_statvfs,
100 ffs_sync,
101 ffs_vget,
102 ffs_fhtovp,
103 ffs_vptofh,
104 ffs_init,
105 ffs_reinit,
106 ffs_done,
107 NULL,
108 ffs_mountroot,
109 ufs_check_export,
110 ffs_snapshot,
111 vfs_stdextattrctl,
112 ffs_vnodeopv_descs,
113 };
114
115 struct genfs_ops ffs_genfsops = {
116 ffs_gop_size,
117 ufs_gop_alloc,
118 genfs_gop_write,
119 };
120
121 POOL_INIT(ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
122 &pool_allocator_nointr);
123 POOL_INIT(ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0, "dino1pl",
124 &pool_allocator_nointr);
125 POOL_INIT(ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0, "dino2pl",
126 &pool_allocator_nointr);
127
128 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
129 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
130
131 /*
132 * Called by main() when ffs is going to be mounted as root.
133 */
134
135 int
136 ffs_mountroot()
137 {
138 struct fs *fs;
139 struct mount *mp;
140 struct proc *p = curproc; /* XXX */
141 struct ufsmount *ump;
142 int error;
143
144 if (root_device->dv_class != DV_DISK)
145 return (ENODEV);
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 return (error);
156 }
157 simple_lock(&mountlist_slock);
158 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
159 simple_unlock(&mountlist_slock);
160 ump = VFSTOUFS(mp);
161 fs = ump->um_fs;
162 memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
163 (void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
164 (void)ffs_statvfs(mp, &mp->mnt_stat, p);
165 vfs_unbusy(mp);
166 setrootfstime((time_t)fs->fs_time);
167 return (0);
168 }
169
170 /*
171 * VFS Operations.
172 *
173 * mount system call
174 */
175 int
176 ffs_mount(mp, path, data, ndp, p)
177 struct mount *mp;
178 const char *path;
179 void *data;
180 struct nameidata *ndp;
181 struct proc *p;
182 {
183 struct vnode *devvp = NULL;
184 struct ufs_args args;
185 struct ufsmount *ump = NULL;
186 struct fs *fs;
187 int error, flags, update;
188 mode_t accessmode;
189
190 if (mp->mnt_flag & MNT_GETARGS) {
191 ump = VFSTOUFS(mp);
192 if (ump == NULL)
193 return EIO;
194 args.fspec = NULL;
195 vfs_showexport(mp, &args.export, &ump->um_export);
196 return copyout(&args, data, sizeof(args));
197 }
198 error = copyin(data, &args, sizeof (struct ufs_args));
199 if (error)
200 return (error);
201
202 #if !defined(SOFTDEP)
203 mp->mnt_flag &= ~MNT_SOFTDEP;
204 #endif
205
206 update = mp->mnt_flag & MNT_UPDATE;
207
208 /* Check arguments */
209 if (args.fspec != NULL) {
210 /*
211 * Look up the name and verify that it's sane.
212 */
213 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
214 if ((error = namei(ndp)) != 0)
215 return (error);
216 devvp = ndp->ni_vp;
217
218 if (!update) {
219 /*
220 * Be sure this is a valid block device
221 */
222 if (devvp->v_type != VBLK)
223 error = ENOTBLK;
224 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
225 error = ENXIO;
226 } else {
227 /*
228 * Be sure we're still naming the same device
229 * used for our initial mount
230 */
231 ump = VFSTOUFS(mp);
232 if (devvp != ump->um_devvp)
233 error = EINVAL;
234 }
235 } else {
236 if (!update) {
237 /* New mounts must have a filename for the device */
238 return (EINVAL);
239 } else {
240 /* Use the extant mount */
241 ump = VFSTOUFS(mp);
242 devvp = ump->um_devvp;
243 vref(devvp);
244 }
245 }
246
247 /*
248 * If mount by non-root, then verify that user has necessary
249 * permissions on the device.
250 */
251 if (error == 0 && p->p_ucred->cr_uid != 0) {
252 accessmode = VREAD;
253 if (update ?
254 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
255 (mp->mnt_flag & MNT_RDONLY) == 0)
256 accessmode |= VWRITE;
257 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
258 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
259 VOP_UNLOCK(devvp, 0);
260 }
261
262 if (error) {
263 vrele(devvp);
264 return (error);
265 }
266
267 if (!update) {
268 int flags;
269
270 /*
271 * Disallow multiple mounts of the same device.
272 * Disallow mounting of a device that is currently in use
273 * (except for root, which might share swap device for
274 * miniroot).
275 */
276 error = vfs_mountedon(devvp);
277 if (error)
278 goto fail;
279 if (vcount(devvp) > 1 && devvp != rootvp) {
280 error = EBUSY;
281 goto fail;
282 }
283 if (mp->mnt_flag & MNT_RDONLY)
284 flags = FREAD;
285 else
286 flags = FREAD|FWRITE;
287 error = VOP_OPEN(devvp, flags, FSCRED, p);
288 if (error)
289 goto fail;
290 error = ffs_mountfs(devvp, mp, p);
291 if (error) {
292 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
293 (void)VOP_CLOSE(devvp, flags, NOCRED, p);
294 VOP_UNLOCK(devvp, 0);
295 goto fail;
296 }
297
298 ump = VFSTOUFS(mp);
299 fs = ump->um_fs;
300 if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
301 (MNT_SOFTDEP | MNT_ASYNC)) {
302 printf("%s fs uses soft updates, "
303 "ignoring async mode\n",
304 fs->fs_fsmnt);
305 mp->mnt_flag &= ~MNT_ASYNC;
306 }
307 } else {
308 /*
309 * Update the mount.
310 */
311
312 /*
313 * The initial mount got a reference on this
314 * device, so drop the one obtained via
315 * namei(), above.
316 */
317 vrele(devvp);
318
319 ump = VFSTOUFS(mp);
320 fs = ump->um_fs;
321 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
322 /*
323 * Changing from r/w to r/o
324 */
325 vn_start_write(NULL, &mp, V_WAIT);
326 flags = WRITECLOSE;
327 if (mp->mnt_flag & MNT_FORCE)
328 flags |= FORCECLOSE;
329 if (mp->mnt_flag & MNT_SOFTDEP)
330 error = softdep_flushfiles(mp, flags, p);
331 else
332 error = ffs_flushfiles(mp, flags, p);
333 if (fs->fs_pendingblocks != 0 ||
334 fs->fs_pendinginodes != 0) {
335 printf("%s: update error: blocks %" PRId64
336 " files %d\n",
337 fs->fs_fsmnt, fs->fs_pendingblocks,
338 fs->fs_pendinginodes);
339 fs->fs_pendingblocks = 0;
340 fs->fs_pendinginodes = 0;
341 }
342 if (error == 0 &&
343 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
344 fs->fs_clean & FS_WASCLEAN) {
345 if (mp->mnt_flag & MNT_SOFTDEP)
346 fs->fs_flags &= ~FS_DOSOFTDEP;
347 fs->fs_clean = FS_ISCLEAN;
348 (void) ffs_sbupdate(ump, MNT_WAIT);
349 }
350 vn_finished_write(mp, 0);
351 if (error)
352 return (error);
353 fs->fs_ronly = 1;
354 fs->fs_fmod = 0;
355 }
356
357 /*
358 * Flush soft dependencies if disabling it via an update
359 * mount. This may leave some items to be processed,
360 * so don't do this yet XXX.
361 */
362 if ((fs->fs_flags & FS_DOSOFTDEP) &&
363 !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
364 #ifdef notyet
365 vn_start_write(NULL, &mp, V_WAIT);
366 flags = WRITECLOSE;
367 if (mp->mnt_flag & MNT_FORCE)
368 flags |= FORCECLOSE;
369 error = softdep_flushfiles(mp, flags, p);
370 if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
371 fs->fs_flags &= ~FS_DOSOFTDEP;
372 (void) ffs_sbupdate(ump, MNT_WAIT);
373 vn_finished_write(mp);
374 #elif defined(SOFTDEP)
375 mp->mnt_flag |= MNT_SOFTDEP;
376 #endif
377 }
378
379 /*
380 * When upgrading to a softdep mount, we must first flush
381 * all vnodes. (not done yet -- see above)
382 */
383 if (!(fs->fs_flags & FS_DOSOFTDEP) &&
384 (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
385 #ifdef notyet
386 vn_start_write(NULL, &mp, V_WAIT);
387 flags = WRITECLOSE;
388 if (mp->mnt_flag & MNT_FORCE)
389 flags |= FORCECLOSE;
390 error = ffs_flushfiles(mp, flags, p);
391 vn_finished_write(mp);
392 #else
393 mp->mnt_flag &= ~MNT_SOFTDEP;
394 #endif
395 }
396
397 if (mp->mnt_flag & MNT_RELOAD) {
398 error = ffs_reload(mp, p->p_ucred, p);
399 if (error)
400 return (error);
401 }
402
403 if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
404 /*
405 * Changing from read-only to read/write
406 */
407 fs->fs_ronly = 0;
408 fs->fs_clean <<= 1;
409 fs->fs_fmod = 1;
410 if ((fs->fs_flags & FS_DOSOFTDEP)) {
411 error = softdep_mount(devvp, mp, fs,
412 p->p_ucred);
413 if (error)
414 return (error);
415 }
416 if (fs->fs_snapinum[0] != 0)
417 ffs_snapshot_mount(mp);
418 }
419 if (args.fspec == 0) {
420 /*
421 * Process export requests.
422 */
423 return (vfs_export(mp, &ump->um_export, &args.export));
424 }
425 if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
426 (MNT_SOFTDEP | MNT_ASYNC)) {
427 printf("%s fs uses soft updates, ignoring async mode\n",
428 fs->fs_fsmnt);
429 mp->mnt_flag &= ~MNT_ASYNC;
430 }
431 }
432
433 error = set_statvfs_info(path, UIO_USERSPACE, args.fspec,
434 UIO_USERSPACE, mp, p);
435 if (error == 0)
436 (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
437 sizeof(fs->fs_fsmnt));
438 if (mp->mnt_flag & MNT_SOFTDEP)
439 fs->fs_flags |= FS_DOSOFTDEP;
440 else
441 fs->fs_flags &= ~FS_DOSOFTDEP;
442 if (fs->fs_fmod != 0) { /* XXX */
443 fs->fs_fmod = 0;
444 if (fs->fs_clean & FS_WASCLEAN)
445 fs->fs_time = time.tv_sec;
446 else {
447 printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
448 mp->mnt_stat.f_mntfromname, fs->fs_clean);
449 printf("%s: lost blocks %" PRId64 " files %d\n",
450 mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
451 fs->fs_pendinginodes);
452 }
453 (void) ffs_cgupdate(ump, MNT_WAIT);
454 }
455 return (error);
456
457 fail:
458 vrele(devvp);
459 return (error);
460 }
461
462 /*
463 * Reload all incore data for a filesystem (used after running fsck on
464 * the root filesystem and finding things to fix). The filesystem must
465 * be mounted read-only.
466 *
467 * Things to do to update the mount:
468 * 1) invalidate all cached meta-data.
469 * 2) re-read superblock from disk.
470 * 3) re-read summary information from disk.
471 * 4) invalidate all inactive vnodes.
472 * 5) invalidate all cached file data.
473 * 6) re-read inode data for all active vnodes.
474 */
475 int
476 ffs_reload(mp, cred, p)
477 struct mount *mp;
478 struct ucred *cred;
479 struct proc *p;
480 {
481 struct vnode *vp, *nvp, *devvp;
482 struct inode *ip;
483 void *space;
484 struct buf *bp;
485 struct fs *fs, *newfs;
486 struct partinfo dpart;
487 int i, blks, size, error;
488 int32_t *lp;
489 struct ufsmount *ump;
490 daddr_t sblockloc;
491
492 if ((mp->mnt_flag & MNT_RDONLY) == 0)
493 return (EINVAL);
494
495 ump = VFSTOUFS(mp);
496 /*
497 * Step 1: invalidate all cached meta-data.
498 */
499 devvp = ump->um_devvp;
500 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
501 error = vinvalbuf(devvp, 0, cred, p, 0, 0);
502 VOP_UNLOCK(devvp, 0);
503 if (error)
504 panic("ffs_reload: dirty1");
505 /*
506 * Step 2: re-read superblock from disk.
507 */
508 fs = ump->um_fs;
509 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, p) != 0)
510 size = DEV_BSIZE;
511 else
512 size = dpart.disklab->d_secsize;
513 /* XXX we don't handle possibility that superblock moved. */
514 error = bread(devvp, fs->fs_sblockloc / size, fs->fs_sbsize,
515 NOCRED, &bp);
516 if (error) {
517 brelse(bp);
518 return (error);
519 }
520 newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
521 memcpy(newfs, bp->b_data, fs->fs_sbsize);
522 #ifdef FFS_EI
523 if (ump->um_flags & UFS_NEEDSWAP) {
524 ffs_sb_swap((struct fs*)bp->b_data, newfs);
525 fs->fs_flags |= FS_SWAPPED;
526 } else
527 #endif
528 fs->fs_flags &= ~FS_SWAPPED;
529 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
530 newfs->fs_magic != FS_UFS2_MAGIC)||
531 newfs->fs_bsize > MAXBSIZE ||
532 newfs->fs_bsize < sizeof(struct fs)) {
533 brelse(bp);
534 free(newfs, M_UFSMNT);
535 return (EIO); /* XXX needs translation */
536 }
537 /* Store off old fs_sblockloc for fs_oldfscompat_read. */
538 sblockloc = fs->fs_sblockloc;
539 /*
540 * Copy pointer fields back into superblock before copying in XXX
541 * new superblock. These should really be in the ufsmount. XXX
542 * Note that important parameters (eg fs_ncg) are unchanged.
543 */
544 newfs->fs_csp = fs->fs_csp;
545 newfs->fs_maxcluster = fs->fs_maxcluster;
546 newfs->fs_contigdirs = fs->fs_contigdirs;
547 newfs->fs_ronly = fs->fs_ronly;
548 newfs->fs_active = fs->fs_active;
549 memcpy(fs, newfs, (u_int)fs->fs_sbsize);
550 brelse(bp);
551 free(newfs, M_UFSMNT);
552
553 /* Recheck for apple UFS filesystem */
554 ump->um_flags &= ~UFS_ISAPPLEUFS;
555 /* First check to see if this is tagged as an Apple UFS filesystem
556 * in the disklabel
557 */
558 if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) == 0) &&
559 (dpart.part->p_fstype == FS_APPLEUFS)) {
560 ump->um_flags |= UFS_ISAPPLEUFS;
561 }
562 #ifdef APPLE_UFS
563 else {
564 /* Manually look for an apple ufs label, and if a valid one
565 * is found, then treat it like an Apple UFS filesystem anyway
566 */
567 error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
568 APPLEUFS_LABEL_SIZE, cred, &bp);
569 if (error) {
570 brelse(bp);
571 return (error);
572 }
573 error = ffs_appleufs_validate(fs->fs_fsmnt,
574 (struct appleufslabel *)bp->b_data,NULL);
575 if (error == 0)
576 ump->um_flags |= UFS_ISAPPLEUFS;
577 brelse(bp);
578 bp = NULL;
579 }
580 #else
581 if (ump->um_flags & UFS_ISAPPLEUFS)
582 return (EIO);
583 #endif
584
585 if (UFS_MPISAPPLEUFS(ump)) {
586 /* see comment about NeXT below */
587 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
588 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
589 mp->mnt_iflag |= IMNT_DTYPE;
590 } else {
591 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
592 ump->um_dirblksiz = DIRBLKSIZ;
593 if (ump->um_maxsymlinklen > 0)
594 mp->mnt_iflag |= IMNT_DTYPE;
595 else
596 mp->mnt_iflag &= ~IMNT_DTYPE;
597 }
598 ffs_oldfscompat_read(fs, ump, sblockloc);
599 ump->um_maxfilesize = fs->fs_maxfilesize;
600 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
601 fs->fs_pendingblocks = 0;
602 fs->fs_pendinginodes = 0;
603 }
604
605 ffs_statvfs(mp, &mp->mnt_stat, p);
606 /*
607 * Step 3: re-read summary information from disk.
608 */
609 blks = howmany(fs->fs_cssize, fs->fs_fsize);
610 space = fs->fs_csp;
611 for (i = 0; i < blks; i += fs->fs_frag) {
612 size = fs->fs_bsize;
613 if (i + fs->fs_frag > blks)
614 size = (blks - i) * fs->fs_fsize;
615 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
616 NOCRED, &bp);
617 if (error) {
618 brelse(bp);
619 return (error);
620 }
621 #ifdef FFS_EI
622 if (UFS_FSNEEDSWAP(fs))
623 ffs_csum_swap((struct csum *)bp->b_data,
624 (struct csum *)space, size);
625 else
626 #endif
627 memcpy(space, bp->b_data, (size_t)size);
628 space = (char *)space + size;
629 brelse(bp);
630 }
631 if ((fs->fs_flags & FS_DOSOFTDEP))
632 softdep_mount(devvp, mp, fs, cred);
633 if (fs->fs_snapinum[0] != 0)
634 ffs_snapshot_mount(mp);
635 /*
636 * We no longer know anything about clusters per cylinder group.
637 */
638 if (fs->fs_contigsumsize > 0) {
639 lp = fs->fs_maxcluster;
640 for (i = 0; i < fs->fs_ncg; i++)
641 *lp++ = fs->fs_contigsumsize;
642 }
643
644 loop:
645 simple_lock(&mntvnode_slock);
646 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
647 if (vp->v_mount != mp) {
648 simple_unlock(&mntvnode_slock);
649 goto loop;
650 }
651 nvp = vp->v_mntvnodes.le_next;
652 /*
653 * Step 4: invalidate all inactive vnodes.
654 */
655 if (vrecycle(vp, &mntvnode_slock, p))
656 goto loop;
657 /*
658 * Step 5: invalidate all cached file data.
659 */
660 simple_lock(&vp->v_interlock);
661 simple_unlock(&mntvnode_slock);
662 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
663 goto loop;
664 if (vinvalbuf(vp, 0, cred, p, 0, 0))
665 panic("ffs_reload: dirty2");
666 /*
667 * Step 6: re-read inode data for all active vnodes.
668 */
669 ip = VTOI(vp);
670 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
671 (int)fs->fs_bsize, NOCRED, &bp);
672 if (error) {
673 brelse(bp);
674 vput(vp);
675 return (error);
676 }
677 ffs_load_inode(bp, ip, fs, ip->i_number);
678 ip->i_ffs_effnlink = ip->i_nlink;
679 brelse(bp);
680 vput(vp);
681 simple_lock(&mntvnode_slock);
682 }
683 simple_unlock(&mntvnode_slock);
684 return (0);
685 }
686
687 /*
688 * Possible superblock locations ordered from most to least likely.
689 */
690 static const int sblock_try[] = SBLOCKSEARCH;
691
692 /*
693 * Common code for mount and mountroot
694 */
695 int
696 ffs_mountfs(devvp, mp, p)
697 struct vnode *devvp;
698 struct mount *mp;
699 struct proc *p;
700 {
701 struct ufsmount *ump;
702 struct buf *bp;
703 struct fs *fs;
704 dev_t dev;
705 struct partinfo dpart;
706 void *space;
707 daddr_t sblockloc, fsblockloc;
708 int blks, fstype;
709 int error, i, size, ronly;
710 #ifdef FFS_EI
711 int needswap = 0; /* keep gcc happy */
712 #endif
713 int32_t *lp;
714 struct ucred *cred;
715 u_int32_t sbsize = 8192; /* keep gcc happy*/
716
717 dev = devvp->v_rdev;
718 cred = p ? p->p_ucred : NOCRED;
719
720 /* Flush out any old buffers remaining from a previous use. */
721 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
722 error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
723 VOP_UNLOCK(devvp, 0);
724 if (error)
725 return (error);
726
727 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
728 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) != 0)
729 size = DEV_BSIZE;
730 else
731 size = dpart.disklab->d_secsize;
732
733 bp = NULL;
734 ump = NULL;
735 fs = NULL;
736 sblockloc = 0;
737 fstype = 0;
738
739 /*
740 * Try reading the superblock in each of its possible locations. */
741 for (i = 0; ; i++) {
742 if (bp != NULL) {
743 bp->b_flags |= B_NOCACHE;
744 brelse(bp);
745 bp = NULL;
746 }
747 if (sblock_try[i] == -1) {
748 error = EINVAL;
749 fs = NULL;
750 goto out;
751 }
752 error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
753 &bp);
754 if (error)
755 goto out;
756 fs = (struct fs*)bp->b_data;
757 fsblockloc = sblockloc = sblock_try[i];
758 if (fs->fs_magic == FS_UFS1_MAGIC) {
759 sbsize = fs->fs_sbsize;
760 fstype = UFS1;
761 #ifdef FFS_EI
762 needswap = 0;
763 } else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
764 sbsize = bswap32(fs->fs_sbsize);
765 fstype = UFS1;
766 needswap = 1;
767 #endif
768 } else if (fs->fs_magic == FS_UFS2_MAGIC) {
769 sbsize = fs->fs_sbsize;
770 fstype = UFS2;
771 #ifdef FFS_EI
772 needswap = 0;
773 } else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
774 sbsize = bswap32(fs->fs_sbsize);
775 fstype = UFS2;
776 needswap = 1;
777 #endif
778 } else
779 continue;
780
781
782 /* fs->fs_sblockloc isn't defined for old filesystems */
783 if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
784 if (sblockloc == SBLOCK_UFS2)
785 /*
786 * This is likely to be the first alternate
787 * in a filesystem with 64k blocks.
788 * Don't use it.
789 */
790 continue;
791 fsblockloc = sblockloc;
792 } else {
793 fsblockloc = fs->fs_sblockloc;
794 #ifdef FFS_EI
795 if (needswap)
796 fsblockloc = bswap64(fsblockloc);
797 #endif
798 }
799
800 /* Check we haven't found an alternate superblock */
801 if (fsblockloc != sblockloc)
802 continue;
803
804 /* Validate size of superblock */
805 if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
806 continue;
807
808 /* Ok seems to be a good superblock */
809 break;
810 }
811
812 fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
813 memcpy(fs, bp->b_data, sbsize);
814
815 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
816 memset(ump, 0, sizeof *ump);
817 TAILQ_INIT(&ump->um_snapshots);
818 ump->um_fs = fs;
819
820 #ifdef FFS_EI
821 if (needswap) {
822 ffs_sb_swap((struct fs*)bp->b_data, fs);
823 fs->fs_flags |= FS_SWAPPED;
824 } else
825 #endif
826 fs->fs_flags &= ~FS_SWAPPED;
827
828 ffs_oldfscompat_read(fs, ump, sblockloc);
829 ump->um_maxfilesize = fs->fs_maxfilesize;
830
831 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
832 fs->fs_pendingblocks = 0;
833 fs->fs_pendinginodes = 0;
834 }
835
836 ump->um_fstype = fstype;
837 if (fs->fs_sbsize < SBLOCKSIZE)
838 bp->b_flags |= B_INVAL;
839 brelse(bp);
840 bp = NULL;
841
842 /* First check to see if this is tagged as an Apple UFS filesystem
843 * in the disklabel
844 */
845 if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) == 0) &&
846 (dpart.part->p_fstype == FS_APPLEUFS)) {
847 ump->um_flags |= UFS_ISAPPLEUFS;
848 }
849 #ifdef APPLE_UFS
850 else {
851 /* Manually look for an apple ufs label, and if a valid one
852 * is found, then treat it like an Apple UFS filesystem anyway
853 */
854 error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
855 APPLEUFS_LABEL_SIZE, cred, &bp);
856 if (error)
857 goto out;
858 error = ffs_appleufs_validate(fs->fs_fsmnt,
859 (struct appleufslabel *)bp->b_data,NULL);
860 if (error == 0) {
861 ump->um_flags |= UFS_ISAPPLEUFS;
862 }
863 brelse(bp);
864 bp = NULL;
865 }
866 #else
867 if (ump->um_flags & UFS_ISAPPLEUFS) {
868 error = EINVAL;
869 goto out;
870 }
871 #endif
872
873 /*
874 * verify that we can access the last block in the fs
875 * if we're mounting read/write.
876 */
877
878 if (!ronly) {
879 error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
880 cred, &bp);
881 if (bp->b_bcount != fs->fs_fsize)
882 error = EINVAL;
883 bp->b_flags |= B_INVAL;
884 if (error)
885 goto out;
886 brelse(bp);
887 bp = NULL;
888 }
889
890 fs->fs_ronly = ronly;
891 if (ronly == 0) {
892 fs->fs_clean <<= 1;
893 fs->fs_fmod = 1;
894 }
895 size = fs->fs_cssize;
896 blks = howmany(size, fs->fs_fsize);
897 if (fs->fs_contigsumsize > 0)
898 size += fs->fs_ncg * sizeof(int32_t);
899 size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
900 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
901 fs->fs_csp = space;
902 for (i = 0; i < blks; i += fs->fs_frag) {
903 size = fs->fs_bsize;
904 if (i + fs->fs_frag > blks)
905 size = (blks - i) * fs->fs_fsize;
906 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
907 cred, &bp);
908 if (error) {
909 free(fs->fs_csp, M_UFSMNT);
910 goto out;
911 }
912 #ifdef FFS_EI
913 if (needswap)
914 ffs_csum_swap((struct csum *)bp->b_data,
915 (struct csum *)space, size);
916 else
917 #endif
918 memcpy(space, bp->b_data, (u_int)size);
919
920 space = (char *)space + size;
921 brelse(bp);
922 bp = NULL;
923 }
924 if (fs->fs_contigsumsize > 0) {
925 fs->fs_maxcluster = lp = space;
926 for (i = 0; i < fs->fs_ncg; i++)
927 *lp++ = fs->fs_contigsumsize;
928 space = lp;
929 }
930 size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
931 fs->fs_contigdirs = space;
932 space = (char *)space + size;
933 memset(fs->fs_contigdirs, 0, size);
934 /* Compatibility for old filesystems - XXX */
935 if (fs->fs_avgfilesize <= 0)
936 fs->fs_avgfilesize = AVFILESIZ;
937 if (fs->fs_avgfpdir <= 0)
938 fs->fs_avgfpdir = AFPDIR;
939 fs->fs_active = NULL;
940 mp->mnt_data = ump;
941 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
942 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
943 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
944 mp->mnt_stat.f_namemax = MAXNAMLEN;
945 if (UFS_MPISAPPLEUFS(ump)) {
946 /* NeXT used to keep short symlinks in the inode even
947 * when using FS_42INODEFMT. In that case fs->fs_maxsymlinklen
948 * is probably -1, but we still need to be able to identify
949 * short symlinks.
950 */
951 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
952 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
953 mp->mnt_iflag |= IMNT_DTYPE;
954 } else {
955 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
956 ump->um_dirblksiz = DIRBLKSIZ;
957 if (ump->um_maxsymlinklen > 0)
958 mp->mnt_iflag |= IMNT_DTYPE;
959 else
960 mp->mnt_iflag &= ~IMNT_DTYPE;
961 }
962 mp->mnt_fs_bshift = fs->fs_bshift;
963 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
964 mp->mnt_flag |= MNT_LOCAL;
965 #ifdef FFS_EI
966 if (needswap)
967 ump->um_flags |= UFS_NEEDSWAP;
968 #endif
969 ump->um_mountp = mp;
970 ump->um_dev = dev;
971 ump->um_devvp = devvp;
972 ump->um_nindir = fs->fs_nindir;
973 ump->um_lognindir = ffs(fs->fs_nindir) - 1;
974 ump->um_bptrtodb = fs->fs_fsbtodb;
975 ump->um_seqinc = fs->fs_frag;
976 for (i = 0; i < MAXQUOTAS; i++)
977 ump->um_quotas[i] = NULLVP;
978 devvp->v_specmountpoint = mp;
979 if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
980 error = softdep_mount(devvp, mp, fs, cred);
981 if (error) {
982 free(fs->fs_csp, M_UFSMNT);
983 goto out;
984 }
985 }
986 if (ronly == 0 && fs->fs_snapinum[0] != 0)
987 ffs_snapshot_mount(mp);
988 return (0);
989 out:
990 if (fs)
991 free(fs, M_UFSMNT);
992 devvp->v_specmountpoint = NULL;
993 if (bp)
994 brelse(bp);
995 if (ump) {
996 if (ump->um_oldfscompat)
997 free(ump->um_oldfscompat, M_UFSMNT);
998 free(ump, M_UFSMNT);
999 mp->mnt_data = NULL;
1000 }
1001 return (error);
1002 }
1003
1004 /*
1005 * Sanity checks for loading old filesystem superblocks.
1006 * See ffs_oldfscompat_write below for unwound actions.
1007 *
1008 * XXX - Parts get retired eventually.
1009 * Unfortunately new bits get added.
1010 */
1011 static void
1012 ffs_oldfscompat_read(fs, ump, sblockloc)
1013 struct fs *fs;
1014 struct ufsmount *ump;
1015 daddr_t sblockloc;
1016 {
1017 off_t maxfilesize;
1018 int32_t *extrasave;
1019
1020 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1021 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1022 return;
1023
1024 if (!ump->um_oldfscompat)
1025 ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
1026 M_UFSMNT, M_WAITOK);
1027
1028 memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
1029 extrasave = ump->um_oldfscompat;
1030 extrasave += 512/sizeof(int32_t);
1031 extrasave[0] = fs->fs_old_npsect;
1032 extrasave[1] = fs->fs_old_interleave;
1033 extrasave[2] = fs->fs_old_trackskew;
1034
1035 /* These fields will be overwritten by their
1036 * original values in fs_oldfscompat_write, so it is harmless
1037 * to modify them here.
1038 */
1039 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1040 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1041 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1042 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1043
1044 fs->fs_maxbsize = fs->fs_bsize;
1045 fs->fs_time = fs->fs_old_time;
1046 fs->fs_size = fs->fs_old_size;
1047 fs->fs_dsize = fs->fs_old_dsize;
1048 fs->fs_csaddr = fs->fs_old_csaddr;
1049 fs->fs_sblockloc = sblockloc;
1050
1051 fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
1052
1053 if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
1054 fs->fs_old_nrpos = 8;
1055 fs->fs_old_npsect = fs->fs_old_nsect;
1056 fs->fs_old_interleave = 1;
1057 fs->fs_old_trackskew = 0;
1058 }
1059
1060 if (fs->fs_old_inodefmt < FS_44INODEFMT) {
1061 ump->um_maxfilesize = (u_quad_t) 1LL << 39;
1062 fs->fs_qbmask = ~fs->fs_bmask;
1063 fs->fs_qfmask = ~fs->fs_fmask;
1064 }
1065
1066 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1067 if (ump->um_maxfilesize > maxfilesize)
1068 ump->um_maxfilesize = maxfilesize;
1069
1070 /* Compatibility for old filesystems */
1071 if (fs->fs_avgfilesize <= 0)
1072 fs->fs_avgfilesize = AVFILESIZ;
1073 if (fs->fs_avgfpdir <= 0)
1074 fs->fs_avgfpdir = AFPDIR;
1075
1076 #if 0
1077 if (bigcgs) {
1078 fs->fs_save_cgsize = fs->fs_cgsize;
1079 fs->fs_cgsize = fs->fs_bsize;
1080 }
1081 #endif
1082 }
1083
1084 /*
1085 * Unwinding superblock updates for old filesystems.
1086 * See ffs_oldfscompat_read above for details.
1087 *
1088 * XXX - Parts get retired eventually.
1089 * Unfortunately new bits get added.
1090 */
1091 static void
1092 ffs_oldfscompat_write(fs, ump)
1093 struct fs *fs;
1094 struct ufsmount *ump;
1095 {
1096 int32_t *extrasave;
1097
1098 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1099 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1100 return;
1101
1102 fs->fs_old_time = fs->fs_time;
1103 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1104 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1105 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1106 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1107 fs->fs_old_flags = fs->fs_flags;
1108
1109 #if 0
1110 if (bigcgs) {
1111 fs->fs_cgsize = fs->fs_save_cgsize;
1112 }
1113 #endif
1114
1115 memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
1116 extrasave = ump->um_oldfscompat;
1117 extrasave += 512/sizeof(int32_t);
1118 fs->fs_old_npsect = extrasave[0];
1119 fs->fs_old_interleave = extrasave[1];
1120 fs->fs_old_trackskew = extrasave[2];
1121
1122 }
1123
1124 /*
1125 * unmount system call
1126 */
1127 int
1128 ffs_unmount(mp, mntflags, p)
1129 struct mount *mp;
1130 int mntflags;
1131 struct proc *p;
1132 {
1133 struct ufsmount *ump;
1134 struct fs *fs;
1135 int error, flags, penderr;
1136
1137 penderr = 0;
1138 flags = 0;
1139 if (mntflags & MNT_FORCE)
1140 flags |= FORCECLOSE;
1141 if (mp->mnt_flag & MNT_SOFTDEP) {
1142 if ((error = softdep_flushfiles(mp, flags, p)) != 0)
1143 return (error);
1144 } else {
1145 if ((error = ffs_flushfiles(mp, flags, p)) != 0)
1146 return (error);
1147 }
1148 ump = VFSTOUFS(mp);
1149 fs = ump->um_fs;
1150 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1151 printf("%s: unmount pending error: blocks %" PRId64
1152 " files %d\n",
1153 fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
1154 fs->fs_pendingblocks = 0;
1155 fs->fs_pendinginodes = 0;
1156 penderr = 1;
1157 }
1158 if (fs->fs_ronly == 0 &&
1159 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1160 fs->fs_clean & FS_WASCLEAN) {
1161 /*
1162 * XXXX don't mark fs clean in the case of softdep
1163 * pending block errors, until they are fixed.
1164 */
1165 if (penderr == 0) {
1166 if (mp->mnt_flag & MNT_SOFTDEP)
1167 fs->fs_flags &= ~FS_DOSOFTDEP;
1168 fs->fs_clean = FS_ISCLEAN;
1169 }
1170 fs->fs_fmod = 0;
1171 (void) ffs_sbupdate(ump, MNT_WAIT);
1172 }
1173 if (ump->um_devvp->v_type != VBAD)
1174 ump->um_devvp->v_specmountpoint = NULL;
1175 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1176 (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
1177 NOCRED, p);
1178 vput(ump->um_devvp);
1179 free(fs->fs_csp, M_UFSMNT);
1180 free(fs, M_UFSMNT);
1181 if (ump->um_oldfscompat != NULL)
1182 free(ump->um_oldfscompat, M_UFSMNT);
1183 free(ump, M_UFSMNT);
1184 mp->mnt_data = NULL;
1185 mp->mnt_flag &= ~MNT_LOCAL;
1186 return (0);
1187 }
1188
1189 /*
1190 * Flush out all the files in a filesystem.
1191 */
1192 int
1193 ffs_flushfiles(mp, flags, p)
1194 struct mount *mp;
1195 int flags;
1196 struct proc *p;
1197 {
1198 extern int doforce;
1199 struct ufsmount *ump;
1200 int error;
1201
1202 if (!doforce)
1203 flags &= ~FORCECLOSE;
1204 ump = VFSTOUFS(mp);
1205 #ifdef QUOTA
1206 if (mp->mnt_flag & MNT_QUOTA) {
1207 int i;
1208 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
1209 return (error);
1210 for (i = 0; i < MAXQUOTAS; i++) {
1211 if (ump->um_quotas[i] == NULLVP)
1212 continue;
1213 quotaoff(p, mp, i);
1214 }
1215 /*
1216 * Here we fall through to vflush again to ensure
1217 * that we have gotten rid of all the system vnodes.
1218 */
1219 }
1220 #endif
1221 if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
1222 return (error);
1223 ffs_snapshot_unmount(mp);
1224 /*
1225 * Flush all the files.
1226 */
1227 error = vflush(mp, NULLVP, flags);
1228 if (error)
1229 return (error);
1230 /*
1231 * Flush filesystem metadata.
1232 */
1233 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1234 error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
1235 VOP_UNLOCK(ump->um_devvp, 0);
1236 return (error);
1237 }
1238
1239 /*
1240 * Get file system statistics.
1241 */
1242 int
1243 ffs_statvfs(mp, sbp, p)
1244 struct mount *mp;
1245 struct statvfs *sbp;
1246 struct proc *p;
1247 {
1248 struct ufsmount *ump;
1249 struct fs *fs;
1250
1251 ump = VFSTOUFS(mp);
1252 fs = ump->um_fs;
1253 sbp->f_bsize = fs->fs_bsize;
1254 sbp->f_frsize = fs->fs_fsize;
1255 sbp->f_iosize = fs->fs_bsize;
1256 sbp->f_blocks = fs->fs_dsize;
1257 sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1258 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1259 sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
1260 fs->fs_minfree) / (u_int64_t) 100;
1261 if (sbp->f_bfree > sbp->f_bresvd)
1262 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1263 else
1264 sbp->f_bavail = 0;
1265 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1266 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1267 sbp->f_favail = sbp->f_ffree;
1268 sbp->f_fresvd = 0;
1269 copy_statvfs_info(sbp, mp);
1270 return (0);
1271 }
1272
1273 /*
1274 * Go through the disk queues to initiate sandbagged IO;
1275 * go through the inodes to write those that have been modified;
1276 * initiate the writing of the super block if it has been modified.
1277 *
1278 * Note: we are always called with the filesystem marked `MPBUSY'.
1279 */
1280 int
1281 ffs_sync(mp, waitfor, cred, p)
1282 struct mount *mp;
1283 int waitfor;
1284 struct ucred *cred;
1285 struct proc *p;
1286 {
1287 struct vnode *vp, *nvp;
1288 struct inode *ip;
1289 struct ufsmount *ump = VFSTOUFS(mp);
1290 struct fs *fs;
1291 int error, count, allerror = 0;
1292
1293 fs = ump->um_fs;
1294 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1295 printf("fs = %s\n", fs->fs_fsmnt);
1296 panic("update: rofs mod");
1297 }
1298 /*
1299 * Write back each (modified) inode.
1300 */
1301 simple_lock(&mntvnode_slock);
1302 loop:
1303 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
1304 /*
1305 * If the vnode that we are about to sync is no longer
1306 * associated with this mount point, start over.
1307 */
1308 if (vp->v_mount != mp)
1309 goto loop;
1310 simple_lock(&vp->v_interlock);
1311 nvp = LIST_NEXT(vp, v_mntvnodes);
1312 ip = VTOI(vp);
1313 if (vp->v_type == VNON ||
1314 ((ip->i_flag &
1315 (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
1316 LIST_EMPTY(&vp->v_dirtyblkhd) &&
1317 vp->v_uobj.uo_npages == 0))
1318 {
1319 simple_unlock(&vp->v_interlock);
1320 continue;
1321 }
1322 simple_unlock(&mntvnode_slock);
1323 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1324 if (error) {
1325 simple_lock(&mntvnode_slock);
1326 if (error == ENOENT)
1327 goto loop;
1328 continue;
1329 }
1330 if (vp->v_type == VREG && waitfor == MNT_LAZY)
1331 error = VOP_UPDATE(vp, NULL, NULL, 0);
1332 else
1333 error = VOP_FSYNC(vp, cred,
1334 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
1335 if (error)
1336 allerror = error;
1337 vput(vp);
1338 simple_lock(&mntvnode_slock);
1339 }
1340 simple_unlock(&mntvnode_slock);
1341 /*
1342 * Force stale file system control information to be flushed.
1343 */
1344 if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
1345 if ((error = softdep_flushworklist(ump->um_mountp, &count, p)))
1346 allerror = error;
1347 /* Flushed work items may create new vnodes to clean */
1348 if (allerror == 0 && count) {
1349 simple_lock(&mntvnode_slock);
1350 goto loop;
1351 }
1352 }
1353 if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
1354 !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
1355 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1356 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1357 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
1358 allerror = error;
1359 VOP_UNLOCK(ump->um_devvp, 0);
1360 if (allerror == 0 && waitfor == MNT_WAIT) {
1361 simple_lock(&mntvnode_slock);
1362 goto loop;
1363 }
1364 }
1365 #ifdef QUOTA
1366 qsync(mp);
1367 #endif
1368 /*
1369 * Write back modified superblock.
1370 */
1371 if (fs->fs_fmod != 0) {
1372 fs->fs_fmod = 0;
1373 fs->fs_time = time.tv_sec;
1374 if ((error = ffs_cgupdate(ump, waitfor)))
1375 allerror = error;
1376 }
1377 return (allerror);
1378 }
1379
1380 /*
1381 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1382 * in from disk. If it is in core, wait for the lock bit to clear, then
1383 * return the inode locked. Detection and handling of mount points must be
1384 * done by the calling routine.
1385 */
1386 int
1387 ffs_vget(mp, ino, vpp)
1388 struct mount *mp;
1389 ino_t ino;
1390 struct vnode **vpp;
1391 {
1392 struct fs *fs;
1393 struct inode *ip;
1394 struct ufsmount *ump;
1395 struct buf *bp;
1396 struct vnode *vp;
1397 dev_t dev;
1398 int error;
1399
1400 ump = VFSTOUFS(mp);
1401 dev = ump->um_dev;
1402
1403 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1404 return (0);
1405
1406 /* Allocate a new vnode/inode. */
1407 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1408 *vpp = NULL;
1409 return (error);
1410 }
1411
1412 /*
1413 * If someone beat us to it while sleeping in getnewvnode(),
1414 * push back the freshly allocated vnode we don't need, and return.
1415 */
1416
1417 do {
1418 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
1419 ungetnewvnode(vp);
1420 return (0);
1421 }
1422 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1423
1424 vp->v_flag |= VLOCKSWORK;
1425
1426 /*
1427 * XXX MFS ends up here, too, to allocate an inode. Should we
1428 * XXX create another pool for MFS inodes?
1429 */
1430
1431 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
1432 memset(ip, 0, sizeof(struct inode));
1433 vp->v_data = ip;
1434 ip->i_vnode = vp;
1435 ip->i_ump = ump;
1436 ip->i_fs = fs = ump->um_fs;
1437 ip->i_dev = dev;
1438 ip->i_number = ino;
1439 LIST_INIT(&ip->i_pcbufhd);
1440 #ifdef QUOTA
1441 {
1442 int i;
1443
1444 for (i = 0; i < MAXQUOTAS; i++)
1445 ip->i_dquot[i] = NODQUOT;
1446 }
1447 #endif
1448
1449 /*
1450 * Put it onto its hash chain and lock it so that other requests for
1451 * this inode will block if they arrive while we are sleeping waiting
1452 * for old data structures to be purged or for the contents of the
1453 * disk portion of this inode to be read.
1454 */
1455
1456 ufs_ihashins(ip);
1457 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1458
1459 /* Read in the disk contents for the inode, copy into the inode. */
1460 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1461 (int)fs->fs_bsize, NOCRED, &bp);
1462 if (error) {
1463
1464 /*
1465 * The inode does not contain anything useful, so it would
1466 * be misleading to leave it on its hash chain. With mode
1467 * still zero, it will be unlinked and returned to the free
1468 * list by vput().
1469 */
1470
1471 vput(vp);
1472 brelse(bp);
1473 *vpp = NULL;
1474 return (error);
1475 }
1476 if (ip->i_ump->um_fstype == UFS1)
1477 ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
1478 else
1479 ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
1480 ffs_load_inode(bp, ip, fs, ino);
1481 if (DOINGSOFTDEP(vp))
1482 softdep_load_inodeblock(ip);
1483 else
1484 ip->i_ffs_effnlink = ip->i_nlink;
1485 brelse(bp);
1486
1487 /*
1488 * Initialize the vnode from the inode, check for aliases.
1489 * Note that the underlying vnode may have changed.
1490 */
1491
1492 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1493
1494 /*
1495 * Finish inode initialization now that aliasing has been resolved.
1496 */
1497
1498 genfs_node_init(vp, &ffs_genfsops);
1499 ip->i_devvp = ump->um_devvp;
1500 VREF(ip->i_devvp);
1501
1502 /*
1503 * Ensure that uid and gid are correct. This is a temporary
1504 * fix until fsck has been changed to do the update.
1505 */
1506
1507 if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1508 ip->i_uid = ip->i_ffs1_ouid; /* XXX */
1509 ip->i_gid = ip->i_ffs1_ogid; /* XXX */
1510 } /* XXX */
1511 uvm_vnp_setsize(vp, ip->i_size);
1512 *vpp = vp;
1513 return (0);
1514 }
1515
1516 /*
1517 * File handle to vnode
1518 *
1519 * Have to be really careful about stale file handles:
1520 * - check that the inode number is valid
1521 * - call ffs_vget() to get the locked inode
1522 * - check for an unallocated inode (i_mode == 0)
1523 * - check that the given client host has export rights and return
1524 * those rights via. exflagsp and credanonp
1525 */
1526 int
1527 ffs_fhtovp(mp, fhp, vpp)
1528 struct mount *mp;
1529 struct fid *fhp;
1530 struct vnode **vpp;
1531 {
1532 struct ufid *ufhp;
1533 struct fs *fs;
1534
1535 ufhp = (struct ufid *)fhp;
1536 fs = VFSTOUFS(mp)->um_fs;
1537 if (ufhp->ufid_ino < ROOTINO ||
1538 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1539 return (ESTALE);
1540 return (ufs_fhtovp(mp, ufhp, vpp));
1541 }
1542
1543 /*
1544 * Vnode pointer to File handle
1545 */
1546 /* ARGSUSED */
1547 int
1548 ffs_vptofh(vp, fhp)
1549 struct vnode *vp;
1550 struct fid *fhp;
1551 {
1552 struct inode *ip;
1553 struct ufid *ufhp;
1554
1555 ip = VTOI(vp);
1556 ufhp = (struct ufid *)fhp;
1557 ufhp->ufid_len = sizeof(struct ufid);
1558 ufhp->ufid_ino = ip->i_number;
1559 ufhp->ufid_gen = ip->i_gen;
1560 return (0);
1561 }
1562
1563 void
1564 ffs_init()
1565 {
1566 if (ffs_initcount++ > 0)
1567 return;
1568
1569 #ifdef _LKM
1570 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0,
1571 "ffsinopl", &pool_allocator_nointr);
1572 pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
1573 "dino1pl", &pool_allocator_nointr);
1574 pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0,
1575 "dino2pl", &pool_allocator_nointr);
1576 #endif
1577 softdep_initialize();
1578 ufs_init();
1579 }
1580
1581 void
1582 ffs_reinit()
1583 {
1584 softdep_reinitialize();
1585 ufs_reinit();
1586 }
1587
1588 void
1589 ffs_done()
1590 {
1591 if (--ffs_initcount > 0)
1592 return;
1593
1594 /* XXX softdep cleanup ? */
1595 ufs_done();
1596 #ifdef _LKM
1597 pool_destroy(&ffs_dinode2_pool);
1598 pool_destroy(&ffs_dinode1_pool);
1599 pool_destroy(&ffs_inode_pool);
1600 #endif
1601 }
1602
1603 SYSCTL_SETUP(sysctl_vfs_ffs_setup, "sysctl vfs.ffs subtree setup")
1604 {
1605 extern int doasyncfree;
1606 extern int ffs_log_changeopt;
1607
1608 sysctl_createv(clog, 0, NULL, NULL,
1609 CTLFLAG_PERMANENT,
1610 CTLTYPE_NODE, "vfs", NULL,
1611 NULL, 0, NULL, 0,
1612 CTL_VFS, CTL_EOL);
1613 sysctl_createv(clog, 0, NULL, NULL,
1614 CTLFLAG_PERMANENT,
1615 CTLTYPE_NODE, "ffs",
1616 SYSCTL_DESCR("Berkeley Fast File System"),
1617 NULL, 0, NULL, 0,
1618 CTL_VFS, 1, CTL_EOL);
1619
1620 /*
1621 * @@@ should we even bother with these first three?
1622 */
1623 sysctl_createv(clog, 0, NULL, NULL,
1624 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1625 CTLTYPE_INT, "doclusterread", NULL,
1626 sysctl_notavail, 0, NULL, 0,
1627 CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
1628 sysctl_createv(clog, 0, NULL, NULL,
1629 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1630 CTLTYPE_INT, "doclusterwrite", NULL,
1631 sysctl_notavail, 0, NULL, 0,
1632 CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
1633 sysctl_createv(clog, 0, NULL, NULL,
1634 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1635 CTLTYPE_INT, "doreallocblks", NULL,
1636 sysctl_notavail, 0, NULL, 0,
1637 CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
1638 sysctl_createv(clog, 0, NULL, NULL,
1639 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1640 CTLTYPE_INT, "doasyncfree",
1641 SYSCTL_DESCR("Release dirty blocks asynchronously"),
1642 NULL, 0, &doasyncfree, 0,
1643 CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
1644 sysctl_createv(clog, 0, NULL, NULL,
1645 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1646 CTLTYPE_INT, "log_changeopt",
1647 SYSCTL_DESCR("Log changes in optimization strategy"),
1648 NULL, 0, &ffs_log_changeopt, 0,
1649 CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
1650 }
1651
1652 /*
1653 * Write a superblock and associated information back to disk.
1654 */
1655 int
1656 ffs_sbupdate(mp, waitfor)
1657 struct ufsmount *mp;
1658 int waitfor;
1659 {
1660 struct fs *fs = mp->um_fs;
1661 struct buf *bp;
1662 int error = 0;
1663 u_int32_t saveflag;
1664
1665 bp = getblk(mp->um_devvp,
1666 fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
1667 (int)fs->fs_sbsize, 0, 0);
1668 saveflag = fs->fs_flags & FS_INTERNAL;
1669 fs->fs_flags &= ~FS_INTERNAL;
1670
1671 memcpy(bp->b_data, fs, fs->fs_sbsize);
1672
1673 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1674 #ifdef FFS_EI
1675 if (mp->um_flags & UFS_NEEDSWAP)
1676 ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
1677 #endif
1678 fs->fs_flags |= saveflag;
1679
1680 if (waitfor == MNT_WAIT)
1681 error = bwrite(bp);
1682 else
1683 bawrite(bp);
1684 return (error);
1685 }
1686
1687 int
1688 ffs_cgupdate(mp, waitfor)
1689 struct ufsmount *mp;
1690 int waitfor;
1691 {
1692 struct fs *fs = mp->um_fs;
1693 struct buf *bp;
1694 int blks;
1695 void *space;
1696 int i, size, error = 0, allerror = 0;
1697
1698 allerror = ffs_sbupdate(mp, waitfor);
1699 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1700 space = fs->fs_csp;
1701 for (i = 0; i < blks; i += fs->fs_frag) {
1702 size = fs->fs_bsize;
1703 if (i + fs->fs_frag > blks)
1704 size = (blks - i) * fs->fs_fsize;
1705 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1706 size, 0, 0);
1707 #ifdef FFS_EI
1708 if (mp->um_flags & UFS_NEEDSWAP)
1709 ffs_csum_swap((struct csum*)space,
1710 (struct csum*)bp->b_data, size);
1711 else
1712 #endif
1713 memcpy(bp->b_data, space, (u_int)size);
1714 space = (char *)space + size;
1715 if (waitfor == MNT_WAIT)
1716 error = bwrite(bp);
1717 else
1718 bawrite(bp);
1719 }
1720 if (!allerror && error)
1721 allerror = error;
1722 return (allerror);
1723 }
1724