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