ffs_vfsops.c revision 1.192 1 /* $NetBSD: ffs_vfsops.c,v 1.192 2007/01/07 09:33:18 isaki 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.192 2007/01/07 09:33:18 isaki 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 */
751 for (i = 0; ; i++) {
752 if (bp != NULL) {
753 bp->b_flags |= B_NOCACHE;
754 brelse(bp);
755 bp = NULL;
756 }
757 if (sblock_try[i] == -1) {
758 error = EINVAL;
759 fs = NULL;
760 goto out;
761 }
762 error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
763 &bp);
764 if (error) {
765 fs = NULL;
766 goto out;
767 }
768 fs = (struct fs*)bp->b_data;
769 fsblockloc = sblockloc = sblock_try[i];
770 if (fs->fs_magic == FS_UFS1_MAGIC) {
771 sbsize = fs->fs_sbsize;
772 fstype = UFS1;
773 #ifdef FFS_EI
774 needswap = 0;
775 } else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
776 sbsize = bswap32(fs->fs_sbsize);
777 fstype = UFS1;
778 needswap = 1;
779 #endif
780 } else if (fs->fs_magic == FS_UFS2_MAGIC) {
781 sbsize = fs->fs_sbsize;
782 fstype = UFS2;
783 #ifdef FFS_EI
784 needswap = 0;
785 } else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
786 sbsize = bswap32(fs->fs_sbsize);
787 fstype = UFS2;
788 needswap = 1;
789 #endif
790 } else
791 continue;
792
793
794 /* fs->fs_sblockloc isn't defined for old filesystems */
795 if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
796 if (sblockloc == SBLOCK_UFS2)
797 /*
798 * This is likely to be the first alternate
799 * in a filesystem with 64k blocks.
800 * Don't use it.
801 */
802 continue;
803 fsblockloc = sblockloc;
804 } else {
805 fsblockloc = fs->fs_sblockloc;
806 #ifdef FFS_EI
807 if (needswap)
808 fsblockloc = bswap64(fsblockloc);
809 #endif
810 }
811
812 /* Check we haven't found an alternate superblock */
813 if (fsblockloc != sblockloc)
814 continue;
815
816 /* Validate size of superblock */
817 if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
818 continue;
819
820 /* Ok seems to be a good superblock */
821 break;
822 }
823
824 fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
825 memcpy(fs, bp->b_data, sbsize);
826
827 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
828 memset(ump, 0, sizeof *ump);
829 TAILQ_INIT(&ump->um_snapshots);
830 ump->um_fs = fs;
831 ump->um_ops = &ffs_ufsops;
832
833 #ifdef FFS_EI
834 if (needswap) {
835 ffs_sb_swap((struct fs*)bp->b_data, fs);
836 fs->fs_flags |= FS_SWAPPED;
837 } else
838 #endif
839 fs->fs_flags &= ~FS_SWAPPED;
840
841 ffs_oldfscompat_read(fs, ump, sblockloc);
842 ump->um_maxfilesize = fs->fs_maxfilesize;
843
844 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
845 fs->fs_pendingblocks = 0;
846 fs->fs_pendinginodes = 0;
847 }
848
849 ump->um_fstype = fstype;
850 if (fs->fs_sbsize < SBLOCKSIZE)
851 bp->b_flags |= B_INVAL;
852 brelse(bp);
853 bp = NULL;
854
855 /* First check to see if this is tagged as an Apple UFS filesystem
856 * in the disklabel
857 */
858 if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) == 0) &&
859 (dpart.part->p_fstype == FS_APPLEUFS)) {
860 ump->um_flags |= UFS_ISAPPLEUFS;
861 }
862 #ifdef APPLE_UFS
863 else {
864 /* Manually look for an apple ufs label, and if a valid one
865 * is found, then treat it like an Apple UFS filesystem anyway
866 */
867 error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
868 APPLEUFS_LABEL_SIZE, cred, &bp);
869 if (error)
870 goto out;
871 error = ffs_appleufs_validate(fs->fs_fsmnt,
872 (struct appleufslabel *)bp->b_data,NULL);
873 if (error == 0) {
874 ump->um_flags |= UFS_ISAPPLEUFS;
875 }
876 brelse(bp);
877 bp = NULL;
878 }
879 #else
880 if (ump->um_flags & UFS_ISAPPLEUFS) {
881 error = EINVAL;
882 goto out;
883 }
884 #endif
885
886 /*
887 * verify that we can access the last block in the fs
888 * if we're mounting read/write.
889 */
890
891 if (!ronly) {
892 error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
893 cred, &bp);
894 if (bp->b_bcount != fs->fs_fsize)
895 error = EINVAL;
896 bp->b_flags |= B_INVAL;
897 if (error)
898 goto out;
899 brelse(bp);
900 bp = NULL;
901 }
902
903 fs->fs_ronly = ronly;
904 if (ronly == 0) {
905 fs->fs_clean <<= 1;
906 fs->fs_fmod = 1;
907 }
908 size = fs->fs_cssize;
909 blks = howmany(size, fs->fs_fsize);
910 if (fs->fs_contigsumsize > 0)
911 size += fs->fs_ncg * sizeof(int32_t);
912 size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
913 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
914 fs->fs_csp = space;
915 for (i = 0; i < blks; i += fs->fs_frag) {
916 size = fs->fs_bsize;
917 if (i + fs->fs_frag > blks)
918 size = (blks - i) * fs->fs_fsize;
919 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
920 cred, &bp);
921 if (error) {
922 free(fs->fs_csp, M_UFSMNT);
923 goto out;
924 }
925 #ifdef FFS_EI
926 if (needswap)
927 ffs_csum_swap((struct csum *)bp->b_data,
928 (struct csum *)space, size);
929 else
930 #endif
931 memcpy(space, bp->b_data, (u_int)size);
932
933 space = (char *)space + size;
934 brelse(bp);
935 bp = NULL;
936 }
937 if (fs->fs_contigsumsize > 0) {
938 fs->fs_maxcluster = lp = space;
939 for (i = 0; i < fs->fs_ncg; i++)
940 *lp++ = fs->fs_contigsumsize;
941 space = lp;
942 }
943 size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
944 fs->fs_contigdirs = space;
945 space = (char *)space + size;
946 memset(fs->fs_contigdirs, 0, size);
947 /* Compatibility for old filesystems - XXX */
948 if (fs->fs_avgfilesize <= 0)
949 fs->fs_avgfilesize = AVFILESIZ;
950 if (fs->fs_avgfpdir <= 0)
951 fs->fs_avgfpdir = AFPDIR;
952 fs->fs_active = NULL;
953 mp->mnt_data = ump;
954 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
955 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
956 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
957 mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
958 if (UFS_MPISAPPLEUFS(ump)) {
959 /* NeXT used to keep short symlinks in the inode even
960 * when using FS_42INODEFMT. In that case fs->fs_maxsymlinklen
961 * is probably -1, but we still need to be able to identify
962 * short symlinks.
963 */
964 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
965 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
966 mp->mnt_iflag |= IMNT_DTYPE;
967 } else {
968 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
969 ump->um_dirblksiz = DIRBLKSIZ;
970 if (ump->um_maxsymlinklen > 0)
971 mp->mnt_iflag |= IMNT_DTYPE;
972 else
973 mp->mnt_iflag &= ~IMNT_DTYPE;
974 }
975 mp->mnt_fs_bshift = fs->fs_bshift;
976 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
977 mp->mnt_flag |= MNT_LOCAL;
978 #ifdef FFS_EI
979 if (needswap)
980 ump->um_flags |= UFS_NEEDSWAP;
981 #endif
982 ump->um_mountp = mp;
983 ump->um_dev = dev;
984 ump->um_devvp = devvp;
985 ump->um_nindir = fs->fs_nindir;
986 ump->um_lognindir = ffs(fs->fs_nindir) - 1;
987 ump->um_bptrtodb = fs->fs_fsbtodb;
988 ump->um_seqinc = fs->fs_frag;
989 for (i = 0; i < MAXQUOTAS; i++)
990 ump->um_quotas[i] = NULLVP;
991 devvp->v_specmountpoint = mp;
992 if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
993 error = softdep_mount(devvp, mp, fs, cred);
994 if (error) {
995 free(fs->fs_csp, M_UFSMNT);
996 goto out;
997 }
998 }
999 if (ronly == 0 && fs->fs_snapinum[0] != 0)
1000 ffs_snapshot_mount(mp);
1001 #ifdef UFS_EXTATTR
1002 /*
1003 * Initialize file-backed extended attributes on UFS1 file
1004 * systems.
1005 */
1006 if (ump->um_fstype == UFS1) {
1007 ufs_extattr_uepm_init(&ump->um_extattr);
1008 #ifdef UFS_EXTATTR_AUTOSTART
1009 /*
1010 * XXX Just ignore errors. Not clear that we should
1011 * XXX fail the mount in this case.
1012 */
1013 (void) ufs_extattr_autostart(mp, l);
1014 #endif
1015 }
1016 #endif /* UFS_EXTATTR */
1017 return (0);
1018 out:
1019 if (fs)
1020 free(fs, M_UFSMNT);
1021 devvp->v_specmountpoint = NULL;
1022 if (bp)
1023 brelse(bp);
1024 if (ump) {
1025 if (ump->um_oldfscompat)
1026 free(ump->um_oldfscompat, M_UFSMNT);
1027 free(ump, M_UFSMNT);
1028 mp->mnt_data = NULL;
1029 }
1030 return (error);
1031 }
1032
1033 /*
1034 * Sanity checks for loading old filesystem superblocks.
1035 * See ffs_oldfscompat_write below for unwound actions.
1036 *
1037 * XXX - Parts get retired eventually.
1038 * Unfortunately new bits get added.
1039 */
1040 static void
1041 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
1042 {
1043 off_t maxfilesize;
1044 int32_t *extrasave;
1045
1046 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1047 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1048 return;
1049
1050 if (!ump->um_oldfscompat)
1051 ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
1052 M_UFSMNT, M_WAITOK);
1053
1054 memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
1055 extrasave = ump->um_oldfscompat;
1056 extrasave += 512/sizeof(int32_t);
1057 extrasave[0] = fs->fs_old_npsect;
1058 extrasave[1] = fs->fs_old_interleave;
1059 extrasave[2] = fs->fs_old_trackskew;
1060
1061 /* These fields will be overwritten by their
1062 * original values in fs_oldfscompat_write, so it is harmless
1063 * to modify them here.
1064 */
1065 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1066 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1067 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1068 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1069
1070 fs->fs_maxbsize = fs->fs_bsize;
1071 fs->fs_time = fs->fs_old_time;
1072 fs->fs_size = fs->fs_old_size;
1073 fs->fs_dsize = fs->fs_old_dsize;
1074 fs->fs_csaddr = fs->fs_old_csaddr;
1075 fs->fs_sblockloc = sblockloc;
1076
1077 fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
1078
1079 if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
1080 fs->fs_old_nrpos = 8;
1081 fs->fs_old_npsect = fs->fs_old_nsect;
1082 fs->fs_old_interleave = 1;
1083 fs->fs_old_trackskew = 0;
1084 }
1085
1086 if (fs->fs_old_inodefmt < FS_44INODEFMT) {
1087 ump->um_maxfilesize = (u_quad_t) 1LL << 39;
1088 fs->fs_qbmask = ~fs->fs_bmask;
1089 fs->fs_qfmask = ~fs->fs_fmask;
1090 }
1091
1092 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1093 if (ump->um_maxfilesize > maxfilesize)
1094 ump->um_maxfilesize = maxfilesize;
1095
1096 /* Compatibility for old filesystems */
1097 if (fs->fs_avgfilesize <= 0)
1098 fs->fs_avgfilesize = AVFILESIZ;
1099 if (fs->fs_avgfpdir <= 0)
1100 fs->fs_avgfpdir = AFPDIR;
1101
1102 #if 0
1103 if (bigcgs) {
1104 fs->fs_save_cgsize = fs->fs_cgsize;
1105 fs->fs_cgsize = fs->fs_bsize;
1106 }
1107 #endif
1108 }
1109
1110 /*
1111 * Unwinding superblock updates for old filesystems.
1112 * See ffs_oldfscompat_read above for details.
1113 *
1114 * XXX - Parts get retired eventually.
1115 * Unfortunately new bits get added.
1116 */
1117 static void
1118 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
1119 {
1120 int32_t *extrasave;
1121
1122 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1123 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1124 return;
1125
1126 fs->fs_old_time = fs->fs_time;
1127 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1128 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1129 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1130 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1131 fs->fs_old_flags = fs->fs_flags;
1132
1133 #if 0
1134 if (bigcgs) {
1135 fs->fs_cgsize = fs->fs_save_cgsize;
1136 }
1137 #endif
1138
1139 memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
1140 extrasave = ump->um_oldfscompat;
1141 extrasave += 512/sizeof(int32_t);
1142 fs->fs_old_npsect = extrasave[0];
1143 fs->fs_old_interleave = extrasave[1];
1144 fs->fs_old_trackskew = extrasave[2];
1145
1146 }
1147
1148 /*
1149 * unmount system call
1150 */
1151 int
1152 ffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
1153 {
1154 struct ufsmount *ump = VFSTOUFS(mp);
1155 struct fs *fs = ump->um_fs;
1156 int error, flags, penderr;
1157
1158 penderr = 0;
1159 flags = 0;
1160 if (mntflags & MNT_FORCE)
1161 flags |= FORCECLOSE;
1162 #ifdef UFS_EXTATTR
1163 if (ump->um_fstype == UFS1) {
1164 error = ufs_extattr_stop(mp, l);
1165 if (error) {
1166 if (error != EOPNOTSUPP)
1167 printf("%s: ufs_extattr_stop returned %d\n",
1168 fs->fs_fsmnt, error);
1169 } else
1170 ufs_extattr_uepm_destroy(&ump->um_extattr);
1171 }
1172 #endif /* UFS_EXTATTR */
1173 if (mp->mnt_flag & MNT_SOFTDEP) {
1174 if ((error = softdep_flushfiles(mp, flags, l)) != 0)
1175 return (error);
1176 } else {
1177 if ((error = ffs_flushfiles(mp, flags, l)) != 0)
1178 return (error);
1179 }
1180 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1181 printf("%s: unmount pending error: blocks %" PRId64
1182 " files %d\n",
1183 fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
1184 fs->fs_pendingblocks = 0;
1185 fs->fs_pendinginodes = 0;
1186 penderr = 1;
1187 }
1188 if (fs->fs_ronly == 0 &&
1189 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1190 fs->fs_clean & FS_WASCLEAN) {
1191 /*
1192 * XXXX don't mark fs clean in the case of softdep
1193 * pending block errors, until they are fixed.
1194 */
1195 if (penderr == 0) {
1196 if (mp->mnt_flag & MNT_SOFTDEP)
1197 fs->fs_flags &= ~FS_DOSOFTDEP;
1198 fs->fs_clean = FS_ISCLEAN;
1199 }
1200 fs->fs_fmod = 0;
1201 (void) ffs_sbupdate(ump, MNT_WAIT);
1202 }
1203 if (ump->um_devvp->v_type != VBAD)
1204 ump->um_devvp->v_specmountpoint = NULL;
1205 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1206 (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
1207 NOCRED, l);
1208 vput(ump->um_devvp);
1209 free(fs->fs_csp, M_UFSMNT);
1210 free(fs, M_UFSMNT);
1211 if (ump->um_oldfscompat != NULL)
1212 free(ump->um_oldfscompat, M_UFSMNT);
1213 free(ump, M_UFSMNT);
1214 mp->mnt_data = NULL;
1215 mp->mnt_flag &= ~MNT_LOCAL;
1216 return (0);
1217 }
1218
1219 /*
1220 * Flush out all the files in a filesystem.
1221 */
1222 int
1223 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
1224 {
1225 extern int doforce;
1226 struct ufsmount *ump;
1227 int error;
1228
1229 if (!doforce)
1230 flags &= ~FORCECLOSE;
1231 ump = VFSTOUFS(mp);
1232 #ifdef QUOTA
1233 if (mp->mnt_flag & MNT_QUOTA) {
1234 int i;
1235 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
1236 return (error);
1237 for (i = 0; i < MAXQUOTAS; i++) {
1238 if (ump->um_quotas[i] == NULLVP)
1239 continue;
1240 quotaoff(l, mp, i);
1241 }
1242 /*
1243 * Here we fall through to vflush again to ensure
1244 * that we have gotten rid of all the system vnodes.
1245 */
1246 }
1247 #endif
1248 if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
1249 return (error);
1250 ffs_snapshot_unmount(mp);
1251 /*
1252 * Flush all the files.
1253 */
1254 error = vflush(mp, NULLVP, flags);
1255 if (error)
1256 return (error);
1257 /*
1258 * Flush filesystem metadata.
1259 */
1260 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1261 error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0, l);
1262 VOP_UNLOCK(ump->um_devvp, 0);
1263 return (error);
1264 }
1265
1266 /*
1267 * Get file system statistics.
1268 */
1269 int
1270 ffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
1271 {
1272 struct ufsmount *ump;
1273 struct fs *fs;
1274
1275 ump = VFSTOUFS(mp);
1276 fs = ump->um_fs;
1277 sbp->f_bsize = fs->fs_bsize;
1278 sbp->f_frsize = fs->fs_fsize;
1279 sbp->f_iosize = fs->fs_bsize;
1280 sbp->f_blocks = fs->fs_dsize;
1281 sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1282 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1283 sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
1284 fs->fs_minfree) / (u_int64_t) 100;
1285 if (sbp->f_bfree > sbp->f_bresvd)
1286 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1287 else
1288 sbp->f_bavail = 0;
1289 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1290 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1291 sbp->f_favail = sbp->f_ffree;
1292 sbp->f_fresvd = 0;
1293 copy_statvfs_info(sbp, mp);
1294 return (0);
1295 }
1296
1297 /*
1298 * Go through the disk queues to initiate sandbagged IO;
1299 * go through the inodes to write those that have been modified;
1300 * initiate the writing of the super block if it has been modified.
1301 *
1302 * Note: we are always called with the filesystem marked `MPBUSY'.
1303 */
1304 int
1305 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred, struct lwp *l)
1306 {
1307 struct vnode *vp, *nvp;
1308 struct inode *ip;
1309 struct ufsmount *ump = VFSTOUFS(mp);
1310 struct fs *fs;
1311 int error, count, allerror = 0;
1312
1313 fs = ump->um_fs;
1314 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1315 printf("fs = %s\n", fs->fs_fsmnt);
1316 panic("update: rofs mod");
1317 }
1318 /*
1319 * Write back each (modified) inode.
1320 */
1321 simple_lock(&mntvnode_slock);
1322 loop:
1323 /*
1324 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
1325 * and vclean() can be called indirectly
1326 */
1327 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
1328 /*
1329 * If the vnode that we are about to sync is no longer
1330 * associated with this mount point, start over.
1331 */
1332 if (vp->v_mount != mp)
1333 goto loop;
1334 simple_lock(&vp->v_interlock);
1335 nvp = TAILQ_NEXT(vp, v_mntvnodes);
1336 ip = VTOI(vp);
1337 if (vp->v_type == VNON ||
1338 ((ip->i_flag &
1339 (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
1340 LIST_EMPTY(&vp->v_dirtyblkhd) &&
1341 vp->v_uobj.uo_npages == 0))
1342 {
1343 simple_unlock(&vp->v_interlock);
1344 continue;
1345 }
1346 simple_unlock(&mntvnode_slock);
1347 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1348 if (error) {
1349 simple_lock(&mntvnode_slock);
1350 if (error == ENOENT)
1351 goto loop;
1352 continue;
1353 }
1354 if (vp->v_type == VREG && waitfor == MNT_LAZY)
1355 error = ffs_update(vp, NULL, NULL, 0);
1356 else
1357 error = VOP_FSYNC(vp, cred,
1358 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l);
1359 if (error)
1360 allerror = error;
1361 vput(vp);
1362 simple_lock(&mntvnode_slock);
1363 }
1364 simple_unlock(&mntvnode_slock);
1365 /*
1366 * Force stale file system control information to be flushed.
1367 */
1368 if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
1369 if ((error = softdep_flushworklist(ump->um_mountp, &count, l)))
1370 allerror = error;
1371 /* Flushed work items may create new vnodes to clean */
1372 if (allerror == 0 && count) {
1373 simple_lock(&mntvnode_slock);
1374 goto loop;
1375 }
1376 }
1377 if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
1378 !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
1379 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1380 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1381 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l)) != 0)
1382 allerror = error;
1383 VOP_UNLOCK(ump->um_devvp, 0);
1384 if (allerror == 0 && waitfor == MNT_WAIT) {
1385 simple_lock(&mntvnode_slock);
1386 goto loop;
1387 }
1388 }
1389 #ifdef QUOTA
1390 qsync(mp);
1391 #endif
1392 /*
1393 * Write back modified superblock.
1394 */
1395 if (fs->fs_fmod != 0) {
1396 fs->fs_fmod = 0;
1397 fs->fs_time = time_second;
1398 if ((error = ffs_cgupdate(ump, waitfor)))
1399 allerror = error;
1400 }
1401 return (allerror);
1402 }
1403
1404 /*
1405 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1406 * in from disk. If it is in core, wait for the lock bit to clear, then
1407 * return the inode locked. Detection and handling of mount points must be
1408 * done by the calling routine.
1409 */
1410 int
1411 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1412 {
1413 struct fs *fs;
1414 struct inode *ip;
1415 struct ufsmount *ump;
1416 struct buf *bp;
1417 struct vnode *vp;
1418 dev_t dev;
1419 int error;
1420
1421 ump = VFSTOUFS(mp);
1422 dev = ump->um_dev;
1423
1424 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1425 return (0);
1426
1427 /* Allocate a new vnode/inode. */
1428 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1429 *vpp = NULL;
1430 return (error);
1431 }
1432
1433 /*
1434 * If someone beat us to it while sleeping in getnewvnode(),
1435 * push back the freshly allocated vnode we don't need, and return.
1436 */
1437
1438 do {
1439 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
1440 ungetnewvnode(vp);
1441 return (0);
1442 }
1443 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1444
1445 vp->v_flag |= VLOCKSWORK;
1446
1447 /*
1448 * XXX MFS ends up here, too, to allocate an inode. Should we
1449 * XXX create another pool for MFS inodes?
1450 */
1451
1452 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
1453 memset(ip, 0, sizeof(struct inode));
1454 vp->v_data = ip;
1455 ip->i_vnode = vp;
1456 ip->i_ump = ump;
1457 ip->i_fs = fs = ump->um_fs;
1458 ip->i_dev = dev;
1459 ip->i_number = ino;
1460 LIST_INIT(&ip->i_pcbufhd);
1461 #ifdef QUOTA
1462 {
1463 int i;
1464
1465 for (i = 0; i < MAXQUOTAS; i++)
1466 ip->i_dquot[i] = NODQUOT;
1467 }
1468 #endif
1469
1470 /*
1471 * Put it onto its hash chain and lock it so that other requests for
1472 * this inode will block if they arrive while we are sleeping waiting
1473 * for old data structures to be purged or for the contents of the
1474 * disk portion of this inode to be read.
1475 */
1476
1477 ufs_ihashins(ip);
1478 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1479
1480 /* Read in the disk contents for the inode, copy into the inode. */
1481 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1482 (int)fs->fs_bsize, NOCRED, &bp);
1483 if (error) {
1484
1485 /*
1486 * The inode does not contain anything useful, so it would
1487 * be misleading to leave it on its hash chain. With mode
1488 * still zero, it will be unlinked and returned to the free
1489 * list by vput().
1490 */
1491
1492 vput(vp);
1493 brelse(bp);
1494 *vpp = NULL;
1495 return (error);
1496 }
1497 if (ip->i_ump->um_fstype == UFS1)
1498 ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
1499 else
1500 ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
1501 ffs_load_inode(bp, ip, fs, ino);
1502 if (DOINGSOFTDEP(vp))
1503 softdep_load_inodeblock(ip);
1504 else
1505 ip->i_ffs_effnlink = ip->i_nlink;
1506 brelse(bp);
1507
1508 /*
1509 * Initialize the vnode from the inode, check for aliases.
1510 * Note that the underlying vnode may have changed.
1511 */
1512
1513 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1514
1515 /*
1516 * Finish inode initialization now that aliasing has been resolved.
1517 */
1518
1519 genfs_node_init(vp, &ffs_genfsops);
1520 ip->i_devvp = ump->um_devvp;
1521 VREF(ip->i_devvp);
1522
1523 /*
1524 * Ensure that uid and gid are correct. This is a temporary
1525 * fix until fsck has been changed to do the update.
1526 */
1527
1528 if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1529 ip->i_uid = ip->i_ffs1_ouid; /* XXX */
1530 ip->i_gid = ip->i_ffs1_ogid; /* XXX */
1531 } /* XXX */
1532 uvm_vnp_setsize(vp, ip->i_size);
1533 *vpp = vp;
1534 return (0);
1535 }
1536
1537 /*
1538 * File handle to vnode
1539 *
1540 * Have to be really careful about stale file handles:
1541 * - check that the inode number is valid
1542 * - call ffs_vget() to get the locked inode
1543 * - check for an unallocated inode (i_mode == 0)
1544 * - check that the given client host has export rights and return
1545 * those rights via. exflagsp and credanonp
1546 */
1547 int
1548 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1549 {
1550 struct ufid ufh;
1551 struct fs *fs;
1552
1553 if (fhp->fid_len != sizeof(struct ufid))
1554 return EINVAL;
1555
1556 memcpy(&ufh, fhp, sizeof(ufh));
1557 fs = VFSTOUFS(mp)->um_fs;
1558 if (ufh.ufid_ino < ROOTINO ||
1559 ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1560 return (ESTALE);
1561 return (ufs_fhtovp(mp, &ufh, vpp));
1562 }
1563
1564 /*
1565 * Vnode pointer to File handle
1566 */
1567 /* ARGSUSED */
1568 int
1569 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1570 {
1571 struct inode *ip;
1572 struct ufid ufh;
1573
1574 if (*fh_size < sizeof(struct ufid)) {
1575 *fh_size = sizeof(struct ufid);
1576 return E2BIG;
1577 }
1578 ip = VTOI(vp);
1579 *fh_size = sizeof(struct ufid);
1580 memset(&ufh, 0, sizeof(ufh));
1581 ufh.ufid_len = sizeof(struct ufid);
1582 ufh.ufid_ino = ip->i_number;
1583 ufh.ufid_gen = ip->i_gen;
1584 memcpy(fhp, &ufh, sizeof(ufh));
1585 return (0);
1586 }
1587
1588 void
1589 ffs_init(void)
1590 {
1591 if (ffs_initcount++ > 0)
1592 return;
1593
1594 #ifdef _LKM
1595 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0,
1596 "ffsinopl", &pool_allocator_nointr);
1597 pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
1598 "dino1pl", &pool_allocator_nointr);
1599 pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0,
1600 "dino2pl", &pool_allocator_nointr);
1601 #endif
1602 softdep_initialize();
1603 ufs_init();
1604 }
1605
1606 void
1607 ffs_reinit(void)
1608 {
1609 softdep_reinitialize();
1610 ufs_reinit();
1611 }
1612
1613 void
1614 ffs_done(void)
1615 {
1616 if (--ffs_initcount > 0)
1617 return;
1618
1619 /* XXX softdep cleanup ? */
1620 ufs_done();
1621 #ifdef _LKM
1622 pool_destroy(&ffs_dinode2_pool);
1623 pool_destroy(&ffs_dinode1_pool);
1624 pool_destroy(&ffs_inode_pool);
1625 #endif
1626 }
1627
1628 SYSCTL_SETUP(sysctl_vfs_ffs_setup, "sysctl vfs.ffs subtree setup")
1629 {
1630 #if 0
1631 extern int doasyncfree;
1632 #endif
1633 extern int ffs_log_changeopt;
1634
1635 sysctl_createv(clog, 0, NULL, NULL,
1636 CTLFLAG_PERMANENT,
1637 CTLTYPE_NODE, "vfs", NULL,
1638 NULL, 0, NULL, 0,
1639 CTL_VFS, CTL_EOL);
1640 sysctl_createv(clog, 0, NULL, NULL,
1641 CTLFLAG_PERMANENT,
1642 CTLTYPE_NODE, "ffs",
1643 SYSCTL_DESCR("Berkeley Fast File System"),
1644 NULL, 0, NULL, 0,
1645 CTL_VFS, 1, CTL_EOL);
1646
1647 /*
1648 * @@@ should we even bother with these first three?
1649 */
1650 sysctl_createv(clog, 0, NULL, NULL,
1651 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1652 CTLTYPE_INT, "doclusterread", NULL,
1653 sysctl_notavail, 0, NULL, 0,
1654 CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
1655 sysctl_createv(clog, 0, NULL, NULL,
1656 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1657 CTLTYPE_INT, "doclusterwrite", NULL,
1658 sysctl_notavail, 0, NULL, 0,
1659 CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
1660 sysctl_createv(clog, 0, NULL, NULL,
1661 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1662 CTLTYPE_INT, "doreallocblks", NULL,
1663 sysctl_notavail, 0, NULL, 0,
1664 CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
1665 #if 0
1666 sysctl_createv(clog, 0, NULL, NULL,
1667 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1668 CTLTYPE_INT, "doasyncfree",
1669 SYSCTL_DESCR("Release dirty blocks asynchronously"),
1670 NULL, 0, &doasyncfree, 0,
1671 CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
1672 #endif
1673 sysctl_createv(clog, 0, NULL, NULL,
1674 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1675 CTLTYPE_INT, "log_changeopt",
1676 SYSCTL_DESCR("Log changes in optimization strategy"),
1677 NULL, 0, &ffs_log_changeopt, 0,
1678 CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
1679 }
1680
1681 /*
1682 * Write a superblock and associated information back to disk.
1683 */
1684 int
1685 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1686 {
1687 struct fs *fs = mp->um_fs;
1688 struct buf *bp;
1689 int error = 0;
1690 u_int32_t saveflag;
1691
1692 bp = getblk(mp->um_devvp,
1693 fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
1694 (int)fs->fs_sbsize, 0, 0);
1695 saveflag = fs->fs_flags & FS_INTERNAL;
1696 fs->fs_flags &= ~FS_INTERNAL;
1697
1698 memcpy(bp->b_data, fs, fs->fs_sbsize);
1699
1700 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1701 #ifdef FFS_EI
1702 if (mp->um_flags & UFS_NEEDSWAP)
1703 ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
1704 #endif
1705 fs->fs_flags |= saveflag;
1706
1707 if (waitfor == MNT_WAIT)
1708 error = bwrite(bp);
1709 else
1710 bawrite(bp);
1711 return (error);
1712 }
1713
1714 int
1715 ffs_cgupdate(struct ufsmount *mp, int waitfor)
1716 {
1717 struct fs *fs = mp->um_fs;
1718 struct buf *bp;
1719 int blks;
1720 void *space;
1721 int i, size, error = 0, allerror = 0;
1722
1723 allerror = ffs_sbupdate(mp, waitfor);
1724 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1725 space = fs->fs_csp;
1726 for (i = 0; i < blks; i += fs->fs_frag) {
1727 size = fs->fs_bsize;
1728 if (i + fs->fs_frag > blks)
1729 size = (blks - i) * fs->fs_fsize;
1730 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1731 size, 0, 0);
1732 #ifdef FFS_EI
1733 if (mp->um_flags & UFS_NEEDSWAP)
1734 ffs_csum_swap((struct csum*)space,
1735 (struct csum*)bp->b_data, size);
1736 else
1737 #endif
1738 memcpy(bp->b_data, space, (u_int)size);
1739 space = (char *)space + size;
1740 if (waitfor == MNT_WAIT)
1741 error = bwrite(bp);
1742 else
1743 bawrite(bp);
1744 }
1745 if (!allerror && error)
1746 allerror = error;
1747 return (allerror);
1748 }
1749
1750 int
1751 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
1752 int attrnamespace, const char *attrname, struct lwp *l)
1753 {
1754 #ifdef UFS_EXTATTR
1755 /*
1756 * File-backed extended attributes are only supported on UFS1.
1757 * UFS2 has native extended attributes.
1758 */
1759 if (VFSTOUFS(mp)->um_fstype == UFS1)
1760 return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname,
1761 l));
1762 #endif
1763 return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname, l));
1764 }
1765