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