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