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