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