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