ffs_vfsops.c revision 1.188 1 /* $NetBSD: ffs_vfsops.c,v 1.188 2006/10/20 18:58:12 reinoud 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.188 2006/10/20 18:58:12 reinoud 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, *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 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
654 if (vp->v_mount != mp) {
655 simple_unlock(&mntvnode_slock);
656 goto loop;
657 }
658 /*
659 * Step 4: invalidate all inactive vnodes.
660 */
661 if (vrecycle(vp, &mntvnode_slock, l))
662 goto loop;
663 /*
664 * Step 5: invalidate all cached file data.
665 */
666 simple_lock(&vp->v_interlock);
667 simple_unlock(&mntvnode_slock);
668 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
669 goto loop;
670 if (vinvalbuf(vp, 0, cred, l, 0, 0))
671 panic("ffs_reload: dirty2");
672 /*
673 * Step 6: re-read inode data for all active vnodes.
674 */
675 ip = VTOI(vp);
676 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
677 (int)fs->fs_bsize, NOCRED, &bp);
678 if (error) {
679 brelse(bp);
680 vput(vp);
681 return (error);
682 }
683 ffs_load_inode(bp, ip, fs, ip->i_number);
684 ip->i_ffs_effnlink = ip->i_nlink;
685 brelse(bp);
686 vput(vp);
687 simple_lock(&mntvnode_slock);
688 }
689 simple_unlock(&mntvnode_slock);
690 return (0);
691 }
692
693 /*
694 * Possible superblock locations ordered from most to least likely.
695 */
696 static const int sblock_try[] = SBLOCKSEARCH;
697
698 /*
699 * Common code for mount and mountroot
700 */
701 int
702 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
703 {
704 struct ufsmount *ump;
705 struct buf *bp;
706 struct fs *fs;
707 dev_t dev;
708 struct partinfo dpart;
709 void *space;
710 daddr_t sblockloc, fsblockloc;
711 int blks, fstype;
712 int error, i, size, ronly;
713 #ifdef FFS_EI
714 int needswap = 0; /* keep gcc happy */
715 #endif
716 int32_t *lp;
717 kauth_cred_t cred;
718 u_int32_t sbsize = 8192; /* keep gcc happy*/
719
720 dev = devvp->v_rdev;
721 cred = l ? l->l_cred : NOCRED;
722
723 /* Flush out any old buffers remaining from a previous use. */
724 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
725 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
726 VOP_UNLOCK(devvp, 0);
727 if (error)
728 return (error);
729
730 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
731 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) != 0)
732 size = DEV_BSIZE;
733 else
734 size = dpart.disklab->d_secsize;
735
736 bp = NULL;
737 ump = NULL;
738 fs = NULL;
739 sblockloc = 0;
740 fstype = 0;
741
742 /*
743 * Try reading the superblock in each of its possible locations. */
744 for (i = 0; ; i++) {
745 if (bp != NULL) {
746 bp->b_flags |= B_NOCACHE;
747 brelse(bp);
748 bp = NULL;
749 }
750 if (sblock_try[i] == -1) {
751 error = EINVAL;
752 fs = NULL;
753 goto out;
754 }
755 error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
756 &bp);
757 if (error) {
758 fs = NULL;
759 goto out;
760 }
761 fs = (struct fs*)bp->b_data;
762 fsblockloc = sblockloc = sblock_try[i];
763 if (fs->fs_magic == FS_UFS1_MAGIC) {
764 sbsize = fs->fs_sbsize;
765 fstype = UFS1;
766 #ifdef FFS_EI
767 needswap = 0;
768 } else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
769 sbsize = bswap32(fs->fs_sbsize);
770 fstype = UFS1;
771 needswap = 1;
772 #endif
773 } else if (fs->fs_magic == FS_UFS2_MAGIC) {
774 sbsize = fs->fs_sbsize;
775 fstype = UFS2;
776 #ifdef FFS_EI
777 needswap = 0;
778 } else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
779 sbsize = bswap32(fs->fs_sbsize);
780 fstype = UFS2;
781 needswap = 1;
782 #endif
783 } else
784 continue;
785
786
787 /* fs->fs_sblockloc isn't defined for old filesystems */
788 if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
789 if (sblockloc == SBLOCK_UFS2)
790 /*
791 * This is likely to be the first alternate
792 * in a filesystem with 64k blocks.
793 * Don't use it.
794 */
795 continue;
796 fsblockloc = sblockloc;
797 } else {
798 fsblockloc = fs->fs_sblockloc;
799 #ifdef FFS_EI
800 if (needswap)
801 fsblockloc = bswap64(fsblockloc);
802 #endif
803 }
804
805 /* Check we haven't found an alternate superblock */
806 if (fsblockloc != sblockloc)
807 continue;
808
809 /* Validate size of superblock */
810 if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
811 continue;
812
813 /* Ok seems to be a good superblock */
814 break;
815 }
816
817 fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
818 memcpy(fs, bp->b_data, sbsize);
819
820 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
821 memset(ump, 0, sizeof *ump);
822 TAILQ_INIT(&ump->um_snapshots);
823 ump->um_fs = fs;
824 ump->um_ops = &ffs_ufsops;
825
826 #ifdef FFS_EI
827 if (needswap) {
828 ffs_sb_swap((struct fs*)bp->b_data, fs);
829 fs->fs_flags |= FS_SWAPPED;
830 } else
831 #endif
832 fs->fs_flags &= ~FS_SWAPPED;
833
834 ffs_oldfscompat_read(fs, ump, sblockloc);
835 ump->um_maxfilesize = fs->fs_maxfilesize;
836
837 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
838 fs->fs_pendingblocks = 0;
839 fs->fs_pendinginodes = 0;
840 }
841
842 ump->um_fstype = fstype;
843 if (fs->fs_sbsize < SBLOCKSIZE)
844 bp->b_flags |= B_INVAL;
845 brelse(bp);
846 bp = NULL;
847
848 /* First check to see if this is tagged as an Apple UFS filesystem
849 * in the disklabel
850 */
851 if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) == 0) &&
852 (dpart.part->p_fstype == FS_APPLEUFS)) {
853 ump->um_flags |= UFS_ISAPPLEUFS;
854 }
855 #ifdef APPLE_UFS
856 else {
857 /* Manually look for an apple ufs label, and if a valid one
858 * is found, then treat it like an Apple UFS filesystem anyway
859 */
860 error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
861 APPLEUFS_LABEL_SIZE, cred, &bp);
862 if (error)
863 goto out;
864 error = ffs_appleufs_validate(fs->fs_fsmnt,
865 (struct appleufslabel *)bp->b_data,NULL);
866 if (error == 0) {
867 ump->um_flags |= UFS_ISAPPLEUFS;
868 }
869 brelse(bp);
870 bp = NULL;
871 }
872 #else
873 if (ump->um_flags & UFS_ISAPPLEUFS) {
874 error = EINVAL;
875 goto out;
876 }
877 #endif
878
879 /*
880 * verify that we can access the last block in the fs
881 * if we're mounting read/write.
882 */
883
884 if (!ronly) {
885 error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
886 cred, &bp);
887 if (bp->b_bcount != fs->fs_fsize)
888 error = EINVAL;
889 bp->b_flags |= B_INVAL;
890 if (error)
891 goto out;
892 brelse(bp);
893 bp = NULL;
894 }
895
896 fs->fs_ronly = ronly;
897 if (ronly == 0) {
898 fs->fs_clean <<= 1;
899 fs->fs_fmod = 1;
900 }
901 size = fs->fs_cssize;
902 blks = howmany(size, fs->fs_fsize);
903 if (fs->fs_contigsumsize > 0)
904 size += fs->fs_ncg * sizeof(int32_t);
905 size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
906 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
907 fs->fs_csp = space;
908 for (i = 0; i < blks; i += fs->fs_frag) {
909 size = fs->fs_bsize;
910 if (i + fs->fs_frag > blks)
911 size = (blks - i) * fs->fs_fsize;
912 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
913 cred, &bp);
914 if (error) {
915 free(fs->fs_csp, M_UFSMNT);
916 goto out;
917 }
918 #ifdef FFS_EI
919 if (needswap)
920 ffs_csum_swap((struct csum *)bp->b_data,
921 (struct csum *)space, size);
922 else
923 #endif
924 memcpy(space, bp->b_data, (u_int)size);
925
926 space = (char *)space + size;
927 brelse(bp);
928 bp = NULL;
929 }
930 if (fs->fs_contigsumsize > 0) {
931 fs->fs_maxcluster = lp = space;
932 for (i = 0; i < fs->fs_ncg; i++)
933 *lp++ = fs->fs_contigsumsize;
934 space = lp;
935 }
936 size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
937 fs->fs_contigdirs = space;
938 space = (char *)space + size;
939 memset(fs->fs_contigdirs, 0, size);
940 /* Compatibility for old filesystems - XXX */
941 if (fs->fs_avgfilesize <= 0)
942 fs->fs_avgfilesize = AVFILESIZ;
943 if (fs->fs_avgfpdir <= 0)
944 fs->fs_avgfpdir = AFPDIR;
945 fs->fs_active = NULL;
946 mp->mnt_data = ump;
947 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
948 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
949 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
950 mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
951 if (UFS_MPISAPPLEUFS(ump)) {
952 /* NeXT used to keep short symlinks in the inode even
953 * when using FS_42INODEFMT. In that case fs->fs_maxsymlinklen
954 * is probably -1, but we still need to be able to identify
955 * short symlinks.
956 */
957 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
958 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
959 mp->mnt_iflag |= IMNT_DTYPE;
960 } else {
961 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
962 ump->um_dirblksiz = DIRBLKSIZ;
963 if (ump->um_maxsymlinklen > 0)
964 mp->mnt_iflag |= IMNT_DTYPE;
965 else
966 mp->mnt_iflag &= ~IMNT_DTYPE;
967 }
968 mp->mnt_fs_bshift = fs->fs_bshift;
969 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
970 mp->mnt_flag |= MNT_LOCAL;
971 #ifdef FFS_EI
972 if (needswap)
973 ump->um_flags |= UFS_NEEDSWAP;
974 #endif
975 ump->um_mountp = mp;
976 ump->um_dev = dev;
977 ump->um_devvp = devvp;
978 ump->um_nindir = fs->fs_nindir;
979 ump->um_lognindir = ffs(fs->fs_nindir) - 1;
980 ump->um_bptrtodb = fs->fs_fsbtodb;
981 ump->um_seqinc = fs->fs_frag;
982 for (i = 0; i < MAXQUOTAS; i++)
983 ump->um_quotas[i] = NULLVP;
984 devvp->v_specmountpoint = mp;
985 if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
986 error = softdep_mount(devvp, mp, fs, cred);
987 if (error) {
988 free(fs->fs_csp, M_UFSMNT);
989 goto out;
990 }
991 }
992 if (ronly == 0 && fs->fs_snapinum[0] != 0)
993 ffs_snapshot_mount(mp);
994 #ifdef UFS_EXTATTR
995 /*
996 * Initialize file-backed extended attributes on UFS1 file
997 * systems.
998 */
999 if (ump->um_fstype == UFS1) {
1000 ufs_extattr_uepm_init(&ump->um_extattr);
1001 #ifdef UFS_EXTATTR_AUTOSTART
1002 /*
1003 * XXX Just ignore errors. Not clear that we should
1004 * XXX fail the mount in this case.
1005 */
1006 (void) ufs_extattr_autostart(mp, l);
1007 #endif
1008 }
1009 #endif /* UFS_EXTATTR */
1010 return (0);
1011 out:
1012 if (fs)
1013 free(fs, M_UFSMNT);
1014 devvp->v_specmountpoint = NULL;
1015 if (bp)
1016 brelse(bp);
1017 if (ump) {
1018 if (ump->um_oldfscompat)
1019 free(ump->um_oldfscompat, M_UFSMNT);
1020 free(ump, M_UFSMNT);
1021 mp->mnt_data = NULL;
1022 }
1023 return (error);
1024 }
1025
1026 /*
1027 * Sanity checks for loading old filesystem superblocks.
1028 * See ffs_oldfscompat_write below for unwound actions.
1029 *
1030 * XXX - Parts get retired eventually.
1031 * Unfortunately new bits get added.
1032 */
1033 static void
1034 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
1035 {
1036 off_t maxfilesize;
1037 int32_t *extrasave;
1038
1039 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1040 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1041 return;
1042
1043 if (!ump->um_oldfscompat)
1044 ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
1045 M_UFSMNT, M_WAITOK);
1046
1047 memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
1048 extrasave = ump->um_oldfscompat;
1049 extrasave += 512/sizeof(int32_t);
1050 extrasave[0] = fs->fs_old_npsect;
1051 extrasave[1] = fs->fs_old_interleave;
1052 extrasave[2] = fs->fs_old_trackskew;
1053
1054 /* These fields will be overwritten by their
1055 * original values in fs_oldfscompat_write, so it is harmless
1056 * to modify them here.
1057 */
1058 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1059 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1060 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1061 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1062
1063 fs->fs_maxbsize = fs->fs_bsize;
1064 fs->fs_time = fs->fs_old_time;
1065 fs->fs_size = fs->fs_old_size;
1066 fs->fs_dsize = fs->fs_old_dsize;
1067 fs->fs_csaddr = fs->fs_old_csaddr;
1068 fs->fs_sblockloc = sblockloc;
1069
1070 fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
1071
1072 if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
1073 fs->fs_old_nrpos = 8;
1074 fs->fs_old_npsect = fs->fs_old_nsect;
1075 fs->fs_old_interleave = 1;
1076 fs->fs_old_trackskew = 0;
1077 }
1078
1079 if (fs->fs_old_inodefmt < FS_44INODEFMT) {
1080 ump->um_maxfilesize = (u_quad_t) 1LL << 39;
1081 fs->fs_qbmask = ~fs->fs_bmask;
1082 fs->fs_qfmask = ~fs->fs_fmask;
1083 }
1084
1085 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1086 if (ump->um_maxfilesize > maxfilesize)
1087 ump->um_maxfilesize = maxfilesize;
1088
1089 /* Compatibility for old filesystems */
1090 if (fs->fs_avgfilesize <= 0)
1091 fs->fs_avgfilesize = AVFILESIZ;
1092 if (fs->fs_avgfpdir <= 0)
1093 fs->fs_avgfpdir = AFPDIR;
1094
1095 #if 0
1096 if (bigcgs) {
1097 fs->fs_save_cgsize = fs->fs_cgsize;
1098 fs->fs_cgsize = fs->fs_bsize;
1099 }
1100 #endif
1101 }
1102
1103 /*
1104 * Unwinding superblock updates for old filesystems.
1105 * See ffs_oldfscompat_read above for details.
1106 *
1107 * XXX - Parts get retired eventually.
1108 * Unfortunately new bits get added.
1109 */
1110 static void
1111 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
1112 {
1113 int32_t *extrasave;
1114
1115 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1116 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1117 return;
1118
1119 fs->fs_old_time = fs->fs_time;
1120 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1121 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1122 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1123 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1124 fs->fs_old_flags = fs->fs_flags;
1125
1126 #if 0
1127 if (bigcgs) {
1128 fs->fs_cgsize = fs->fs_save_cgsize;
1129 }
1130 #endif
1131
1132 memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
1133 extrasave = ump->um_oldfscompat;
1134 extrasave += 512/sizeof(int32_t);
1135 fs->fs_old_npsect = extrasave[0];
1136 fs->fs_old_interleave = extrasave[1];
1137 fs->fs_old_trackskew = extrasave[2];
1138
1139 }
1140
1141 /*
1142 * unmount system call
1143 */
1144 int
1145 ffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
1146 {
1147 struct ufsmount *ump = VFSTOUFS(mp);
1148 struct fs *fs = ump->um_fs;
1149 int error, flags, penderr;
1150
1151 penderr = 0;
1152 flags = 0;
1153 if (mntflags & MNT_FORCE)
1154 flags |= FORCECLOSE;
1155 #ifdef UFS_EXTATTR
1156 if (ump->um_fstype == UFS1) {
1157 error = ufs_extattr_stop(mp, l);
1158 if (error) {
1159 if (error != EOPNOTSUPP)
1160 printf("%s: ufs_extattr_stop returned %d\n",
1161 fs->fs_fsmnt, error);
1162 } else
1163 ufs_extattr_uepm_destroy(&ump->um_extattr);
1164 }
1165 #endif /* UFS_EXTATTR */
1166 if (mp->mnt_flag & MNT_SOFTDEP) {
1167 if ((error = softdep_flushfiles(mp, flags, l)) != 0)
1168 return (error);
1169 } else {
1170 if ((error = ffs_flushfiles(mp, flags, l)) != 0)
1171 return (error);
1172 }
1173 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1174 printf("%s: unmount pending error: blocks %" PRId64
1175 " files %d\n",
1176 fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
1177 fs->fs_pendingblocks = 0;
1178 fs->fs_pendinginodes = 0;
1179 penderr = 1;
1180 }
1181 if (fs->fs_ronly == 0 &&
1182 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1183 fs->fs_clean & FS_WASCLEAN) {
1184 /*
1185 * XXXX don't mark fs clean in the case of softdep
1186 * pending block errors, until they are fixed.
1187 */
1188 if (penderr == 0) {
1189 if (mp->mnt_flag & MNT_SOFTDEP)
1190 fs->fs_flags &= ~FS_DOSOFTDEP;
1191 fs->fs_clean = FS_ISCLEAN;
1192 }
1193 fs->fs_fmod = 0;
1194 (void) ffs_sbupdate(ump, MNT_WAIT);
1195 }
1196 if (ump->um_devvp->v_type != VBAD)
1197 ump->um_devvp->v_specmountpoint = NULL;
1198 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1199 (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
1200 NOCRED, l);
1201 vput(ump->um_devvp);
1202 free(fs->fs_csp, M_UFSMNT);
1203 free(fs, M_UFSMNT);
1204 if (ump->um_oldfscompat != NULL)
1205 free(ump->um_oldfscompat, M_UFSMNT);
1206 free(ump, M_UFSMNT);
1207 mp->mnt_data = NULL;
1208 mp->mnt_flag &= ~MNT_LOCAL;
1209 return (0);
1210 }
1211
1212 /*
1213 * Flush out all the files in a filesystem.
1214 */
1215 int
1216 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
1217 {
1218 extern int doforce;
1219 struct ufsmount *ump;
1220 int error;
1221
1222 if (!doforce)
1223 flags &= ~FORCECLOSE;
1224 ump = VFSTOUFS(mp);
1225 #ifdef QUOTA
1226 if (mp->mnt_flag & MNT_QUOTA) {
1227 int i;
1228 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
1229 return (error);
1230 for (i = 0; i < MAXQUOTAS; i++) {
1231 if (ump->um_quotas[i] == NULLVP)
1232 continue;
1233 quotaoff(l, mp, i);
1234 }
1235 /*
1236 * Here we fall through to vflush again to ensure
1237 * that we have gotten rid of all the system vnodes.
1238 */
1239 }
1240 #endif
1241 if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
1242 return (error);
1243 ffs_snapshot_unmount(mp);
1244 /*
1245 * Flush all the files.
1246 */
1247 error = vflush(mp, NULLVP, flags);
1248 if (error)
1249 return (error);
1250 /*
1251 * Flush filesystem metadata.
1252 */
1253 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1254 error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0, l);
1255 VOP_UNLOCK(ump->um_devvp, 0);
1256 return (error);
1257 }
1258
1259 /*
1260 * Get file system statistics.
1261 */
1262 int
1263 ffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l __unused)
1264 {
1265 struct ufsmount *ump;
1266 struct fs *fs;
1267
1268 ump = VFSTOUFS(mp);
1269 fs = ump->um_fs;
1270 sbp->f_bsize = fs->fs_bsize;
1271 sbp->f_frsize = fs->fs_fsize;
1272 sbp->f_iosize = fs->fs_bsize;
1273 sbp->f_blocks = fs->fs_dsize;
1274 sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1275 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1276 sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
1277 fs->fs_minfree) / (u_int64_t) 100;
1278 if (sbp->f_bfree > sbp->f_bresvd)
1279 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1280 else
1281 sbp->f_bavail = 0;
1282 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1283 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1284 sbp->f_favail = sbp->f_ffree;
1285 sbp->f_fresvd = 0;
1286 copy_statvfs_info(sbp, mp);
1287 return (0);
1288 }
1289
1290 /*
1291 * Go through the disk queues to initiate sandbagged IO;
1292 * go through the inodes to write those that have been modified;
1293 * initiate the writing of the super block if it has been modified.
1294 *
1295 * Note: we are always called with the filesystem marked `MPBUSY'.
1296 */
1297 int
1298 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred, struct lwp *l)
1299 {
1300 struct vnode *vp;
1301 struct inode *ip;
1302 struct ufsmount *ump = VFSTOUFS(mp);
1303 struct fs *fs;
1304 int error, count, allerror = 0;
1305
1306 fs = ump->um_fs;
1307 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1308 printf("fs = %s\n", fs->fs_fsmnt);
1309 panic("update: rofs mod");
1310 }
1311 /*
1312 * Write back each (modified) inode.
1313 */
1314 simple_lock(&mntvnode_slock);
1315 loop:
1316 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
1317 /*
1318 * If the vnode that we are about to sync is no longer
1319 * associated with this mount point, start over.
1320 */
1321 if (vp->v_mount != mp)
1322 goto loop;
1323 simple_lock(&vp->v_interlock);
1324 ip = VTOI(vp);
1325 if (vp->v_type == VNON ||
1326 ((ip->i_flag &
1327 (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
1328 LIST_EMPTY(&vp->v_dirtyblkhd) &&
1329 vp->v_uobj.uo_npages == 0))
1330 {
1331 simple_unlock(&vp->v_interlock);
1332 continue;
1333 }
1334 simple_unlock(&mntvnode_slock);
1335 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1336 if (error) {
1337 simple_lock(&mntvnode_slock);
1338 if (error == ENOENT)
1339 goto loop;
1340 continue;
1341 }
1342 if (vp->v_type == VREG && waitfor == MNT_LAZY)
1343 error = ffs_update(vp, NULL, NULL, 0);
1344 else
1345 error = VOP_FSYNC(vp, cred,
1346 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l);
1347 if (error)
1348 allerror = error;
1349 vput(vp);
1350 simple_lock(&mntvnode_slock);
1351 }
1352 simple_unlock(&mntvnode_slock);
1353 /*
1354 * Force stale file system control information to be flushed.
1355 */
1356 if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
1357 if ((error = softdep_flushworklist(ump->um_mountp, &count, l)))
1358 allerror = error;
1359 /* Flushed work items may create new vnodes to clean */
1360 if (allerror == 0 && count) {
1361 simple_lock(&mntvnode_slock);
1362 goto loop;
1363 }
1364 }
1365 if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
1366 !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
1367 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1368 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1369 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l)) != 0)
1370 allerror = error;
1371 VOP_UNLOCK(ump->um_devvp, 0);
1372 if (allerror == 0 && waitfor == MNT_WAIT) {
1373 simple_lock(&mntvnode_slock);
1374 goto loop;
1375 }
1376 }
1377 #ifdef QUOTA
1378 qsync(mp);
1379 #endif
1380 /*
1381 * Write back modified superblock.
1382 */
1383 if (fs->fs_fmod != 0) {
1384 fs->fs_fmod = 0;
1385 fs->fs_time = time_second;
1386 if ((error = ffs_cgupdate(ump, waitfor)))
1387 allerror = error;
1388 }
1389 return (allerror);
1390 }
1391
1392 /*
1393 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1394 * in from disk. If it is in core, wait for the lock bit to clear, then
1395 * return the inode locked. Detection and handling of mount points must be
1396 * done by the calling routine.
1397 */
1398 int
1399 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1400 {
1401 struct fs *fs;
1402 struct inode *ip;
1403 struct ufsmount *ump;
1404 struct buf *bp;
1405 struct vnode *vp;
1406 dev_t dev;
1407 int error;
1408
1409 ump = VFSTOUFS(mp);
1410 dev = ump->um_dev;
1411
1412 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1413 return (0);
1414
1415 /* Allocate a new vnode/inode. */
1416 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1417 *vpp = NULL;
1418 return (error);
1419 }
1420
1421 /*
1422 * If someone beat us to it while sleeping in getnewvnode(),
1423 * push back the freshly allocated vnode we don't need, and return.
1424 */
1425
1426 do {
1427 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
1428 ungetnewvnode(vp);
1429 return (0);
1430 }
1431 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1432
1433 vp->v_flag |= VLOCKSWORK;
1434
1435 /*
1436 * XXX MFS ends up here, too, to allocate an inode. Should we
1437 * XXX create another pool for MFS inodes?
1438 */
1439
1440 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
1441 memset(ip, 0, sizeof(struct inode));
1442 vp->v_data = ip;
1443 ip->i_vnode = vp;
1444 ip->i_ump = ump;
1445 ip->i_fs = fs = ump->um_fs;
1446 ip->i_dev = dev;
1447 ip->i_number = ino;
1448 LIST_INIT(&ip->i_pcbufhd);
1449 #ifdef QUOTA
1450 {
1451 int i;
1452
1453 for (i = 0; i < MAXQUOTAS; i++)
1454 ip->i_dquot[i] = NODQUOT;
1455 }
1456 #endif
1457
1458 /*
1459 * Put it onto its hash chain and lock it so that other requests for
1460 * this inode will block if they arrive while we are sleeping waiting
1461 * for old data structures to be purged or for the contents of the
1462 * disk portion of this inode to be read.
1463 */
1464
1465 ufs_ihashins(ip);
1466 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1467
1468 /* Read in the disk contents for the inode, copy into the inode. */
1469 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1470 (int)fs->fs_bsize, NOCRED, &bp);
1471 if (error) {
1472
1473 /*
1474 * The inode does not contain anything useful, so it would
1475 * be misleading to leave it on its hash chain. With mode
1476 * still zero, it will be unlinked and returned to the free
1477 * list by vput().
1478 */
1479
1480 vput(vp);
1481 brelse(bp);
1482 *vpp = NULL;
1483 return (error);
1484 }
1485 if (ip->i_ump->um_fstype == UFS1)
1486 ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
1487 else
1488 ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
1489 ffs_load_inode(bp, ip, fs, ino);
1490 if (DOINGSOFTDEP(vp))
1491 softdep_load_inodeblock(ip);
1492 else
1493 ip->i_ffs_effnlink = ip->i_nlink;
1494 brelse(bp);
1495
1496 /*
1497 * Initialize the vnode from the inode, check for aliases.
1498 * Note that the underlying vnode may have changed.
1499 */
1500
1501 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1502
1503 /*
1504 * Finish inode initialization now that aliasing has been resolved.
1505 */
1506
1507 genfs_node_init(vp, &ffs_genfsops);
1508 ip->i_devvp = ump->um_devvp;
1509 VREF(ip->i_devvp);
1510
1511 /*
1512 * Ensure that uid and gid are correct. This is a temporary
1513 * fix until fsck has been changed to do the update.
1514 */
1515
1516 if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1517 ip->i_uid = ip->i_ffs1_ouid; /* XXX */
1518 ip->i_gid = ip->i_ffs1_ogid; /* XXX */
1519 } /* XXX */
1520 uvm_vnp_setsize(vp, ip->i_size);
1521 *vpp = vp;
1522 return (0);
1523 }
1524
1525 /*
1526 * File handle to vnode
1527 *
1528 * Have to be really careful about stale file handles:
1529 * - check that the inode number is valid
1530 * - call ffs_vget() to get the locked inode
1531 * - check for an unallocated inode (i_mode == 0)
1532 * - check that the given client host has export rights and return
1533 * those rights via. exflagsp and credanonp
1534 */
1535 int
1536 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1537 {
1538 struct ufid ufh;
1539 struct fs *fs;
1540
1541 if (fhp->fid_len != sizeof(struct ufid))
1542 return EINVAL;
1543
1544 memcpy(&ufh, fhp, sizeof(ufh));
1545 fs = VFSTOUFS(mp)->um_fs;
1546 if (ufh.ufid_ino < ROOTINO ||
1547 ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1548 return (ESTALE);
1549 return (ufs_fhtovp(mp, &ufh, vpp));
1550 }
1551
1552 /*
1553 * Vnode pointer to File handle
1554 */
1555 /* ARGSUSED */
1556 int
1557 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1558 {
1559 struct inode *ip;
1560 struct ufid ufh;
1561
1562 if (*fh_size < sizeof(struct ufid)) {
1563 *fh_size = sizeof(struct ufid);
1564 return E2BIG;
1565 }
1566 ip = VTOI(vp);
1567 *fh_size = sizeof(struct ufid);
1568 memset(&ufh, 0, sizeof(ufh));
1569 ufh.ufid_len = sizeof(struct ufid);
1570 ufh.ufid_ino = ip->i_number;
1571 ufh.ufid_gen = ip->i_gen;
1572 memcpy(fhp, &ufh, sizeof(ufh));
1573 return (0);
1574 }
1575
1576 void
1577 ffs_init(void)
1578 {
1579 if (ffs_initcount++ > 0)
1580 return;
1581
1582 #ifdef _LKM
1583 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0,
1584 "ffsinopl", &pool_allocator_nointr);
1585 pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
1586 "dino1pl", &pool_allocator_nointr);
1587 pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0,
1588 "dino2pl", &pool_allocator_nointr);
1589 #endif
1590 softdep_initialize();
1591 ufs_init();
1592 }
1593
1594 void
1595 ffs_reinit(void)
1596 {
1597 softdep_reinitialize();
1598 ufs_reinit();
1599 }
1600
1601 void
1602 ffs_done(void)
1603 {
1604 if (--ffs_initcount > 0)
1605 return;
1606
1607 /* XXX softdep cleanup ? */
1608 ufs_done();
1609 #ifdef _LKM
1610 pool_destroy(&ffs_dinode2_pool);
1611 pool_destroy(&ffs_dinode1_pool);
1612 pool_destroy(&ffs_inode_pool);
1613 #endif
1614 }
1615
1616 SYSCTL_SETUP(sysctl_vfs_ffs_setup, "sysctl vfs.ffs subtree setup")
1617 {
1618 #if 0
1619 extern int doasyncfree;
1620 #endif
1621 extern int ffs_log_changeopt;
1622
1623 sysctl_createv(clog, 0, NULL, NULL,
1624 CTLFLAG_PERMANENT,
1625 CTLTYPE_NODE, "vfs", NULL,
1626 NULL, 0, NULL, 0,
1627 CTL_VFS, CTL_EOL);
1628 sysctl_createv(clog, 0, NULL, NULL,
1629 CTLFLAG_PERMANENT,
1630 CTLTYPE_NODE, "ffs",
1631 SYSCTL_DESCR("Berkeley Fast File System"),
1632 NULL, 0, NULL, 0,
1633 CTL_VFS, 1, CTL_EOL);
1634
1635 /*
1636 * @@@ should we even bother with these first three?
1637 */
1638 sysctl_createv(clog, 0, NULL, NULL,
1639 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1640 CTLTYPE_INT, "doclusterread", NULL,
1641 sysctl_notavail, 0, NULL, 0,
1642 CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
1643 sysctl_createv(clog, 0, NULL, NULL,
1644 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1645 CTLTYPE_INT, "doclusterwrite", NULL,
1646 sysctl_notavail, 0, NULL, 0,
1647 CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
1648 sysctl_createv(clog, 0, NULL, NULL,
1649 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1650 CTLTYPE_INT, "doreallocblks", NULL,
1651 sysctl_notavail, 0, NULL, 0,
1652 CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
1653 #if 0
1654 sysctl_createv(clog, 0, NULL, NULL,
1655 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1656 CTLTYPE_INT, "doasyncfree",
1657 SYSCTL_DESCR("Release dirty blocks asynchronously"),
1658 NULL, 0, &doasyncfree, 0,
1659 CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
1660 #endif
1661 sysctl_createv(clog, 0, NULL, NULL,
1662 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1663 CTLTYPE_INT, "log_changeopt",
1664 SYSCTL_DESCR("Log changes in optimization strategy"),
1665 NULL, 0, &ffs_log_changeopt, 0,
1666 CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
1667 }
1668
1669 /*
1670 * Write a superblock and associated information back to disk.
1671 */
1672 int
1673 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1674 {
1675 struct fs *fs = mp->um_fs;
1676 struct buf *bp;
1677 int error = 0;
1678 u_int32_t saveflag;
1679
1680 bp = getblk(mp->um_devvp,
1681 fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
1682 (int)fs->fs_sbsize, 0, 0);
1683 saveflag = fs->fs_flags & FS_INTERNAL;
1684 fs->fs_flags &= ~FS_INTERNAL;
1685
1686 memcpy(bp->b_data, fs, fs->fs_sbsize);
1687
1688 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1689 #ifdef FFS_EI
1690 if (mp->um_flags & UFS_NEEDSWAP)
1691 ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
1692 #endif
1693 fs->fs_flags |= saveflag;
1694
1695 if (waitfor == MNT_WAIT)
1696 error = bwrite(bp);
1697 else
1698 bawrite(bp);
1699 return (error);
1700 }
1701
1702 int
1703 ffs_cgupdate(struct ufsmount *mp, int waitfor)
1704 {
1705 struct fs *fs = mp->um_fs;
1706 struct buf *bp;
1707 int blks;
1708 void *space;
1709 int i, size, error = 0, allerror = 0;
1710
1711 allerror = ffs_sbupdate(mp, waitfor);
1712 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1713 space = fs->fs_csp;
1714 for (i = 0; i < blks; i += fs->fs_frag) {
1715 size = fs->fs_bsize;
1716 if (i + fs->fs_frag > blks)
1717 size = (blks - i) * fs->fs_fsize;
1718 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1719 size, 0, 0);
1720 #ifdef FFS_EI
1721 if (mp->um_flags & UFS_NEEDSWAP)
1722 ffs_csum_swap((struct csum*)space,
1723 (struct csum*)bp->b_data, size);
1724 else
1725 #endif
1726 memcpy(bp->b_data, space, (u_int)size);
1727 space = (char *)space + size;
1728 if (waitfor == MNT_WAIT)
1729 error = bwrite(bp);
1730 else
1731 bawrite(bp);
1732 }
1733 if (!allerror && error)
1734 allerror = error;
1735 return (allerror);
1736 }
1737
1738 int
1739 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
1740 int attrnamespace, const char *attrname, struct lwp *l)
1741 {
1742 #ifdef UFS_EXTATTR
1743 /*
1744 * File-backed extended attributes are only supported on UFS1.
1745 * UFS2 has native extended attributes.
1746 */
1747 if (VFSTOUFS(mp)->um_fstype == UFS1)
1748 return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname,
1749 l));
1750 #endif
1751 return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname, l));
1752 }
1753