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