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