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