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