ffs_vfsops.c revision 1.119 1 /* $NetBSD: ffs_vfsops.c,v 1.119 2003/08/07 16:34:31 agc 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.119 2003/08/07 16:34:31 agc 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_statfs,
101 ffs_sync,
102 ffs_vget,
103 ffs_fhtovp,
104 ffs_vptofh,
105 ffs_init,
106 ffs_reinit,
107 ffs_done,
108 ffs_sysctl,
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_statfs(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_flag & MNT_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 flags = WRITECLOSE;
306 if (mp->mnt_flag & MNT_FORCE)
307 flags |= FORCECLOSE;
308 if (mp->mnt_flag & MNT_SOFTDEP)
309 error = softdep_flushfiles(mp, flags, p);
310 else
311 error = ffs_flushfiles(mp, flags, p);
312 if (fs->fs_pendingblocks != 0 ||
313 fs->fs_pendinginodes != 0) {
314 printf("%s: update error: blocks %" PRId64
315 " files %d\n",
316 fs->fs_fsmnt, fs->fs_pendingblocks,
317 fs->fs_pendinginodes);
318 fs->fs_pendingblocks = 0;
319 fs->fs_pendinginodes = 0;
320 }
321 if (error == 0 &&
322 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
323 fs->fs_clean & FS_WASCLEAN) {
324 if (mp->mnt_flag & MNT_SOFTDEP)
325 fs->fs_flags &= ~FS_DOSOFTDEP;
326 fs->fs_clean = FS_ISCLEAN;
327 (void) ffs_sbupdate(ump, MNT_WAIT);
328 }
329 if (error)
330 return (error);
331 fs->fs_ronly = 1;
332 fs->fs_fmod = 0;
333 }
334
335 /*
336 * Flush soft dependencies if disabling it via an update
337 * mount. This may leave some items to be processed,
338 * so don't do this yet XXX.
339 */
340 if ((fs->fs_flags & FS_DOSOFTDEP) &&
341 !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
342 #ifdef notyet
343 flags = WRITECLOSE;
344 if (mp->mnt_flag & MNT_FORCE)
345 flags |= FORCECLOSE;
346 error = softdep_flushfiles(mp, flags, p);
347 if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
348 fs->fs_flags &= ~FS_DOSOFTDEP;
349 (void) ffs_sbupdate(ump, MNT_WAIT);
350 #elif defined(SOFTDEP)
351 mp->mnt_flag |= MNT_SOFTDEP;
352 #endif
353 }
354
355 /*
356 * When upgrading to a softdep mount, we must first flush
357 * all vnodes. (not done yet -- see above)
358 */
359 if (!(fs->fs_flags & FS_DOSOFTDEP) &&
360 (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
361 #ifdef notyet
362 flags = WRITECLOSE;
363 if (mp->mnt_flag & MNT_FORCE)
364 flags |= FORCECLOSE;
365 error = ffs_flushfiles(mp, flags, p);
366 #else
367 mp->mnt_flag &= ~MNT_SOFTDEP;
368 #endif
369 }
370
371 if (mp->mnt_flag & MNT_RELOAD) {
372 error = ffs_reload(mp, p->p_ucred, p);
373 if (error)
374 return (error);
375 }
376
377 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
378 /*
379 * Changing from read-only to read/write
380 */
381 fs->fs_ronly = 0;
382 fs->fs_clean <<= 1;
383 fs->fs_fmod = 1;
384 if ((fs->fs_flags & FS_DOSOFTDEP)) {
385 error = softdep_mount(devvp, mp, fs,
386 p->p_ucred);
387 if (error)
388 return (error);
389 }
390 }
391 if (args.fspec == 0) {
392 /*
393 * Process export requests.
394 */
395 return (vfs_export(mp, &ump->um_export, &args.export));
396 }
397 if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
398 (MNT_SOFTDEP | MNT_ASYNC)) {
399 printf("%s fs uses soft updates, ignoring async mode\n",
400 fs->fs_fsmnt);
401 mp->mnt_flag &= ~MNT_ASYNC;
402 }
403 }
404
405 error = set_statfs_info(path, UIO_USERSPACE, args.fspec,
406 UIO_USERSPACE, mp, p);
407 if (error == 0)
408 (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
409 sizeof(fs->fs_fsmnt));
410 if (mp->mnt_flag & MNT_SOFTDEP)
411 fs->fs_flags |= FS_DOSOFTDEP;
412 else
413 fs->fs_flags &= ~FS_DOSOFTDEP;
414 if (fs->fs_fmod != 0) { /* XXX */
415 fs->fs_fmod = 0;
416 if (fs->fs_clean & FS_WASCLEAN)
417 fs->fs_time = time.tv_sec;
418 else {
419 printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
420 mp->mnt_stat.f_mntfromname, fs->fs_clean);
421 printf("%s: lost blocks %" PRId64 " files %d\n",
422 mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
423 fs->fs_pendinginodes);
424 }
425 (void) ffs_cgupdate(ump, MNT_WAIT);
426 }
427 return error;
428 }
429
430 /*
431 * Reload all incore data for a filesystem (used after running fsck on
432 * the root filesystem and finding things to fix). The filesystem must
433 * be mounted read-only.
434 *
435 * Things to do to update the mount:
436 * 1) invalidate all cached meta-data.
437 * 2) re-read superblock from disk.
438 * 3) re-read summary information from disk.
439 * 4) invalidate all inactive vnodes.
440 * 5) invalidate all cached file data.
441 * 6) re-read inode data for all active vnodes.
442 */
443 int
444 ffs_reload(mountp, cred, p)
445 struct mount *mountp;
446 struct ucred *cred;
447 struct proc *p;
448 {
449 struct vnode *vp, *nvp, *devvp;
450 struct inode *ip;
451 void *space;
452 struct buf *bp;
453 struct fs *fs, *newfs;
454 struct partinfo dpart;
455 daddr_t sblockloc;
456 int i, blks, size, error;
457 int32_t *lp;
458 struct ufsmount *ump;
459
460 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
461 return (EINVAL);
462
463 ump = VFSTOUFS(mountp);
464 /*
465 * Step 1: invalidate all cached meta-data.
466 */
467 devvp = ump->um_devvp;
468 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
469 error = vinvalbuf(devvp, 0, cred, p, 0, 0);
470 VOP_UNLOCK(devvp, 0);
471 if (error)
472 panic("ffs_reload: dirty1");
473 /*
474 * Step 2: re-read superblock from disk.
475 */
476 fs = ump->um_fs;
477 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, p) != 0)
478 size = DEV_BSIZE;
479 else
480 size = dpart.disklab->d_secsize;
481 error = bread(devvp, fs->fs_sblockloc / size, fs->fs_sbsize,
482 NOCRED, &bp);
483 if (error) {
484 brelse(bp);
485 return (error);
486 }
487 newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
488 memcpy(newfs, bp->b_data, fs->fs_sbsize);
489 #ifdef SUPPORT_42POSTBLFMT_WRITE
490 if (fs->fs_old_postblformat == FS_42POSTBLFMT)
491 memcpy(ump->um_opostsave, &newfs->fs_old_postbl_start, 256);
492 #endif
493 #ifdef FFS_EI
494 if (ump->um_flags & UFS_NEEDSWAP) {
495 ffs_sb_swap((struct fs*)bp->b_data, newfs);
496 fs->fs_flags |= FS_SWAPPED;
497 }
498 #endif
499 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
500 newfs->fs_magic != FS_UFS2_MAGIC)||
501 newfs->fs_bsize > MAXBSIZE ||
502 newfs->fs_bsize < sizeof(struct fs)) {
503 brelse(bp);
504 free(newfs, M_UFSMNT);
505 return (EIO); /* XXX needs translation */
506 }
507 /*
508 * Copy pointer fields back into superblock before copying in XXX
509 * new superblock. These should really be in the ufsmount. XXX
510 * Note that important parameters (eg fs_ncg) are unchanged.
511 */
512 newfs->fs_csp = fs->fs_csp;
513 newfs->fs_maxcluster = fs->fs_maxcluster;
514 newfs->fs_contigdirs = fs->fs_contigdirs;
515 newfs->fs_ronly = fs->fs_ronly;
516 newfs->fs_active = fs->fs_active;
517 sblockloc = fs->fs_sblockloc;
518 memcpy(fs, newfs, (u_int)fs->fs_sbsize);
519 brelse(bp);
520 free(newfs, M_UFSMNT);
521
522 /* Recheck for apple UFS filesystem */
523 VFSTOUFS(mountp)->um_flags &= ~UFS_ISAPPLEUFS;
524 /* First check to see if this is tagged as an Apple UFS filesystem
525 * in the disklabel
526 */
527 if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) == 0) &&
528 (dpart.part->p_fstype == FS_APPLEUFS)) {
529 VFSTOUFS(mountp)->um_flags |= UFS_ISAPPLEUFS;
530 }
531 #ifdef APPLE_UFS
532 else {
533 /* Manually look for an apple ufs label, and if a valid one
534 * is found, then treat it like an Apple UFS filesystem anyway
535 */
536 error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
537 APPLEUFS_LABEL_SIZE, cred, &bp);
538 if (error) {
539 brelse(bp);
540 return (error);
541 }
542 error = ffs_appleufs_validate(fs->fs_fsmnt,
543 (struct appleufslabel *)bp->b_data,NULL);
544 if (error == 0) {
545 VFSTOUFS(mountp)->um_flags |= UFS_ISAPPLEUFS;
546 }
547 brelse(bp);
548 bp = NULL;
549 }
550 #else
551 if (VFSTOUFS(mountp)->um_flags & UFS_ISAPPLEUFS)
552 return (EIO);
553 #endif
554
555 mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
556 if (UFS_MPISAPPLEUFS(mountp)) {
557 /* see comment about NeXT below */
558 mountp->mnt_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
559 }
560 ffs_oldfscompat_read(fs, VFSTOUFS(mountp), fs->fs_sblockloc);
561 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
562 fs->fs_pendingblocks = 0;
563 fs->fs_pendinginodes = 0;
564 }
565
566 ffs_statfs(mountp, &mountp->mnt_stat, p);
567 /*
568 * Step 3: re-read summary information from disk.
569 */
570 blks = howmany(fs->fs_cssize, fs->fs_fsize);
571 space = fs->fs_csp;
572 for (i = 0; i < blks; i += fs->fs_frag) {
573 size = fs->fs_bsize;
574 if (i + fs->fs_frag > blks)
575 size = (blks - i) * fs->fs_fsize;
576 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
577 NOCRED, &bp);
578 if (error) {
579 brelse(bp);
580 return (error);
581 }
582 #ifdef FFS_EI
583 if (UFS_FSNEEDSWAP(fs))
584 ffs_csum_swap((struct csum *)bp->b_data,
585 (struct csum *)space, size);
586 else
587 #endif
588 memcpy(space, bp->b_data, (size_t)size);
589 space = (char *)space + size;
590 brelse(bp);
591 }
592 if ((fs->fs_flags & FS_DOSOFTDEP))
593 softdep_mount(devvp, mountp, fs, cred);
594 /*
595 * We no longer know anything about clusters per cylinder group.
596 */
597 if (fs->fs_contigsumsize > 0) {
598 lp = fs->fs_maxcluster;
599 for (i = 0; i < fs->fs_ncg; i++)
600 *lp++ = fs->fs_contigsumsize;
601 }
602
603 loop:
604 simple_lock(&mntvnode_slock);
605 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
606 if (vp->v_mount != mountp) {
607 simple_unlock(&mntvnode_slock);
608 goto loop;
609 }
610 nvp = vp->v_mntvnodes.le_next;
611 /*
612 * Step 4: invalidate all inactive vnodes.
613 */
614 if (vrecycle(vp, &mntvnode_slock, p))
615 goto loop;
616 /*
617 * Step 5: invalidate all cached file data.
618 */
619 simple_lock(&vp->v_interlock);
620 simple_unlock(&mntvnode_slock);
621 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
622 goto loop;
623 if (vinvalbuf(vp, 0, cred, p, 0, 0))
624 panic("ffs_reload: dirty2");
625 /*
626 * Step 6: re-read inode data for all active vnodes.
627 */
628 ip = VTOI(vp);
629 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
630 (int)fs->fs_bsize, NOCRED, &bp);
631 if (error) {
632 brelse(bp);
633 vput(vp);
634 return (error);
635 }
636 ffs_load_inode(bp, ip, fs, ip->i_number);
637 ip->i_ffs_effnlink = ip->i_nlink;
638 brelse(bp);
639 vput(vp);
640 simple_lock(&mntvnode_slock);
641 }
642 simple_unlock(&mntvnode_slock);
643 return (0);
644 }
645
646 /*
647 * Possible superblock locations ordered from most to least likely.
648 */
649 static int sblock_try[] = SBLOCKSEARCH;
650
651 /*
652 * Common code for mount and mountroot
653 */
654 int
655 ffs_mountfs(devvp, mp, p)
656 struct vnode *devvp;
657 struct mount *mp;
658 struct proc *p;
659 {
660 struct ufsmount *ump;
661 struct buf *bp;
662 struct fs *fs;
663 dev_t dev;
664 struct partinfo dpart;
665 void *space;
666 daddr_t sblockloc, fsblockloc;
667 int blks, fstype;
668 int error, i, size, ronly;
669 #ifdef FFS_EI
670 int needswap = 0; /* keep gcc happy */
671 #endif
672 int32_t *lp;
673 struct ucred *cred;
674 u_int32_t sbsize = 8192; /* keep gcc happy*/
675
676 dev = devvp->v_rdev;
677 cred = p ? p->p_ucred : NOCRED;
678 /*
679 * Disallow multiple mounts of the same device.
680 * Disallow mounting of a device that is currently in use
681 * (except for root, which might share swap device for miniroot).
682 * Flush out any old buffers remaining from a previous use.
683 */
684 if ((error = vfs_mountedon(devvp)) != 0)
685 return (error);
686 if (vcount(devvp) > 1 && devvp != rootvp)
687 return (EBUSY);
688 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
689 error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
690 VOP_UNLOCK(devvp, 0);
691 if (error)
692 return (error);
693
694 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
695 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
696 if (error)
697 return (error);
698 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) != 0)
699 size = DEV_BSIZE;
700 else
701 size = dpart.disklab->d_secsize;
702
703 bp = NULL;
704 ump = NULL;
705 fs = NULL;
706 fsblockloc = sblockloc = 0;
707 fstype = 0;
708
709 /*
710 * Try reading the superblock in each of its possible locations. */
711 for (i = 0; sblock_try[i] != -1; i++) {
712 error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
713 &bp);
714 if (error)
715 goto out;
716 fs = (struct fs*)bp->b_data;
717 fsblockloc = sblockloc = sblock_try[i];
718 if (fs->fs_magic == FS_UFS1_MAGIC) {
719 sbsize = fs->fs_sbsize;
720 fstype = UFS1;
721 #ifdef FFS_EI
722 needswap = 0;
723 } else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
724 sbsize = bswap32(fs->fs_sbsize);
725 fstype = UFS1;
726 needswap = 1;
727 #endif
728 } else if (fs->fs_magic == FS_UFS2_MAGIC) {
729 sbsize = fs->fs_sbsize;
730 fstype = UFS2;
731 fsblockloc = fs->fs_sblockloc;
732 #ifdef FFS_EI
733 needswap = 0;
734 } else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
735 sbsize = bswap32(fs->fs_sbsize);
736 fstype = UFS2;
737 fsblockloc = bswap64(fs->fs_sblockloc);
738 needswap = 1;
739 #endif
740 } else
741 goto next_sblock;
742
743 if ((fsblockloc == sblockloc ||
744 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)
745 && sbsize <= MAXBSIZE && sbsize >= sizeof(struct fs))
746 break;
747
748 next_sblock:
749 bp->b_flags |= B_NOCACHE;
750 brelse(bp);
751 bp = NULL;
752 }
753
754 if (sblock_try[i] == -1) {
755 error = EINVAL;
756 goto out;
757 }
758
759 fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
760 memcpy(fs, bp->b_data, sbsize);
761
762 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
763 memset(ump, 0, sizeof *ump);
764 ump->um_fs = fs;
765
766 if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
767 #ifdef SUPPORT_FS_42POSTBLFMT_WRITE
768 ump->um_opostsave = malloc(256, M_UFSMNT, M_WAITOK);
769 memcpy(ump->um_opostsave, &fs->fs_old_postbl_start, 256);
770 #else
771 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
772 error = EROFS;
773 goto out2;
774 }
775 #endif
776 }
777
778 #ifdef FFS_EI
779 if (needswap) {
780 ffs_sb_swap((struct fs*)bp->b_data, fs);
781 fs->fs_flags |= FS_SWAPPED;
782 }
783 #endif
784
785 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
786 fs->fs_pendingblocks = 0;
787 fs->fs_pendinginodes = 0;
788 }
789
790 ump->um_fstype = fstype;
791 if (fs->fs_sbsize < SBLOCKSIZE)
792 bp->b_flags |= B_INVAL;
793 brelse(bp);
794 bp = NULL;
795
796 ffs_oldfscompat_read(fs, ump, sblockloc);
797
798 /* First check to see if this is tagged as an Apple UFS filesystem
799 * in the disklabel
800 */
801 if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) == 0) &&
802 (dpart.part->p_fstype == FS_APPLEUFS)) {
803 ump->um_flags |= UFS_ISAPPLEUFS;
804 }
805 #ifdef APPLE_UFS
806 else {
807 /* Manually look for an apple ufs label, and if a valid one
808 * is found, then treat it like an Apple UFS filesystem anyway
809 */
810 error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
811 APPLEUFS_LABEL_SIZE, cred, &bp);
812 if (error)
813 goto out;
814 error = ffs_appleufs_validate(fs->fs_fsmnt,
815 (struct appleufslabel *)bp->b_data,NULL);
816 if (error == 0) {
817 ump->um_flags |= UFS_ISAPPLEUFS;
818 }
819 brelse(bp);
820 bp = NULL;
821 }
822 #else
823 if (ump->um_flags & UFS_ISAPPLEUFS) {
824 error = EINVAL;
825 goto out;
826 }
827 #endif
828
829 /*
830 * verify that we can access the last block in the fs
831 * if we're mounting read/write.
832 */
833
834 if (!ronly) {
835 error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
836 cred, &bp);
837 if (bp->b_bcount != fs->fs_fsize)
838 error = EINVAL;
839 bp->b_flags |= B_INVAL;
840 if (error)
841 goto out;
842 brelse(bp);
843 bp = NULL;
844 }
845
846 fs->fs_ronly = ronly;
847 if (ronly == 0) {
848 fs->fs_clean <<= 1;
849 fs->fs_fmod = 1;
850 }
851 size = fs->fs_cssize;
852 blks = howmany(size, fs->fs_fsize);
853 if (fs->fs_contigsumsize > 0)
854 size += fs->fs_ncg * sizeof(int32_t);
855 size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
856 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
857 fs->fs_csp = space;
858 for (i = 0; i < blks; i += fs->fs_frag) {
859 size = fs->fs_bsize;
860 if (i + fs->fs_frag > blks)
861 size = (blks - i) * fs->fs_fsize;
862 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
863 cred, &bp);
864 if (error) {
865 free(fs->fs_csp, M_UFSMNT);
866 goto out2;
867 }
868 #ifdef FFS_EI
869 if (needswap)
870 ffs_csum_swap((struct csum *)bp->b_data,
871 (struct csum *)space, size);
872 else
873 #endif
874 memcpy(space, bp->b_data, (u_int)size);
875
876 space = (char *)space + size;
877 brelse(bp);
878 bp = NULL;
879 }
880 if (fs->fs_contigsumsize > 0) {
881 fs->fs_maxcluster = lp = space;
882 for (i = 0; i < fs->fs_ncg; i++)
883 *lp++ = fs->fs_contigsumsize;
884 space = lp;
885 }
886 size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
887 fs->fs_contigdirs = space;
888 space = (char *)space + size;
889 memset(fs->fs_contigdirs, 0, size);
890 /* Compatibility for old filesystems - XXX */
891 if (fs->fs_avgfilesize <= 0)
892 fs->fs_avgfilesize = AVFILESIZ;
893 if (fs->fs_avgfpdir <= 0)
894 fs->fs_avgfpdir = AFPDIR;
895 mp->mnt_data = ump;
896 mp->mnt_stat.f_fsid.val[0] = (long)dev;
897 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
898 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
899 if (UFS_MPISAPPLEUFS(mp)) {
900 /* NeXT used to keep short symlinks in the inode even
901 * when using FS_42INODEFMT. In that case fs->fs_maxsymlinklen
902 * is probably -1, but we still need to be able to identify
903 * short symlinks.
904 */
905 mp->mnt_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
906 }
907 mp->mnt_fs_bshift = fs->fs_bshift;
908 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
909 mp->mnt_flag |= MNT_LOCAL;
910 #ifdef FFS_EI
911 if (needswap)
912 ump->um_flags |= UFS_NEEDSWAP;
913 #endif
914 ump->um_mountp = mp;
915 ump->um_dev = dev;
916 ump->um_devvp = devvp;
917 ump->um_nindir = fs->fs_nindir;
918 ump->um_lognindir = ffs(fs->fs_nindir) - 1;
919 ump->um_bptrtodb = fs->fs_fsbtodb;
920 ump->um_seqinc = fs->fs_frag;
921 for (i = 0; i < MAXQUOTAS; i++)
922 ump->um_quotas[i] = NULLVP;
923 devvp->v_specmountpoint = mp;
924 if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
925 error = softdep_mount(devvp, mp, fs, cred);
926 if (error) {
927 free(fs->fs_csp, M_UFSMNT);
928 goto out;
929 }
930 }
931 return (0);
932 out2:
933 free(fs, M_UFSMNT);
934 out:
935 devvp->v_specmountpoint = NULL;
936 if (bp)
937 brelse(bp);
938 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
939 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
940 VOP_UNLOCK(devvp, 0);
941 if (ump) {
942 free(ump, M_UFSMNT);
943 mp->mnt_data = NULL;
944 }
945 return (error);
946 }
947
948 /*
949 * Sanity checks for loading old filesystem superblocks.
950 * See ffs_oldfscompat_write below for unwound actions.
951 *
952 * XXX - Parts get retired eventually.
953 * Unfortunately new bits get added.
954 */
955 static void
956 ffs_oldfscompat_read(fs, ump, sblockloc)
957 struct fs *fs;
958 struct ufsmount *ump;
959 daddr_t sblockloc;
960 {
961 off_t maxfilesize;
962
963 if (fs->fs_magic != FS_UFS1_MAGIC)
964 return;
965
966 /*
967 * If not yet done, update fs_flags location and value of fs_sblockloc.
968 */
969 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
970 fs->fs_flags = fs->fs_old_flags;
971 fs->fs_old_flags |= FS_FLAGS_UPDATED;
972 fs->fs_sblockloc = sblockloc;
973 }
974
975 /*
976 * If the new fields haven't been set yet, or if the filesystem
977 * was mounted and modified by an old kernel, use the old csum
978 * totals.
979 */
980 if (fs->fs_maxbsize != fs->fs_bsize || fs->fs_time < fs->fs_old_time) {
981 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
982 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
983 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
984 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
985 }
986
987 /*
988 * If not yet done, update UFS1 superblock with new wider fields.
989 */
990 if (fs->fs_maxbsize != fs->fs_bsize) {
991 fs->fs_maxbsize = fs->fs_bsize;
992 fs->fs_time = fs->fs_old_time;
993 fs->fs_size = fs->fs_old_size;
994 fs->fs_dsize = fs->fs_old_dsize;
995 fs->fs_csaddr = fs->fs_old_csaddr;
996 }
997
998 if (fs->fs_old_inodefmt < FS_44INODEFMT) {
999 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
1000 fs->fs_qbmask = ~fs->fs_bmask;
1001 fs->fs_qfmask = ~fs->fs_fmask;
1002 }
1003
1004 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1005 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1006 if (fs->fs_maxfilesize > maxfilesize)
1007 fs->fs_maxfilesize = maxfilesize;
1008
1009 /* Compatibility for old filesystems */
1010 if (fs->fs_avgfilesize <= 0)
1011 fs->fs_avgfilesize = AVFILESIZ;
1012 if (fs->fs_avgfpdir <= 0)
1013 fs->fs_avgfpdir = AFPDIR;
1014 #if 0
1015 if (bigcgs) {
1016 fs->fs_save_cgsize = fs->fs_cgsize;
1017 fs->fs_cgsize = fs->fs_bsize;
1018 }
1019 #endif
1020 }
1021
1022 /*
1023 * Unwinding superblock updates for old filesystems.
1024 * See ffs_oldfscompat_read above for details.
1025 *
1026 * XXX - Parts get retired eventually.
1027 * Unfortunately new bits get added.
1028 */
1029 static void
1030 ffs_oldfscompat_write(fs, ump)
1031 struct fs *fs;
1032 struct ufsmount *ump;
1033 {
1034 if (fs->fs_magic != FS_UFS1_MAGIC)
1035 return;
1036
1037 /*
1038 * OS X somehow still seems to use this field and panic.
1039 * Just set it to zero.
1040 */
1041 if (ump->um_flags & UFS_ISAPPLEUFS)
1042 fs->fs_old_nrpos = 0;
1043 /*
1044 * Copy back UFS2 updated fields that UFS1 inspects.
1045 */
1046
1047 fs->fs_old_time = fs->fs_time;
1048 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1049 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1050 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1051 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1052 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1053
1054 #if 0
1055 if (bigcgs) {
1056 fs->fs_cgsize = fs->fs_save_cgsize;
1057 fs->fs_save_cgsize = 0;
1058 }
1059 #endif
1060 }
1061
1062 /*
1063 * unmount system call
1064 */
1065 int
1066 ffs_unmount(mp, mntflags, p)
1067 struct mount *mp;
1068 int mntflags;
1069 struct proc *p;
1070 {
1071 struct ufsmount *ump;
1072 struct fs *fs;
1073 int error, flags, penderr;
1074
1075 penderr = 0;
1076 flags = 0;
1077 if (mntflags & MNT_FORCE)
1078 flags |= FORCECLOSE;
1079 if (mp->mnt_flag & MNT_SOFTDEP) {
1080 if ((error = softdep_flushfiles(mp, flags, p)) != 0)
1081 return (error);
1082 } else {
1083 if ((error = ffs_flushfiles(mp, flags, p)) != 0)
1084 return (error);
1085 }
1086 ump = VFSTOUFS(mp);
1087 fs = ump->um_fs;
1088 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1089 printf("%s: unmount pending error: blocks %" PRId64
1090 " files %d\n",
1091 fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
1092 fs->fs_pendingblocks = 0;
1093 fs->fs_pendinginodes = 0;
1094 penderr = 1;
1095 }
1096 if (fs->fs_ronly == 0 &&
1097 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1098 fs->fs_clean & FS_WASCLEAN) {
1099 /*
1100 * XXXX don't mark fs clean in the case of softdep
1101 * pending block errors, until they are fixed.
1102 */
1103 if (penderr == 0) {
1104 if (mp->mnt_flag & MNT_SOFTDEP)
1105 fs->fs_flags &= ~FS_DOSOFTDEP;
1106 fs->fs_clean = FS_ISCLEAN;
1107 }
1108 fs->fs_fmod = 0;
1109 (void) ffs_sbupdate(ump, MNT_WAIT);
1110 }
1111 if (ump->um_devvp->v_type != VBAD)
1112 ump->um_devvp->v_specmountpoint = NULL;
1113 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1114 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
1115 NOCRED, p);
1116 vput(ump->um_devvp);
1117 free(fs->fs_csp, M_UFSMNT);
1118 free(fs, M_UFSMNT);
1119 #ifdef SUPPORT_FS_42POSTBLFMT_WRITE
1120 if (ump->um_opostsave != NULL)
1121 free(ump->um_opostsave, M_UFSMNT);
1122 #endif
1123 free(ump, M_UFSMNT);
1124 mp->mnt_data = NULL;
1125 mp->mnt_flag &= ~MNT_LOCAL;
1126 return (error);
1127 }
1128
1129 /*
1130 * Flush out all the files in a filesystem.
1131 */
1132 int
1133 ffs_flushfiles(mp, flags, p)
1134 struct mount *mp;
1135 int flags;
1136 struct proc *p;
1137 {
1138 extern int doforce;
1139 struct ufsmount *ump;
1140 int error;
1141
1142 if (!doforce)
1143 flags &= ~FORCECLOSE;
1144 ump = VFSTOUFS(mp);
1145 #ifdef QUOTA
1146 if (mp->mnt_flag & MNT_QUOTA) {
1147 int i;
1148 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
1149 return (error);
1150 for (i = 0; i < MAXQUOTAS; i++) {
1151 if (ump->um_quotas[i] == NULLVP)
1152 continue;
1153 quotaoff(p, mp, i);
1154 }
1155 /*
1156 * Here we fall through to vflush again to ensure
1157 * that we have gotten rid of all the system vnodes.
1158 */
1159 }
1160 #endif
1161 /*
1162 * Flush all the files.
1163 */
1164 error = vflush(mp, NULLVP, flags);
1165 if (error)
1166 return (error);
1167 /*
1168 * Flush filesystem metadata.
1169 */
1170 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1171 error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
1172 VOP_UNLOCK(ump->um_devvp, 0);
1173 return (error);
1174 }
1175
1176 /*
1177 * Get file system statistics.
1178 */
1179 int
1180 ffs_statfs(mp, sbp, p)
1181 struct mount *mp;
1182 struct statfs *sbp;
1183 struct proc *p;
1184 {
1185 struct ufsmount *ump;
1186 struct fs *fs;
1187
1188 ump = VFSTOUFS(mp);
1189 fs = ump->um_fs;
1190 #ifdef COMPAT_09
1191 sbp->f_type = 1;
1192 #else
1193 sbp->f_type = 0;
1194 #endif
1195 sbp->f_bsize = fs->fs_fsize;
1196 sbp->f_iosize = fs->fs_bsize;
1197 sbp->f_blocks = fs->fs_dsize;
1198 sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1199 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1200 sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
1201 (100 - fs->fs_minfree) / (u_int64_t) 100) -
1202 (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
1203 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1204 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1205 copy_statfs_info(sbp, mp);
1206 return (0);
1207 }
1208
1209 /*
1210 * Go through the disk queues to initiate sandbagged IO;
1211 * go through the inodes to write those that have been modified;
1212 * initiate the writing of the super block if it has been modified.
1213 *
1214 * Note: we are always called with the filesystem marked `MPBUSY'.
1215 */
1216 int
1217 ffs_sync(mp, waitfor, cred, p)
1218 struct mount *mp;
1219 int waitfor;
1220 struct ucred *cred;
1221 struct proc *p;
1222 {
1223 struct vnode *vp, *nvp;
1224 struct inode *ip;
1225 struct ufsmount *ump = VFSTOUFS(mp);
1226 struct fs *fs;
1227 int error, allerror = 0;
1228
1229 fs = ump->um_fs;
1230 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1231 printf("fs = %s\n", fs->fs_fsmnt);
1232 panic("update: rofs mod");
1233 }
1234 /*
1235 * Write back each (modified) inode.
1236 */
1237 simple_lock(&mntvnode_slock);
1238 loop:
1239 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
1240 /*
1241 * If the vnode that we are about to sync is no longer
1242 * associated with this mount point, start over.
1243 */
1244 if (vp->v_mount != mp)
1245 goto loop;
1246 simple_lock(&vp->v_interlock);
1247 nvp = LIST_NEXT(vp, v_mntvnodes);
1248 ip = VTOI(vp);
1249 if (vp->v_type == VNON ||
1250 ((ip->i_flag &
1251 (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
1252 LIST_EMPTY(&vp->v_dirtyblkhd) &&
1253 vp->v_uobj.uo_npages == 0))
1254 {
1255 simple_unlock(&vp->v_interlock);
1256 continue;
1257 }
1258 simple_unlock(&mntvnode_slock);
1259 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1260 if (error) {
1261 simple_lock(&mntvnode_slock);
1262 if (error == ENOENT)
1263 goto loop;
1264 continue;
1265 }
1266 if ((error = VOP_FSYNC(vp, cred,
1267 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
1268 allerror = error;
1269 vput(vp);
1270 simple_lock(&mntvnode_slock);
1271 }
1272 simple_unlock(&mntvnode_slock);
1273 /*
1274 * Force stale file system control information to be flushed.
1275 */
1276 if (waitfor != MNT_LAZY) {
1277 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
1278 waitfor = MNT_NOWAIT;
1279 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1280 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1281 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
1282 allerror = error;
1283 VOP_UNLOCK(ump->um_devvp, 0);
1284 }
1285 #ifdef QUOTA
1286 qsync(mp);
1287 #endif
1288 /*
1289 * Write back modified superblock.
1290 */
1291 if (fs->fs_fmod != 0) {
1292 fs->fs_fmod = 0;
1293 fs->fs_time = time.tv_sec;
1294 if ((error = ffs_cgupdate(ump, waitfor)))
1295 allerror = error;
1296 }
1297 return (allerror);
1298 }
1299
1300 /*
1301 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1302 * in from disk. If it is in core, wait for the lock bit to clear, then
1303 * return the inode locked. Detection and handling of mount points must be
1304 * done by the calling routine.
1305 */
1306 int
1307 ffs_vget(mp, ino, vpp)
1308 struct mount *mp;
1309 ino_t ino;
1310 struct vnode **vpp;
1311 {
1312 struct fs *fs;
1313 struct inode *ip;
1314 struct ufsmount *ump;
1315 struct buf *bp;
1316 struct vnode *vp;
1317 dev_t dev;
1318 int error;
1319
1320 ump = VFSTOUFS(mp);
1321 dev = ump->um_dev;
1322
1323 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1324 return (0);
1325
1326 /* Allocate a new vnode/inode. */
1327 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1328 *vpp = NULL;
1329 return (error);
1330 }
1331
1332 /*
1333 * If someone beat us to it while sleeping in getnewvnode(),
1334 * push back the freshly allocated vnode we don't need, and return.
1335 */
1336
1337 do {
1338 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
1339 ungetnewvnode(vp);
1340 return (0);
1341 }
1342 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1343
1344 /*
1345 * XXX MFS ends up here, too, to allocate an inode. Should we
1346 * XXX create another pool for MFS inodes?
1347 */
1348
1349 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
1350 memset(ip, 0, sizeof(struct inode));
1351 vp->v_data = ip;
1352 ip->i_vnode = vp;
1353 ip->i_ump = ump;
1354 ip->i_fs = fs = ump->um_fs;
1355 ip->i_dev = dev;
1356 ip->i_number = ino;
1357 LIST_INIT(&ip->i_pcbufhd);
1358 #ifdef QUOTA
1359 {
1360 int i;
1361
1362 for (i = 0; i < MAXQUOTAS; i++)
1363 ip->i_dquot[i] = NODQUOT;
1364 }
1365 #endif
1366
1367 /*
1368 * Put it onto its hash chain and lock it so that other requests for
1369 * this inode will block if they arrive while we are sleeping waiting
1370 * for old data structures to be purged or for the contents of the
1371 * disk portion of this inode to be read.
1372 */
1373
1374 ufs_ihashins(ip);
1375 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1376
1377 /* Read in the disk contents for the inode, copy into the inode. */
1378 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1379 (int)fs->fs_bsize, NOCRED, &bp);
1380 if (error) {
1381
1382 /*
1383 * The inode does not contain anything useful, so it would
1384 * be misleading to leave it on its hash chain. With mode
1385 * still zero, it will be unlinked and returned to the free
1386 * list by vput().
1387 */
1388
1389 vput(vp);
1390 brelse(bp);
1391 *vpp = NULL;
1392 return (error);
1393 }
1394 if (ip->i_ump->um_fstype == UFS1)
1395 ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
1396 else
1397 ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
1398 ffs_load_inode(bp, ip, fs, ino);
1399 if (DOINGSOFTDEP(vp))
1400 softdep_load_inodeblock(ip);
1401 else
1402 ip->i_ffs_effnlink = ip->i_nlink;
1403 brelse(bp);
1404
1405 /*
1406 * Initialize the vnode from the inode, check for aliases.
1407 * Note that the underlying vnode may have changed.
1408 */
1409
1410 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1411
1412 /*
1413 * Finish inode initialization now that aliasing has been resolved.
1414 */
1415
1416 genfs_node_init(vp, &ffs_genfsops);
1417 ip->i_devvp = ump->um_devvp;
1418 VREF(ip->i_devvp);
1419
1420 /*
1421 * Ensure that uid and gid are correct. This is a temporary
1422 * fix until fsck has been changed to do the update.
1423 */
1424
1425 if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1426 ip->i_uid = ip->i_ffs1_ouid; /* XXX */
1427 ip->i_gid = ip->i_ffs1_ogid; /* XXX */
1428 } /* XXX */
1429 uvm_vnp_setsize(vp, ip->i_size);
1430 *vpp = vp;
1431 return (0);
1432 }
1433
1434 /*
1435 * File handle to vnode
1436 *
1437 * Have to be really careful about stale file handles:
1438 * - check that the inode number is valid
1439 * - call ffs_vget() to get the locked inode
1440 * - check for an unallocated inode (i_mode == 0)
1441 * - check that the given client host has export rights and return
1442 * those rights via. exflagsp and credanonp
1443 */
1444 int
1445 ffs_fhtovp(mp, fhp, vpp)
1446 struct mount *mp;
1447 struct fid *fhp;
1448 struct vnode **vpp;
1449 {
1450 struct ufid *ufhp;
1451 struct fs *fs;
1452
1453 ufhp = (struct ufid *)fhp;
1454 fs = VFSTOUFS(mp)->um_fs;
1455 if (ufhp->ufid_ino < ROOTINO ||
1456 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1457 return (ESTALE);
1458 return (ufs_fhtovp(mp, ufhp, vpp));
1459 }
1460
1461 /*
1462 * Vnode pointer to File handle
1463 */
1464 /* ARGSUSED */
1465 int
1466 ffs_vptofh(vp, fhp)
1467 struct vnode *vp;
1468 struct fid *fhp;
1469 {
1470 struct inode *ip;
1471 struct ufid *ufhp;
1472
1473 ip = VTOI(vp);
1474 ufhp = (struct ufid *)fhp;
1475 ufhp->ufid_len = sizeof(struct ufid);
1476 ufhp->ufid_ino = ip->i_number;
1477 ufhp->ufid_gen = ip->i_gen;
1478 return (0);
1479 }
1480
1481 void
1482 ffs_init()
1483 {
1484 if (ffs_initcount++ > 0)
1485 return;
1486
1487 softdep_initialize();
1488 ufs_init();
1489
1490 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
1491 &pool_allocator_nointr);
1492 pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
1493 "dino1pl", &pool_allocator_nointr);
1494 pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0,
1495 "dino2pl", &pool_allocator_nointr);
1496 }
1497
1498 void
1499 ffs_reinit()
1500 {
1501 softdep_reinitialize();
1502 ufs_reinit();
1503 }
1504
1505 void
1506 ffs_done()
1507 {
1508 if (--ffs_initcount > 0)
1509 return;
1510
1511 /* XXX softdep cleanup ? */
1512 ufs_done();
1513 pool_destroy(&ffs_inode_pool);
1514 }
1515
1516 int
1517 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1518 int *name;
1519 u_int namelen;
1520 void *oldp;
1521 size_t *oldlenp;
1522 void *newp;
1523 size_t newlen;
1524 struct proc *p;
1525 {
1526 extern int doasyncfree;
1527 extern int ffs_log_changeopt;
1528
1529 /* all sysctl names at this level are terminal */
1530 if (namelen != 1)
1531 return (ENOTDIR); /* overloaded */
1532
1533 switch (name[0]) {
1534 case FFS_ASYNCFREE:
1535 return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
1536 case FFS_LOG_CHANGEOPT:
1537 return (sysctl_int(oldp, oldlenp, newp, newlen,
1538 &ffs_log_changeopt));
1539 default:
1540 return (EOPNOTSUPP);
1541 }
1542 /* NOTREACHED */
1543 }
1544
1545 /*
1546 * Write a superblock and associated information back to disk.
1547 */
1548 int
1549 ffs_sbupdate(mp, waitfor)
1550 struct ufsmount *mp;
1551 int waitfor;
1552 {
1553 struct fs *fs = mp->um_fs;
1554 struct buf *bp;
1555 int error = 0;
1556 u_int32_t saveflag;
1557
1558 bp = getblk(mp->um_devvp,
1559 fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
1560 (int)fs->fs_sbsize, 0, 0);
1561 saveflag = fs->fs_flags & FS_INTERNAL;
1562 fs->fs_flags &= ~FS_INTERNAL;
1563
1564 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1565 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1566 printf("%s: correcting fs_sblockloc from %" PRId64 " to %d\n",
1567 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1568 fs->fs_sblockloc = SBLOCK_UFS1;
1569 }
1570
1571 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1572 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1573 printf("%s: correcting fs_sblockloc from %" PRId64 " to %d\n",
1574 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1575 fs->fs_sblockloc = SBLOCK_UFS2;
1576 }
1577
1578 memcpy(bp->b_data, fs, fs->fs_sbsize);
1579
1580 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1581 #ifdef FFS_EI
1582 if (mp->um_flags & UFS_NEEDSWAP)
1583 ffs_sb_swap(fs, (struct fs *)bp->b_data);
1584 #endif
1585 #ifdef SUPPORT_FS_42POSTBLFMT_WRITE
1586 if (fs->fs_old_postblformat == FS_42POSTBLFMT)
1587 memcpy(&((struct fs *)bp->b_data)->fs_old_postbl_start,
1588 mp->um_opostsave, 256);
1589 #endif
1590 fs->fs_flags |= saveflag;
1591
1592 if (waitfor == MNT_WAIT)
1593 error = bwrite(bp);
1594 else
1595 bawrite(bp);
1596 return (error);
1597 }
1598
1599 int
1600 ffs_cgupdate(mp, waitfor)
1601 struct ufsmount *mp;
1602 int waitfor;
1603 {
1604 struct fs *fs = mp->um_fs;
1605 struct buf *bp;
1606 int blks;
1607 void *space;
1608 int i, size, error = 0, allerror = 0;
1609
1610 allerror = ffs_sbupdate(mp, waitfor);
1611 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1612 space = fs->fs_csp;
1613 for (i = 0; i < blks; i += fs->fs_frag) {
1614 size = fs->fs_bsize;
1615 if (i + fs->fs_frag > blks)
1616 size = (blks - i) * fs->fs_fsize;
1617 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1618 size, 0, 0);
1619 #ifdef FFS_EI
1620 if (mp->um_flags & UFS_NEEDSWAP)
1621 ffs_csum_swap((struct csum*)space,
1622 (struct csum*)bp->b_data, size);
1623 else
1624 #endif
1625 memcpy(bp->b_data, space, (u_int)size);
1626 space = (char *)space + size;
1627 if (waitfor == MNT_WAIT)
1628 error = bwrite(bp);
1629 else
1630 bawrite(bp);
1631 }
1632 if (!allerror && error)
1633 allerror = error;
1634 return (allerror);
1635 }
1636