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