ffs_vfsops.c revision 1.120 1 /* $NetBSD: ffs_vfsops.c,v 1.120 2003/09/13 13:47:04 bouyer 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.120 2003/09/13 13:47:04 bouyer 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 UFS1 superblock with new wider fields,
968 * and update fs_flags location and value of fs_sblockloc.
969 */
970 if (fs->fs_maxbsize != fs->fs_bsize) {
971 fs->fs_maxbsize = fs->fs_bsize;
972 fs->fs_time = fs->fs_old_time;
973 fs->fs_size = fs->fs_old_size;
974 fs->fs_dsize = fs->fs_old_dsize;
975 fs->fs_csaddr = fs->fs_old_csaddr;
976 fs->fs_sblockloc = sblockloc;
977 fs->fs_flags = fs->fs_old_flags;
978 fs->fs_old_flags |= FS_FLAGS_UPDATED;
979 }
980 /*
981 * If the new fields haven't been set yet, or if the filesystem
982 * was mounted and modified by an old kernel, use the old csum
983 * totals, and update the flags
984 */
985 if (fs->fs_maxbsize != fs->fs_bsize || fs->fs_time < fs->fs_old_time) {
986 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
987 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
988 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
989 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
990 fs->fs_flags |= (fs->fs_old_flags & ~FS_FLAGS_UPDATED);
991 }
992
993
994 if (fs->fs_old_inodefmt < FS_44INODEFMT) {
995 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
996 fs->fs_qbmask = ~fs->fs_bmask;
997 fs->fs_qfmask = ~fs->fs_fmask;
998 }
999
1000 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1001 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1002 if (fs->fs_maxfilesize > maxfilesize)
1003 fs->fs_maxfilesize = maxfilesize;
1004
1005 /* Compatibility for old filesystems */
1006 if (fs->fs_avgfilesize <= 0)
1007 fs->fs_avgfilesize = AVFILESIZ;
1008 if (fs->fs_avgfpdir <= 0)
1009 fs->fs_avgfpdir = AFPDIR;
1010 #if 0
1011 if (bigcgs) {
1012 fs->fs_save_cgsize = fs->fs_cgsize;
1013 fs->fs_cgsize = fs->fs_bsize;
1014 }
1015 #endif
1016 }
1017
1018 /*
1019 * Unwinding superblock updates for old filesystems.
1020 * See ffs_oldfscompat_read above for details.
1021 *
1022 * XXX - Parts get retired eventually.
1023 * Unfortunately new bits get added.
1024 */
1025 static void
1026 ffs_oldfscompat_write(fs, ump)
1027 struct fs *fs;
1028 struct ufsmount *ump;
1029 {
1030 if (fs->fs_magic != FS_UFS1_MAGIC)
1031 return;
1032
1033 /*
1034 * if none of the newer flags are used, copy back fs_flags
1035 * to fs_old_flags
1036 */
1037 if ((fs->fs_flags & ~(FS_UNCLEAN|FS_DOSOFTDEP)) == 0)
1038 fs->fs_old_flags = fs->fs_flags & (FS_UNCLEAN|FS_DOSOFTDEP);
1039 /*
1040 * OS X somehow still seems to use this field and panic.
1041 * Just set it to zero.
1042 */
1043 if (ump->um_flags & UFS_ISAPPLEUFS)
1044 fs->fs_old_nrpos = 0;
1045 /*
1046 * Copy back UFS2 updated fields that UFS1 inspects.
1047 */
1048
1049 fs->fs_old_time = fs->fs_time;
1050 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1051 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1052 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1053 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1054 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1055
1056 #if 0
1057 if (bigcgs) {
1058 fs->fs_cgsize = fs->fs_save_cgsize;
1059 fs->fs_save_cgsize = 0;
1060 }
1061 #endif
1062 }
1063
1064 /*
1065 * unmount system call
1066 */
1067 int
1068 ffs_unmount(mp, mntflags, p)
1069 struct mount *mp;
1070 int mntflags;
1071 struct proc *p;
1072 {
1073 struct ufsmount *ump;
1074 struct fs *fs;
1075 int error, flags, penderr;
1076
1077 penderr = 0;
1078 flags = 0;
1079 if (mntflags & MNT_FORCE)
1080 flags |= FORCECLOSE;
1081 if (mp->mnt_flag & MNT_SOFTDEP) {
1082 if ((error = softdep_flushfiles(mp, flags, p)) != 0)
1083 return (error);
1084 } else {
1085 if ((error = ffs_flushfiles(mp, flags, p)) != 0)
1086 return (error);
1087 }
1088 ump = VFSTOUFS(mp);
1089 fs = ump->um_fs;
1090 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1091 printf("%s: unmount pending error: blocks %" PRId64
1092 " files %d\n",
1093 fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
1094 fs->fs_pendingblocks = 0;
1095 fs->fs_pendinginodes = 0;
1096 penderr = 1;
1097 }
1098 if (fs->fs_ronly == 0 &&
1099 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1100 fs->fs_clean & FS_WASCLEAN) {
1101 /*
1102 * XXXX don't mark fs clean in the case of softdep
1103 * pending block errors, until they are fixed.
1104 */
1105 if (penderr == 0) {
1106 if (mp->mnt_flag & MNT_SOFTDEP)
1107 fs->fs_flags &= ~FS_DOSOFTDEP;
1108 fs->fs_clean = FS_ISCLEAN;
1109 }
1110 fs->fs_fmod = 0;
1111 (void) ffs_sbupdate(ump, MNT_WAIT);
1112 }
1113 if (ump->um_devvp->v_type != VBAD)
1114 ump->um_devvp->v_specmountpoint = NULL;
1115 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1116 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
1117 NOCRED, p);
1118 vput(ump->um_devvp);
1119 free(fs->fs_csp, M_UFSMNT);
1120 free(fs, M_UFSMNT);
1121 #ifdef SUPPORT_FS_42POSTBLFMT_WRITE
1122 if (ump->um_opostsave != NULL)
1123 free(ump->um_opostsave, M_UFSMNT);
1124 #endif
1125 free(ump, M_UFSMNT);
1126 mp->mnt_data = NULL;
1127 mp->mnt_flag &= ~MNT_LOCAL;
1128 return (error);
1129 }
1130
1131 /*
1132 * Flush out all the files in a filesystem.
1133 */
1134 int
1135 ffs_flushfiles(mp, flags, p)
1136 struct mount *mp;
1137 int flags;
1138 struct proc *p;
1139 {
1140 extern int doforce;
1141 struct ufsmount *ump;
1142 int error;
1143
1144 if (!doforce)
1145 flags &= ~FORCECLOSE;
1146 ump = VFSTOUFS(mp);
1147 #ifdef QUOTA
1148 if (mp->mnt_flag & MNT_QUOTA) {
1149 int i;
1150 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
1151 return (error);
1152 for (i = 0; i < MAXQUOTAS; i++) {
1153 if (ump->um_quotas[i] == NULLVP)
1154 continue;
1155 quotaoff(p, mp, i);
1156 }
1157 /*
1158 * Here we fall through to vflush again to ensure
1159 * that we have gotten rid of all the system vnodes.
1160 */
1161 }
1162 #endif
1163 /*
1164 * Flush all the files.
1165 */
1166 error = vflush(mp, NULLVP, flags);
1167 if (error)
1168 return (error);
1169 /*
1170 * Flush filesystem metadata.
1171 */
1172 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1173 error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
1174 VOP_UNLOCK(ump->um_devvp, 0);
1175 return (error);
1176 }
1177
1178 /*
1179 * Get file system statistics.
1180 */
1181 int
1182 ffs_statfs(mp, sbp, p)
1183 struct mount *mp;
1184 struct statfs *sbp;
1185 struct proc *p;
1186 {
1187 struct ufsmount *ump;
1188 struct fs *fs;
1189
1190 ump = VFSTOUFS(mp);
1191 fs = ump->um_fs;
1192 #ifdef COMPAT_09
1193 sbp->f_type = 1;
1194 #else
1195 sbp->f_type = 0;
1196 #endif
1197 sbp->f_bsize = fs->fs_fsize;
1198 sbp->f_iosize = fs->fs_bsize;
1199 sbp->f_blocks = fs->fs_dsize;
1200 sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1201 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1202 sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
1203 (100 - fs->fs_minfree) / (u_int64_t) 100) -
1204 (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
1205 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1206 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1207 copy_statfs_info(sbp, mp);
1208 return (0);
1209 }
1210
1211 /*
1212 * Go through the disk queues to initiate sandbagged IO;
1213 * go through the inodes to write those that have been modified;
1214 * initiate the writing of the super block if it has been modified.
1215 *
1216 * Note: we are always called with the filesystem marked `MPBUSY'.
1217 */
1218 int
1219 ffs_sync(mp, waitfor, cred, p)
1220 struct mount *mp;
1221 int waitfor;
1222 struct ucred *cred;
1223 struct proc *p;
1224 {
1225 struct vnode *vp, *nvp;
1226 struct inode *ip;
1227 struct ufsmount *ump = VFSTOUFS(mp);
1228 struct fs *fs;
1229 int error, allerror = 0;
1230
1231 fs = ump->um_fs;
1232 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1233 printf("fs = %s\n", fs->fs_fsmnt);
1234 panic("update: rofs mod");
1235 }
1236 /*
1237 * Write back each (modified) inode.
1238 */
1239 simple_lock(&mntvnode_slock);
1240 loop:
1241 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
1242 /*
1243 * If the vnode that we are about to sync is no longer
1244 * associated with this mount point, start over.
1245 */
1246 if (vp->v_mount != mp)
1247 goto loop;
1248 simple_lock(&vp->v_interlock);
1249 nvp = LIST_NEXT(vp, v_mntvnodes);
1250 ip = VTOI(vp);
1251 if (vp->v_type == VNON ||
1252 ((ip->i_flag &
1253 (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
1254 LIST_EMPTY(&vp->v_dirtyblkhd) &&
1255 vp->v_uobj.uo_npages == 0))
1256 {
1257 simple_unlock(&vp->v_interlock);
1258 continue;
1259 }
1260 simple_unlock(&mntvnode_slock);
1261 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1262 if (error) {
1263 simple_lock(&mntvnode_slock);
1264 if (error == ENOENT)
1265 goto loop;
1266 continue;
1267 }
1268 if ((error = VOP_FSYNC(vp, cred,
1269 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
1270 allerror = error;
1271 vput(vp);
1272 simple_lock(&mntvnode_slock);
1273 }
1274 simple_unlock(&mntvnode_slock);
1275 /*
1276 * Force stale file system control information to be flushed.
1277 */
1278 if (waitfor != MNT_LAZY) {
1279 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
1280 waitfor = MNT_NOWAIT;
1281 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1282 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1283 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
1284 allerror = error;
1285 VOP_UNLOCK(ump->um_devvp, 0);
1286 }
1287 #ifdef QUOTA
1288 qsync(mp);
1289 #endif
1290 /*
1291 * Write back modified superblock.
1292 */
1293 if (fs->fs_fmod != 0) {
1294 fs->fs_fmod = 0;
1295 fs->fs_time = time.tv_sec;
1296 if ((error = ffs_cgupdate(ump, waitfor)))
1297 allerror = error;
1298 }
1299 return (allerror);
1300 }
1301
1302 /*
1303 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1304 * in from disk. If it is in core, wait for the lock bit to clear, then
1305 * return the inode locked. Detection and handling of mount points must be
1306 * done by the calling routine.
1307 */
1308 int
1309 ffs_vget(mp, ino, vpp)
1310 struct mount *mp;
1311 ino_t ino;
1312 struct vnode **vpp;
1313 {
1314 struct fs *fs;
1315 struct inode *ip;
1316 struct ufsmount *ump;
1317 struct buf *bp;
1318 struct vnode *vp;
1319 dev_t dev;
1320 int error;
1321
1322 ump = VFSTOUFS(mp);
1323 dev = ump->um_dev;
1324
1325 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1326 return (0);
1327
1328 /* Allocate a new vnode/inode. */
1329 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1330 *vpp = NULL;
1331 return (error);
1332 }
1333
1334 /*
1335 * If someone beat us to it while sleeping in getnewvnode(),
1336 * push back the freshly allocated vnode we don't need, and return.
1337 */
1338
1339 do {
1340 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
1341 ungetnewvnode(vp);
1342 return (0);
1343 }
1344 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1345
1346 /*
1347 * XXX MFS ends up here, too, to allocate an inode. Should we
1348 * XXX create another pool for MFS inodes?
1349 */
1350
1351 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
1352 memset(ip, 0, sizeof(struct inode));
1353 vp->v_data = ip;
1354 ip->i_vnode = vp;
1355 ip->i_ump = ump;
1356 ip->i_fs = fs = ump->um_fs;
1357 ip->i_dev = dev;
1358 ip->i_number = ino;
1359 LIST_INIT(&ip->i_pcbufhd);
1360 #ifdef QUOTA
1361 {
1362 int i;
1363
1364 for (i = 0; i < MAXQUOTAS; i++)
1365 ip->i_dquot[i] = NODQUOT;
1366 }
1367 #endif
1368
1369 /*
1370 * Put it onto its hash chain and lock it so that other requests for
1371 * this inode will block if they arrive while we are sleeping waiting
1372 * for old data structures to be purged or for the contents of the
1373 * disk portion of this inode to be read.
1374 */
1375
1376 ufs_ihashins(ip);
1377 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1378
1379 /* Read in the disk contents for the inode, copy into the inode. */
1380 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1381 (int)fs->fs_bsize, NOCRED, &bp);
1382 if (error) {
1383
1384 /*
1385 * The inode does not contain anything useful, so it would
1386 * be misleading to leave it on its hash chain. With mode
1387 * still zero, it will be unlinked and returned to the free
1388 * list by vput().
1389 */
1390
1391 vput(vp);
1392 brelse(bp);
1393 *vpp = NULL;
1394 return (error);
1395 }
1396 if (ip->i_ump->um_fstype == UFS1)
1397 ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
1398 else
1399 ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
1400 ffs_load_inode(bp, ip, fs, ino);
1401 if (DOINGSOFTDEP(vp))
1402 softdep_load_inodeblock(ip);
1403 else
1404 ip->i_ffs_effnlink = ip->i_nlink;
1405 brelse(bp);
1406
1407 /*
1408 * Initialize the vnode from the inode, check for aliases.
1409 * Note that the underlying vnode may have changed.
1410 */
1411
1412 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1413
1414 /*
1415 * Finish inode initialization now that aliasing has been resolved.
1416 */
1417
1418 genfs_node_init(vp, &ffs_genfsops);
1419 ip->i_devvp = ump->um_devvp;
1420 VREF(ip->i_devvp);
1421
1422 /*
1423 * Ensure that uid and gid are correct. This is a temporary
1424 * fix until fsck has been changed to do the update.
1425 */
1426
1427 if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1428 ip->i_uid = ip->i_ffs1_ouid; /* XXX */
1429 ip->i_gid = ip->i_ffs1_ogid; /* XXX */
1430 } /* XXX */
1431 uvm_vnp_setsize(vp, ip->i_size);
1432 *vpp = vp;
1433 return (0);
1434 }
1435
1436 /*
1437 * File handle to vnode
1438 *
1439 * Have to be really careful about stale file handles:
1440 * - check that the inode number is valid
1441 * - call ffs_vget() to get the locked inode
1442 * - check for an unallocated inode (i_mode == 0)
1443 * - check that the given client host has export rights and return
1444 * those rights via. exflagsp and credanonp
1445 */
1446 int
1447 ffs_fhtovp(mp, fhp, vpp)
1448 struct mount *mp;
1449 struct fid *fhp;
1450 struct vnode **vpp;
1451 {
1452 struct ufid *ufhp;
1453 struct fs *fs;
1454
1455 ufhp = (struct ufid *)fhp;
1456 fs = VFSTOUFS(mp)->um_fs;
1457 if (ufhp->ufid_ino < ROOTINO ||
1458 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1459 return (ESTALE);
1460 return (ufs_fhtovp(mp, ufhp, vpp));
1461 }
1462
1463 /*
1464 * Vnode pointer to File handle
1465 */
1466 /* ARGSUSED */
1467 int
1468 ffs_vptofh(vp, fhp)
1469 struct vnode *vp;
1470 struct fid *fhp;
1471 {
1472 struct inode *ip;
1473 struct ufid *ufhp;
1474
1475 ip = VTOI(vp);
1476 ufhp = (struct ufid *)fhp;
1477 ufhp->ufid_len = sizeof(struct ufid);
1478 ufhp->ufid_ino = ip->i_number;
1479 ufhp->ufid_gen = ip->i_gen;
1480 return (0);
1481 }
1482
1483 void
1484 ffs_init()
1485 {
1486 if (ffs_initcount++ > 0)
1487 return;
1488
1489 softdep_initialize();
1490 ufs_init();
1491
1492 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
1493 &pool_allocator_nointr);
1494 pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
1495 "dino1pl", &pool_allocator_nointr);
1496 pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0,
1497 "dino2pl", &pool_allocator_nointr);
1498 }
1499
1500 void
1501 ffs_reinit()
1502 {
1503 softdep_reinitialize();
1504 ufs_reinit();
1505 }
1506
1507 void
1508 ffs_done()
1509 {
1510 if (--ffs_initcount > 0)
1511 return;
1512
1513 /* XXX softdep cleanup ? */
1514 ufs_done();
1515 pool_destroy(&ffs_inode_pool);
1516 }
1517
1518 int
1519 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1520 int *name;
1521 u_int namelen;
1522 void *oldp;
1523 size_t *oldlenp;
1524 void *newp;
1525 size_t newlen;
1526 struct proc *p;
1527 {
1528 extern int doasyncfree;
1529 extern int ffs_log_changeopt;
1530
1531 /* all sysctl names at this level are terminal */
1532 if (namelen != 1)
1533 return (ENOTDIR); /* overloaded */
1534
1535 switch (name[0]) {
1536 case FFS_ASYNCFREE:
1537 return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
1538 case FFS_LOG_CHANGEOPT:
1539 return (sysctl_int(oldp, oldlenp, newp, newlen,
1540 &ffs_log_changeopt));
1541 default:
1542 return (EOPNOTSUPP);
1543 }
1544 /* NOTREACHED */
1545 }
1546
1547 /*
1548 * Write a superblock and associated information back to disk.
1549 */
1550 int
1551 ffs_sbupdate(mp, waitfor)
1552 struct ufsmount *mp;
1553 int waitfor;
1554 {
1555 struct fs *fs = mp->um_fs;
1556 struct buf *bp;
1557 int error = 0;
1558 u_int32_t saveflag;
1559
1560 bp = getblk(mp->um_devvp,
1561 fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
1562 (int)fs->fs_sbsize, 0, 0);
1563 saveflag = fs->fs_flags & FS_INTERNAL;
1564 fs->fs_flags &= ~FS_INTERNAL;
1565
1566 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1) {
1567 printf("%s: correcting fs_sblockloc from %" PRId64 " to %d\n",
1568 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1569 fs->fs_sblockloc = SBLOCK_UFS1;
1570 }
1571
1572 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2) {
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