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