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