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