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