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