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