ffs_vfsops.c revision 1.208 1 /* $NetBSD: ffs_vfsops.c,v 1.208 2007/08/09 07:34:28 hannken Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.208 2007/08/09 07:34:28 hannken Exp $");
36
37 #if defined(_KERNEL_OPT)
38 #include "opt_ffs.h"
39 #include "opt_quota.h"
40 #include "opt_softdep.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/proc.h>
47 #include <sys/kernel.h>
48 #include <sys/vnode.h>
49 #include <sys/socket.h>
50 #include <sys/mount.h>
51 #include <sys/buf.h>
52 #include <sys/device.h>
53 #include <sys/mbuf.h>
54 #include <sys/file.h>
55 #include <sys/disklabel.h>
56 #include <sys/ioctl.h>
57 #include <sys/errno.h>
58 #include <sys/malloc.h>
59 #include <sys/pool.h>
60 #include <sys/lock.h>
61 #include <sys/sysctl.h>
62 #include <sys/conf.h>
63 #include <sys/kauth.h>
64 #include <sys/fstrans.h>
65
66 #include <miscfs/specfs/specdev.h>
67
68 #include <ufs/ufs/quota.h>
69 #include <ufs/ufs/ufsmount.h>
70 #include <ufs/ufs/inode.h>
71 #include <ufs/ufs/dir.h>
72 #include <ufs/ufs/ufs_extern.h>
73 #include <ufs/ufs/ufs_bswap.h>
74
75 #include <ufs/ffs/fs.h>
76 #include <ufs/ffs/ffs_extern.h>
77
78 /* how many times ffs_init() was called */
79 int ffs_initcount = 0;
80
81 extern kmutex_t ufs_hashlock;
82
83 extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
84 extern const struct vnodeopv_desc ffs_specop_opv_desc;
85 extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
86
87 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
88 &ffs_vnodeop_opv_desc,
89 &ffs_specop_opv_desc,
90 &ffs_fifoop_opv_desc,
91 NULL,
92 };
93
94 struct vfsops ffs_vfsops = {
95 MOUNT_FFS,
96 sizeof (struct ufs_args),
97 ffs_mount,
98 ufs_start,
99 ffs_unmount,
100 ufs_root,
101 ufs_quotactl,
102 ffs_statvfs,
103 ffs_sync,
104 ffs_vget,
105 ffs_fhtovp,
106 ffs_vptofh,
107 ffs_init,
108 ffs_reinit,
109 ffs_done,
110 ffs_mountroot,
111 ffs_snapshot,
112 ffs_extattrctl,
113 ffs_suspendctl,
114 ffs_vnodeopv_descs,
115 0,
116 { NULL, NULL },
117 };
118 VFS_ATTACH(ffs_vfsops);
119
120 static const struct genfs_ops ffs_genfsops = {
121 .gop_size = ffs_gop_size,
122 .gop_alloc = ufs_gop_alloc,
123 .gop_write = genfs_gop_write,
124 .gop_markupdate = ufs_gop_markupdate,
125 };
126
127 static const struct ufs_ops ffs_ufsops = {
128 .uo_itimes = ffs_itimes,
129 .uo_update = ffs_update,
130 .uo_truncate = ffs_truncate,
131 .uo_valloc = ffs_valloc,
132 .uo_vfree = ffs_vfree,
133 .uo_balloc = ffs_balloc,
134 };
135
136 struct pool ffs_inode_pool;
137 struct pool ffs_dinode1_pool;
138 struct pool ffs_dinode2_pool;
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, size_t *data_len,
189 struct lwp *l)
190 {
191 struct nameidata nd;
192 struct vnode *devvp = NULL;
193 struct ufs_args *args = data;
194 struct ufsmount *ump = NULL;
195 struct fs *fs;
196 int error = 0, flags, update;
197 mode_t accessmode;
198
199 if (*data_len < sizeof *args)
200 return EINVAL;
201
202 if (mp->mnt_flag & MNT_GETARGS) {
203 ump = VFSTOUFS(mp);
204 if (ump == NULL)
205 return EIO;
206 args->fspec = NULL;
207 *data_len = sizeof *args;
208 return 0;
209 }
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(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec, l);
223 if ((error = namei(&nd)) != 0)
224 return (error);
225 devvp = nd.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->mnt_op->vfs_name, 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 ump->um_fs = fs;
826 ump->um_ops = &ffs_ufsops;
827
828 #ifdef FFS_EI
829 if (needswap) {
830 ffs_sb_swap((struct fs*)bp->b_data, fs);
831 fs->fs_flags |= FS_SWAPPED;
832 } else
833 #endif
834 fs->fs_flags &= ~FS_SWAPPED;
835
836 ffs_oldfscompat_read(fs, ump, sblockloc);
837 ump->um_maxfilesize = fs->fs_maxfilesize;
838
839 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
840 fs->fs_pendingblocks = 0;
841 fs->fs_pendinginodes = 0;
842 }
843
844 ump->um_fstype = fstype;
845 if (fs->fs_sbsize < SBLOCKSIZE)
846 bp->b_flags |= B_INVAL;
847 brelse(bp);
848 bp = NULL;
849
850 /* First check to see if this is tagged as an Apple UFS filesystem
851 * in the disklabel
852 */
853 if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) == 0) &&
854 (dpart.part->p_fstype == FS_APPLEUFS)) {
855 ump->um_flags |= UFS_ISAPPLEUFS;
856 }
857 #ifdef APPLE_UFS
858 else {
859 /* Manually look for an apple ufs label, and if a valid one
860 * is found, then treat it like an Apple UFS filesystem anyway
861 */
862 error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
863 APPLEUFS_LABEL_SIZE, cred, &bp);
864 if (error)
865 goto out;
866 error = ffs_appleufs_validate(fs->fs_fsmnt,
867 (struct appleufslabel *)bp->b_data,NULL);
868 if (error == 0) {
869 ump->um_flags |= UFS_ISAPPLEUFS;
870 }
871 brelse(bp);
872 bp = NULL;
873 }
874 #else
875 if (ump->um_flags & UFS_ISAPPLEUFS) {
876 error = EINVAL;
877 goto out;
878 }
879 #endif
880
881 /*
882 * verify that we can access the last block in the fs
883 * if we're mounting read/write.
884 */
885
886 if (!ronly) {
887 error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
888 cred, &bp);
889 if (bp->b_bcount != fs->fs_fsize)
890 error = EINVAL;
891 bp->b_flags |= B_INVAL;
892 if (error)
893 goto out;
894 brelse(bp);
895 bp = NULL;
896 }
897
898 fs->fs_ronly = ronly;
899 if (ronly == 0) {
900 fs->fs_clean <<= 1;
901 fs->fs_fmod = 1;
902 }
903 size = fs->fs_cssize;
904 blks = howmany(size, fs->fs_fsize);
905 if (fs->fs_contigsumsize > 0)
906 size += fs->fs_ncg * sizeof(int32_t);
907 size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
908 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
909 fs->fs_csp = space;
910 for (i = 0; i < blks; i += fs->fs_frag) {
911 size = fs->fs_bsize;
912 if (i + fs->fs_frag > blks)
913 size = (blks - i) * fs->fs_fsize;
914 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
915 cred, &bp);
916 if (error) {
917 free(fs->fs_csp, M_UFSMNT);
918 goto out;
919 }
920 #ifdef FFS_EI
921 if (needswap)
922 ffs_csum_swap((struct csum *)bp->b_data,
923 (struct csum *)space, size);
924 else
925 #endif
926 memcpy(space, bp->b_data, (u_int)size);
927
928 space = (char *)space + size;
929 brelse(bp);
930 bp = NULL;
931 }
932 if (fs->fs_contigsumsize > 0) {
933 fs->fs_maxcluster = lp = space;
934 for (i = 0; i < fs->fs_ncg; i++)
935 *lp++ = fs->fs_contigsumsize;
936 space = lp;
937 }
938 size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
939 fs->fs_contigdirs = space;
940 space = (char *)space + size;
941 memset(fs->fs_contigdirs, 0, size);
942 /* Compatibility for old filesystems - XXX */
943 if (fs->fs_avgfilesize <= 0)
944 fs->fs_avgfilesize = AVFILESIZ;
945 if (fs->fs_avgfpdir <= 0)
946 fs->fs_avgfpdir = AFPDIR;
947 fs->fs_active = NULL;
948 mp->mnt_data = ump;
949 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
950 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
951 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
952 mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
953 if (UFS_MPISAPPLEUFS(ump)) {
954 /* NeXT used to keep short symlinks in the inode even
955 * when using FS_42INODEFMT. In that case fs->fs_maxsymlinklen
956 * is probably -1, but we still need to be able to identify
957 * short symlinks.
958 */
959 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
960 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
961 mp->mnt_iflag |= IMNT_DTYPE;
962 } else {
963 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
964 ump->um_dirblksiz = DIRBLKSIZ;
965 if (ump->um_maxsymlinklen > 0)
966 mp->mnt_iflag |= IMNT_DTYPE;
967 else
968 mp->mnt_iflag &= ~IMNT_DTYPE;
969 }
970 mp->mnt_fs_bshift = fs->fs_bshift;
971 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
972 mp->mnt_flag |= MNT_LOCAL;
973 mp->mnt_iflag |= IMNT_HAS_TRANS;
974 #ifdef FFS_EI
975 if (needswap)
976 ump->um_flags |= UFS_NEEDSWAP;
977 #endif
978 ump->um_mountp = mp;
979 ump->um_dev = dev;
980 ump->um_devvp = devvp;
981 ump->um_nindir = fs->fs_nindir;
982 ump->um_lognindir = ffs(fs->fs_nindir) - 1;
983 ump->um_bptrtodb = fs->fs_fsbtodb;
984 ump->um_seqinc = fs->fs_frag;
985 for (i = 0; i < MAXQUOTAS; i++)
986 ump->um_quotas[i] = NULLVP;
987 devvp->v_specmountpoint = mp;
988 if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
989 error = softdep_mount(devvp, mp, fs, cred);
990 if (error) {
991 free(fs->fs_csp, M_UFSMNT);
992 goto out;
993 }
994 }
995 if (ronly == 0 && fs->fs_snapinum[0] != 0)
996 ffs_snapshot_mount(mp);
997 #ifdef UFS_EXTATTR
998 /*
999 * Initialize file-backed extended attributes on UFS1 file
1000 * systems.
1001 */
1002 if (ump->um_fstype == UFS1) {
1003 ufs_extattr_uepm_init(&ump->um_extattr);
1004 #ifdef UFS_EXTATTR_AUTOSTART
1005 /*
1006 * XXX Just ignore errors. Not clear that we should
1007 * XXX fail the mount in this case.
1008 */
1009 (void) ufs_extattr_autostart(mp, l);
1010 #endif
1011 }
1012 #endif /* UFS_EXTATTR */
1013 return (0);
1014 out:
1015 if (fs)
1016 free(fs, M_UFSMNT);
1017 devvp->v_specmountpoint = NULL;
1018 if (bp)
1019 brelse(bp);
1020 if (ump) {
1021 if (ump->um_oldfscompat)
1022 free(ump->um_oldfscompat, M_UFSMNT);
1023 free(ump, M_UFSMNT);
1024 mp->mnt_data = NULL;
1025 }
1026 return (error);
1027 }
1028
1029 /*
1030 * Sanity checks for loading old filesystem superblocks.
1031 * See ffs_oldfscompat_write below for unwound actions.
1032 *
1033 * XXX - Parts get retired eventually.
1034 * Unfortunately new bits get added.
1035 */
1036 static void
1037 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
1038 {
1039 off_t maxfilesize;
1040 int32_t *extrasave;
1041
1042 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1043 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1044 return;
1045
1046 if (!ump->um_oldfscompat)
1047 ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
1048 M_UFSMNT, M_WAITOK);
1049
1050 memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
1051 extrasave = ump->um_oldfscompat;
1052 extrasave += 512/sizeof(int32_t);
1053 extrasave[0] = fs->fs_old_npsect;
1054 extrasave[1] = fs->fs_old_interleave;
1055 extrasave[2] = fs->fs_old_trackskew;
1056
1057 /* These fields will be overwritten by their
1058 * original values in fs_oldfscompat_write, so it is harmless
1059 * to modify them here.
1060 */
1061 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1062 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1063 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1064 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1065
1066 fs->fs_maxbsize = fs->fs_bsize;
1067 fs->fs_time = fs->fs_old_time;
1068 fs->fs_size = fs->fs_old_size;
1069 fs->fs_dsize = fs->fs_old_dsize;
1070 fs->fs_csaddr = fs->fs_old_csaddr;
1071 fs->fs_sblockloc = sblockloc;
1072
1073 fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
1074
1075 if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
1076 fs->fs_old_nrpos = 8;
1077 fs->fs_old_npsect = fs->fs_old_nsect;
1078 fs->fs_old_interleave = 1;
1079 fs->fs_old_trackskew = 0;
1080 }
1081
1082 if (fs->fs_old_inodefmt < FS_44INODEFMT) {
1083 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
1084 fs->fs_qbmask = ~fs->fs_bmask;
1085 fs->fs_qfmask = ~fs->fs_fmask;
1086 }
1087
1088 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1089 if (fs->fs_maxfilesize > maxfilesize)
1090 fs->fs_maxfilesize = maxfilesize;
1091
1092 /* Compatibility for old filesystems */
1093 if (fs->fs_avgfilesize <= 0)
1094 fs->fs_avgfilesize = AVFILESIZ;
1095 if (fs->fs_avgfpdir <= 0)
1096 fs->fs_avgfpdir = AFPDIR;
1097
1098 #if 0
1099 if (bigcgs) {
1100 fs->fs_save_cgsize = fs->fs_cgsize;
1101 fs->fs_cgsize = fs->fs_bsize;
1102 }
1103 #endif
1104 }
1105
1106 /*
1107 * Unwinding superblock updates for old filesystems.
1108 * See ffs_oldfscompat_read above for details.
1109 *
1110 * XXX - Parts get retired eventually.
1111 * Unfortunately new bits get added.
1112 */
1113 static void
1114 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
1115 {
1116 int32_t *extrasave;
1117
1118 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1119 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1120 return;
1121
1122 fs->fs_old_time = fs->fs_time;
1123 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1124 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1125 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1126 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1127 fs->fs_old_flags = fs->fs_flags;
1128
1129 #if 0
1130 if (bigcgs) {
1131 fs->fs_cgsize = fs->fs_save_cgsize;
1132 }
1133 #endif
1134
1135 memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
1136 extrasave = ump->um_oldfscompat;
1137 extrasave += 512/sizeof(int32_t);
1138 fs->fs_old_npsect = extrasave[0];
1139 fs->fs_old_interleave = extrasave[1];
1140 fs->fs_old_trackskew = extrasave[2];
1141
1142 }
1143
1144 /*
1145 * unmount system call
1146 */
1147 int
1148 ffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
1149 {
1150 struct ufsmount *ump = VFSTOUFS(mp);
1151 struct fs *fs = ump->um_fs;
1152 int error, flags, penderr;
1153
1154 penderr = 0;
1155 flags = 0;
1156 if (mntflags & MNT_FORCE)
1157 flags |= FORCECLOSE;
1158 #ifdef UFS_EXTATTR
1159 if (ump->um_fstype == UFS1) {
1160 error = ufs_extattr_stop(mp, l);
1161 if (error) {
1162 if (error != EOPNOTSUPP)
1163 printf("%s: ufs_extattr_stop returned %d\n",
1164 fs->fs_fsmnt, error);
1165 } else
1166 ufs_extattr_uepm_destroy(&ump->um_extattr);
1167 }
1168 #endif /* UFS_EXTATTR */
1169 if (mp->mnt_flag & MNT_SOFTDEP) {
1170 if ((error = softdep_flushfiles(mp, flags, l)) != 0)
1171 return (error);
1172 } else {
1173 if ((error = ffs_flushfiles(mp, flags, l)) != 0)
1174 return (error);
1175 }
1176 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1177 printf("%s: unmount pending error: blocks %" PRId64
1178 " files %d\n",
1179 fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
1180 fs->fs_pendingblocks = 0;
1181 fs->fs_pendinginodes = 0;
1182 penderr = 1;
1183 }
1184 if (fs->fs_ronly == 0 &&
1185 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1186 fs->fs_clean & FS_WASCLEAN) {
1187 /*
1188 * XXXX don't mark fs clean in the case of softdep
1189 * pending block errors, until they are fixed.
1190 */
1191 if (penderr == 0) {
1192 if (mp->mnt_flag & MNT_SOFTDEP)
1193 fs->fs_flags &= ~FS_DOSOFTDEP;
1194 fs->fs_clean = FS_ISCLEAN;
1195 }
1196 fs->fs_fmod = 0;
1197 (void) ffs_sbupdate(ump, MNT_WAIT);
1198 }
1199 if (ump->um_devvp->v_type != VBAD)
1200 ump->um_devvp->v_specmountpoint = NULL;
1201 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1202 (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
1203 NOCRED, l);
1204 vput(ump->um_devvp);
1205 free(fs->fs_csp, M_UFSMNT);
1206 free(fs, M_UFSMNT);
1207 if (ump->um_oldfscompat != NULL)
1208 free(ump->um_oldfscompat, M_UFSMNT);
1209 free(ump, M_UFSMNT);
1210 mp->mnt_data = NULL;
1211 mp->mnt_flag &= ~MNT_LOCAL;
1212 return (0);
1213 }
1214
1215 /*
1216 * Flush out all the files in a filesystem.
1217 */
1218 int
1219 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
1220 {
1221 extern int doforce;
1222 struct ufsmount *ump;
1223 int error;
1224
1225 if (!doforce)
1226 flags &= ~FORCECLOSE;
1227 ump = VFSTOUFS(mp);
1228 #ifdef QUOTA
1229 if (mp->mnt_flag & MNT_QUOTA) {
1230 int i;
1231 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
1232 return (error);
1233 for (i = 0; i < MAXQUOTAS; i++) {
1234 if (ump->um_quotas[i] == NULLVP)
1235 continue;
1236 quotaoff(l, mp, i);
1237 }
1238 /*
1239 * Here we fall through to vflush again to ensure
1240 * that we have gotten rid of all the system vnodes.
1241 */
1242 }
1243 #endif
1244 if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
1245 return (error);
1246 ffs_snapshot_unmount(mp);
1247 /*
1248 * Flush all the files.
1249 */
1250 error = vflush(mp, NULLVP, flags);
1251 if (error)
1252 return (error);
1253 /*
1254 * Flush filesystem metadata.
1255 */
1256 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1257 error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0, l);
1258 VOP_UNLOCK(ump->um_devvp, 0);
1259 return (error);
1260 }
1261
1262 /*
1263 * Get file system statistics.
1264 */
1265 int
1266 ffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
1267 {
1268 struct ufsmount *ump;
1269 struct fs *fs;
1270
1271 ump = VFSTOUFS(mp);
1272 fs = ump->um_fs;
1273 sbp->f_bsize = fs->fs_bsize;
1274 sbp->f_frsize = fs->fs_fsize;
1275 sbp->f_iosize = fs->fs_bsize;
1276 sbp->f_blocks = fs->fs_dsize;
1277 sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1278 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1279 sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
1280 fs->fs_minfree) / (u_int64_t) 100;
1281 if (sbp->f_bfree > sbp->f_bresvd)
1282 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1283 else
1284 sbp->f_bavail = 0;
1285 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1286 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1287 sbp->f_favail = sbp->f_ffree;
1288 sbp->f_fresvd = 0;
1289 copy_statvfs_info(sbp, mp);
1290 return (0);
1291 }
1292
1293 /*
1294 * Go through the disk queues to initiate sandbagged IO;
1295 * go through the inodes to write those that have been modified;
1296 * initiate the writing of the super block if it has been modified.
1297 *
1298 * Note: we are always called with the filesystem marked `MPBUSY'.
1299 */
1300 int
1301 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred, struct lwp *l)
1302 {
1303 struct vnode *vp, *nvp;
1304 struct inode *ip;
1305 struct ufsmount *ump = VFSTOUFS(mp);
1306 struct fs *fs;
1307 int error, count, allerror = 0;
1308
1309 fs = ump->um_fs;
1310 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1311 printf("fs = %s\n", fs->fs_fsmnt);
1312 panic("update: rofs mod");
1313 }
1314 fstrans_start(mp, FSTRANS_SHARED);
1315 /*
1316 * Write back each (modified) inode.
1317 */
1318 simple_lock(&mntvnode_slock);
1319 loop:
1320 /*
1321 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
1322 * and vclean() can be called indirectly
1323 */
1324 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
1325 /*
1326 * If the vnode that we are about to sync is no longer
1327 * associated with this mount point, start over.
1328 */
1329 if (vp->v_mount != mp)
1330 goto loop;
1331 simple_lock(&vp->v_interlock);
1332 nvp = TAILQ_NEXT(vp, v_mntvnodes);
1333 ip = VTOI(vp);
1334 if (vp->v_type == VNON ||
1335 ((ip->i_flag &
1336 (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
1337 LIST_EMPTY(&vp->v_dirtyblkhd) &&
1338 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
1339 {
1340 simple_unlock(&vp->v_interlock);
1341 continue;
1342 }
1343 if (vp->v_type == VBLK &&
1344 fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
1345 simple_unlock(&vp->v_interlock);
1346 continue;
1347 }
1348 simple_unlock(&mntvnode_slock);
1349 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1350 if (error) {
1351 simple_lock(&mntvnode_slock);
1352 if (error == ENOENT)
1353 goto loop;
1354 continue;
1355 }
1356 if (vp->v_type == VREG && waitfor == MNT_LAZY)
1357 error = ffs_update(vp, NULL, NULL, 0);
1358 else
1359 error = VOP_FSYNC(vp, cred,
1360 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l);
1361 if (error)
1362 allerror = error;
1363 vput(vp);
1364 simple_lock(&mntvnode_slock);
1365 }
1366 simple_unlock(&mntvnode_slock);
1367 /*
1368 * Force stale file system control information to be flushed.
1369 */
1370 if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
1371 if ((error = softdep_flushworklist(ump->um_mountp, &count, l)))
1372 allerror = error;
1373 /* Flushed work items may create new vnodes to clean */
1374 if (allerror == 0 && count) {
1375 simple_lock(&mntvnode_slock);
1376 goto loop;
1377 }
1378 }
1379 if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
1380 !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
1381 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1382 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1383 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l)) != 0)
1384 allerror = error;
1385 VOP_UNLOCK(ump->um_devvp, 0);
1386 if (allerror == 0 && waitfor == MNT_WAIT) {
1387 simple_lock(&mntvnode_slock);
1388 goto loop;
1389 }
1390 }
1391 #ifdef QUOTA
1392 qsync(mp);
1393 #endif
1394 /*
1395 * Write back modified superblock.
1396 */
1397 if (fs->fs_fmod != 0) {
1398 fs->fs_fmod = 0;
1399 fs->fs_time = time_second;
1400 if ((error = ffs_cgupdate(ump, waitfor)))
1401 allerror = error;
1402 }
1403 fstrans_done(mp);
1404 return (allerror);
1405 }
1406
1407 /*
1408 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1409 * in from disk. If it is in core, wait for the lock bit to clear, then
1410 * return the inode locked. Detection and handling of mount points must be
1411 * done by the calling routine.
1412 */
1413 int
1414 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1415 {
1416 struct fs *fs;
1417 struct inode *ip;
1418 struct ufsmount *ump;
1419 struct buf *bp;
1420 struct vnode *vp;
1421 dev_t dev;
1422 int error;
1423
1424 ump = VFSTOUFS(mp);
1425 dev = ump->um_dev;
1426
1427 retry:
1428 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1429 return (0);
1430
1431 /* Allocate a new vnode/inode. */
1432 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1433 *vpp = NULL;
1434 return (error);
1435 }
1436 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
1437
1438 /*
1439 * If someone beat us to it, put back the freshly allocated
1440 * vnode/inode pair and retry.
1441 */
1442 mutex_enter(&ufs_hashlock);
1443 if (ufs_ihashget(dev, ino, 0) != NULL) {
1444 mutex_exit(&ufs_hashlock);
1445 ungetnewvnode(vp);
1446 pool_put(&ffs_inode_pool, ip);
1447 goto retry;
1448 }
1449
1450 vp->v_flag |= VLOCKSWORK;
1451
1452 /*
1453 * XXX MFS ends up here, too, to allocate an inode. Should we
1454 * XXX create another pool for MFS inodes?
1455 */
1456
1457 memset(ip, 0, sizeof(struct inode));
1458 vp->v_data = ip;
1459 ip->i_vnode = vp;
1460 ip->i_ump = ump;
1461 ip->i_fs = fs = ump->um_fs;
1462 ip->i_dev = dev;
1463 ip->i_number = ino;
1464 LIST_INIT(&ip->i_pcbufhd);
1465 #ifdef QUOTA
1466 ufsquota_init(ip);
1467 #endif
1468
1469 /*
1470 * Put it onto its hash chain and lock it so that other requests for
1471 * this inode will block if they arrive while we are sleeping waiting
1472 * for old data structures to be purged or for the contents of the
1473 * disk portion of this inode to be read.
1474 */
1475
1476 ufs_ihashins(ip);
1477 mutex_exit(&ufs_hashlock);
1478
1479 /* Read in the disk contents for the inode, copy into the inode. */
1480 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1481 (int)fs->fs_bsize, NOCRED, &bp);
1482 if (error) {
1483
1484 /*
1485 * The inode does not contain anything useful, so it would
1486 * be misleading to leave it on its hash chain. With mode
1487 * still zero, it will be unlinked and returned to the free
1488 * list by vput().
1489 */
1490
1491 vput(vp);
1492 brelse(bp);
1493 *vpp = NULL;
1494 return (error);
1495 }
1496 if (ip->i_ump->um_fstype == UFS1)
1497 ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
1498 else
1499 ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
1500 ffs_load_inode(bp, ip, fs, ino);
1501 if (DOINGSOFTDEP(vp))
1502 softdep_load_inodeblock(ip);
1503 else
1504 ip->i_ffs_effnlink = ip->i_nlink;
1505 brelse(bp);
1506
1507 /*
1508 * Initialize the vnode from the inode, check for aliases.
1509 * Note that the underlying vnode may have changed.
1510 */
1511
1512 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1513
1514 /*
1515 * Finish inode initialization now that aliasing has been resolved.
1516 */
1517
1518 genfs_node_init(vp, &ffs_genfsops);
1519 ip->i_devvp = ump->um_devvp;
1520 VREF(ip->i_devvp);
1521
1522 /*
1523 * Ensure that uid and gid are correct. This is a temporary
1524 * fix until fsck has been changed to do the update.
1525 */
1526
1527 if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1528 ip->i_uid = ip->i_ffs1_ouid; /* XXX */
1529 ip->i_gid = ip->i_ffs1_ogid; /* XXX */
1530 } /* XXX */
1531 uvm_vnp_setsize(vp, ip->i_size);
1532 *vpp = vp;
1533 return (0);
1534 }
1535
1536 /*
1537 * File handle to vnode
1538 *
1539 * Have to be really careful about stale file handles:
1540 * - check that the inode number is valid
1541 * - call ffs_vget() to get the locked inode
1542 * - check for an unallocated inode (i_mode == 0)
1543 * - check that the given client host has export rights and return
1544 * those rights via. exflagsp and credanonp
1545 */
1546 int
1547 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1548 {
1549 struct ufid ufh;
1550 struct fs *fs;
1551
1552 if (fhp->fid_len != sizeof(struct ufid))
1553 return EINVAL;
1554
1555 memcpy(&ufh, fhp, sizeof(ufh));
1556 fs = VFSTOUFS(mp)->um_fs;
1557 if (ufh.ufid_ino < ROOTINO ||
1558 ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1559 return (ESTALE);
1560 return (ufs_fhtovp(mp, &ufh, vpp));
1561 }
1562
1563 /*
1564 * Vnode pointer to File handle
1565 */
1566 /* ARGSUSED */
1567 int
1568 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1569 {
1570 struct inode *ip;
1571 struct ufid ufh;
1572
1573 if (*fh_size < sizeof(struct ufid)) {
1574 *fh_size = sizeof(struct ufid);
1575 return E2BIG;
1576 }
1577 ip = VTOI(vp);
1578 *fh_size = sizeof(struct ufid);
1579 memset(&ufh, 0, sizeof(ufh));
1580 ufh.ufid_len = sizeof(struct ufid);
1581 ufh.ufid_ino = ip->i_number;
1582 ufh.ufid_gen = ip->i_gen;
1583 memcpy(fhp, &ufh, sizeof(ufh));
1584 return (0);
1585 }
1586
1587 void
1588 ffs_init(void)
1589 {
1590 if (ffs_initcount++ > 0)
1591 return;
1592
1593 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0,
1594 "ffsinopl", &pool_allocator_nointr, IPL_NONE);
1595 pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
1596 "dino1pl", &pool_allocator_nointr, IPL_NONE);
1597 pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0,
1598 "dino2pl", &pool_allocator_nointr, IPL_NONE);
1599 softdep_initialize();
1600 ffs_snapshot_init();
1601 ufs_init();
1602 }
1603
1604 void
1605 ffs_reinit(void)
1606 {
1607 softdep_reinitialize();
1608 ufs_reinit();
1609 }
1610
1611 void
1612 ffs_done(void)
1613 {
1614 if (--ffs_initcount > 0)
1615 return;
1616
1617 /* XXX softdep cleanup ? */
1618 ffs_snapshot_fini();
1619 ufs_done();
1620 pool_destroy(&ffs_dinode2_pool);
1621 pool_destroy(&ffs_dinode1_pool);
1622 pool_destroy(&ffs_inode_pool);
1623 }
1624
1625 SYSCTL_SETUP(sysctl_vfs_ffs_setup, "sysctl vfs.ffs subtree setup")
1626 {
1627 #if 0
1628 extern int doasyncfree;
1629 #endif
1630 extern int ffs_log_changeopt;
1631
1632 sysctl_createv(clog, 0, NULL, NULL,
1633 CTLFLAG_PERMANENT,
1634 CTLTYPE_NODE, "vfs", NULL,
1635 NULL, 0, NULL, 0,
1636 CTL_VFS, CTL_EOL);
1637 sysctl_createv(clog, 0, NULL, NULL,
1638 CTLFLAG_PERMANENT,
1639 CTLTYPE_NODE, "ffs",
1640 SYSCTL_DESCR("Berkeley Fast File System"),
1641 NULL, 0, NULL, 0,
1642 CTL_VFS, 1, CTL_EOL);
1643
1644 /*
1645 * @@@ should we even bother with these first three?
1646 */
1647 sysctl_createv(clog, 0, NULL, NULL,
1648 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1649 CTLTYPE_INT, "doclusterread", NULL,
1650 sysctl_notavail, 0, NULL, 0,
1651 CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
1652 sysctl_createv(clog, 0, NULL, NULL,
1653 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1654 CTLTYPE_INT, "doclusterwrite", NULL,
1655 sysctl_notavail, 0, NULL, 0,
1656 CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
1657 sysctl_createv(clog, 0, NULL, NULL,
1658 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1659 CTLTYPE_INT, "doreallocblks", NULL,
1660 sysctl_notavail, 0, NULL, 0,
1661 CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
1662 #if 0
1663 sysctl_createv(clog, 0, NULL, NULL,
1664 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1665 CTLTYPE_INT, "doasyncfree",
1666 SYSCTL_DESCR("Release dirty blocks asynchronously"),
1667 NULL, 0, &doasyncfree, 0,
1668 CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
1669 #endif
1670 sysctl_createv(clog, 0, NULL, NULL,
1671 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1672 CTLTYPE_INT, "log_changeopt",
1673 SYSCTL_DESCR("Log changes in optimization strategy"),
1674 NULL, 0, &ffs_log_changeopt, 0,
1675 CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
1676 }
1677
1678 /*
1679 * Write a superblock and associated information back to disk.
1680 */
1681 int
1682 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1683 {
1684 struct fs *fs = mp->um_fs;
1685 struct buf *bp;
1686 int error = 0;
1687 u_int32_t saveflag;
1688
1689 bp = getblk(mp->um_devvp,
1690 fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
1691 (int)fs->fs_sbsize, 0, 0);
1692 saveflag = fs->fs_flags & FS_INTERNAL;
1693 fs->fs_flags &= ~FS_INTERNAL;
1694
1695 memcpy(bp->b_data, fs, fs->fs_sbsize);
1696
1697 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1698 #ifdef FFS_EI
1699 if (mp->um_flags & UFS_NEEDSWAP)
1700 ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
1701 #endif
1702 fs->fs_flags |= saveflag;
1703
1704 if (waitfor == MNT_WAIT)
1705 error = bwrite(bp);
1706 else
1707 bawrite(bp);
1708 return (error);
1709 }
1710
1711 int
1712 ffs_cgupdate(struct ufsmount *mp, int waitfor)
1713 {
1714 struct fs *fs = mp->um_fs;
1715 struct buf *bp;
1716 int blks;
1717 void *space;
1718 int i, size, error = 0, allerror = 0;
1719
1720 allerror = ffs_sbupdate(mp, waitfor);
1721 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1722 space = fs->fs_csp;
1723 for (i = 0; i < blks; i += fs->fs_frag) {
1724 size = fs->fs_bsize;
1725 if (i + fs->fs_frag > blks)
1726 size = (blks - i) * fs->fs_fsize;
1727 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1728 size, 0, 0);
1729 #ifdef FFS_EI
1730 if (mp->um_flags & UFS_NEEDSWAP)
1731 ffs_csum_swap((struct csum*)space,
1732 (struct csum*)bp->b_data, size);
1733 else
1734 #endif
1735 memcpy(bp->b_data, space, (u_int)size);
1736 space = (char *)space + size;
1737 if (waitfor == MNT_WAIT)
1738 error = bwrite(bp);
1739 else
1740 bawrite(bp);
1741 }
1742 if (!allerror && error)
1743 allerror = error;
1744 return (allerror);
1745 }
1746
1747 int
1748 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
1749 int attrnamespace, const char *attrname, struct lwp *l)
1750 {
1751 #ifdef UFS_EXTATTR
1752 /*
1753 * File-backed extended attributes are only supported on UFS1.
1754 * UFS2 has native extended attributes.
1755 */
1756 if (VFSTOUFS(mp)->um_fstype == UFS1)
1757 return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname,
1758 l));
1759 #endif
1760 return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname, l));
1761 }
1762
1763 int
1764 ffs_suspendctl(struct mount *mp, int cmd)
1765 {
1766 int error;
1767 struct lwp *l = curlwp;
1768
1769 switch (cmd) {
1770 case SUSPEND_SUSPEND:
1771 if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
1772 return error;
1773 error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred, l);
1774 if (error == 0)
1775 error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
1776 if (error != 0) {
1777 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
1778 return error;
1779 }
1780 return 0;
1781
1782 case SUSPEND_RESUME:
1783 return fstrans_setstate(mp, FSTRANS_NORMAL);
1784
1785 default:
1786 return EINVAL;
1787 }
1788 }
1789