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