ffs_vfsops.c revision 1.52.2.1 1 /* $NetBSD: ffs_vfsops.c,v 1.52.2.1 1999/12/21 23:20:07 wrstuden 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 #if defined(_KERNEL) && !defined(_LKM)
39 #include "opt_ffs.h"
40 #include "opt_quota.h"
41 #include "opt_compat_netbsd.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 <vm/vm.h>
63 #include <sys/sysctl.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 extern struct lock ufs_hashlock;
78
79 int ffs_sbupdate __P((struct ufsmount *, int));
80
81 extern struct vnodeopv_desc ffs_vnodeop_opv_desc;
82 extern struct vnodeopv_desc ffs_specop_opv_desc;
83 extern struct vnodeopv_desc ffs_fifoop_opv_desc;
84
85 struct vnodeopv_desc *ffs_vnodeopv_descs[] = {
86 &ffs_vnodeop_opv_desc,
87 &ffs_specop_opv_desc,
88 &ffs_fifoop_opv_desc,
89 NULL,
90 };
91
92 struct vfsops ffs_vfsops = {
93 MOUNT_FFS,
94 ffs_mount,
95 ufs_start,
96 ffs_unmount,
97 ufs_root,
98 ufs_quotactl,
99 ffs_statfs,
100 ffs_sync,
101 ffs_vget,
102 ffs_fhtovp,
103 ffs_vptofh,
104 ffs_init,
105 ffs_sysctl,
106 ffs_mountroot,
107 ufs_check_export,
108 ffs_vnodeopv_descs,
109 };
110
111 struct pool ffs_inode_pool;
112
113 /*
114 * Called by main() when ffs is going to be mounted as root.
115 */
116
117 int
118 ffs_mountroot()
119 {
120 extern struct vnode *rootvp;
121 struct fs *fs;
122 struct mount *mp;
123 struct proc *p = curproc; /* XXX */
124 struct ufsmount *ump;
125 int error;
126
127 if (root_device->dv_class != DV_DISK)
128 return (ENODEV);
129
130 /*
131 * Get vnodes for rootdev.
132 */
133 if (bdevvp(rootdev, &rootvp))
134 panic("ffs_mountroot: can't setup bdevvp's");
135
136 if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
137 vrele(rootvp);
138 return (error);
139 }
140 if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
141 mp->mnt_op->vfs_refcount--;
142 vfs_unbusy(mp);
143 free(mp, M_MOUNT);
144 vrele(rootvp);
145 return (error);
146 }
147 simple_lock(&mountlist_slock);
148 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
149 simple_unlock(&mountlist_slock);
150 ump = VFSTOUFS(mp);
151 fs = ump->um_fs;
152 memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
153 (void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
154 (void)ffs_statfs(mp, &mp->mnt_stat, p);
155 vfs_unbusy(mp);
156 inittodr(fs->fs_time);
157 return (0);
158 }
159
160 /*
161 * VFS Operations.
162 *
163 * mount system call
164 */
165 int
166 ffs_mount(mp, path, data, ndp, p)
167 register struct mount *mp;
168 const char *path;
169 void *data;
170 struct nameidata *ndp;
171 struct proc *p;
172 {
173 struct vnode *devvp;
174 struct ufs_args args;
175 struct ufsmount *ump = NULL;
176 register struct fs *fs;
177 size_t size;
178 int error, flags;
179 mode_t accessmode;
180
181 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
182 if (error)
183 return (error);
184 /*
185 * If updating, check whether changing from read-only to
186 * read/write; if there is no device name, that's all we do.
187 */
188 if (mp->mnt_flag & MNT_UPDATE) {
189 ump = VFSTOUFS(mp);
190 fs = ump->um_fs;
191 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
192 flags = WRITECLOSE;
193 if (mp->mnt_flag & MNT_FORCE)
194 flags |= FORCECLOSE;
195 error = ffs_flushfiles(mp, flags, p);
196 if (error == 0 &&
197 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
198 fs->fs_clean & FS_WASCLEAN) {
199 fs->fs_clean = FS_ISCLEAN;
200 (void) ffs_sbupdate(ump, MNT_WAIT);
201 }
202 if (error)
203 return (error);
204 fs->fs_ronly = 1;
205 }
206 if (mp->mnt_flag & MNT_RELOAD) {
207 error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p);
208 if (error)
209 return (error);
210 }
211 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
212 /*
213 * If upgrade to read-write by non-root, then verify
214 * that user has necessary permissions on the device.
215 */
216 if (p->p_ucred->cr_uid != 0) {
217 devvp = ump->um_devvp;
218 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
219 error = VOP_ACCESS(devvp, VREAD | VWRITE,
220 p->p_ucred, p);
221 VOP_UNLOCK(devvp, 0);
222 if (error)
223 return (error);
224 }
225 fs->fs_ronly = 0;
226 fs->fs_clean <<= 1;
227 fs->fs_fmod = 1;
228 }
229 if (args.fspec == 0) {
230 /*
231 * Process export requests.
232 */
233 return (vfs_export(mp, &ump->um_export, &args.export));
234 }
235 }
236 /*
237 * Not an update, or updating the name: look up the name
238 * and verify that it refers to a sensible block device.
239 */
240 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
241 if ((error = namei(ndp)) != 0)
242 return (error);
243 devvp = ndp->ni_vp;
244
245 if (devvp->v_type != VBLK) {
246 vrele(devvp);
247 return (ENOTBLK);
248 }
249 if (major(devvp->v_rdev) >= nblkdev) {
250 vrele(devvp);
251 return (ENXIO);
252 }
253 /*
254 * If mount by non-root, then verify that user has necessary
255 * permissions on the device.
256 */
257 if (p->p_ucred->cr_uid != 0) {
258 accessmode = VREAD;
259 if ((mp->mnt_flag & MNT_RDONLY) == 0)
260 accessmode |= VWRITE;
261 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
262 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
263 VOP_UNLOCK(devvp, 0);
264 if (error) {
265 vrele(devvp);
266 return (error);
267 }
268 }
269 if ((mp->mnt_flag & MNT_UPDATE) == 0)
270 error = ffs_mountfs(devvp, mp, p);
271 else {
272 if (devvp != ump->um_devvp)
273 error = EINVAL; /* needs translation */
274 else
275 vrele(devvp);
276 }
277 if (error) {
278 vrele(devvp);
279 return (error);
280 }
281 ump = VFSTOUFS(mp);
282 fs = ump->um_fs;
283 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
284 memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
285 memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
286 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
287 &size);
288 memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
289 if (fs->fs_fmod != 0) { /* XXX */
290 fs->fs_fmod = 0;
291 if (fs->fs_clean & FS_WASCLEAN)
292 fs->fs_time = time.tv_sec;
293 else
294 printf("%s: file system not clean (fs_flags=%x); please fsck(8)\n",
295 mp->mnt_stat.f_mntfromname, fs->fs_clean);
296 (void) ffs_cgupdate(ump, MNT_WAIT);
297 }
298 return (0);
299 }
300
301 /*
302 * Reload all incore data for a filesystem (used after running fsck on
303 * the root filesystem and finding things to fix). The filesystem must
304 * be mounted read-only.
305 *
306 * Things to do to update the mount:
307 * 1) invalidate all cached meta-data.
308 * 2) re-read superblock from disk.
309 * 3) re-read summary information from disk.
310 * 4) invalidate all inactive vnodes.
311 * 5) invalidate all cached file data.
312 * 6) re-read inode data for all active vnodes.
313 */
314 int
315 ffs_reload(mountp, cred, p)
316 register struct mount *mountp;
317 struct ucred *cred;
318 struct proc *p;
319 {
320 register struct vnode *vp, *nvp, *devvp;
321 struct inode *ip;
322 struct buf *bp;
323 struct fs *fs, *newfs;
324 int i, blks, size, error;
325 int32_t *lp;
326 caddr_t cp;
327
328 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
329 return (EINVAL);
330 /*
331 * Step 1: invalidate all cached meta-data.
332 */
333 devvp = VFSTOUFS(mountp)->um_devvp;
334 if (vinvalbuf(devvp, 0, cred, p, 0, 0))
335 panic("ffs_reload: dirty1");
336 /*
337 * Step 2: re-read superblock from disk.
338 */
339 #if 0
340 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
341 size = DEV_BSIZE;
342 else
343 size = dpart.disklab->d_secsize;
344 #endif
345 if ((mountp->mnt_bshift = devvp->v_specbshift) <= 0)
346 return (EINVAL);
347
348 error = bread(devvp, (ufs_daddr_t)(SBOFF >> devvp->v_specbshift),
349 SBSIZE, NOCRED, &bp);
350 if (error) {
351 brelse(bp);
352 return (error);
353 }
354 fs = VFSTOUFS(mountp)->um_fs;
355 newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
356 memcpy(newfs, bp->b_data, fs->fs_sbsize);
357 #ifdef FFS_EI
358 if (VFSTOUFS(mountp)->um_flags & UFS_NEEDSWAP)
359 ffs_sb_swap((struct fs*)bp->b_data, newfs, 0);
360 #endif
361 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
362 newfs->fs_bsize < sizeof(struct fs)) {
363 brelse(bp);
364 free(newfs, M_UFSMNT);
365 return (EIO); /* XXX needs translation */
366 }
367 /*
368 * Copy pointer fields back into superblock before copying in XXX
369 * new superblock. These should really be in the ufsmount. XXX
370 * Note that important parameters (eg fs_ncg) are unchanged.
371 */
372 memcpy(&newfs->fs_csp[0], &fs->fs_csp[0], sizeof(fs->fs_csp));
373 newfs->fs_maxcluster = fs->fs_maxcluster;
374 newfs->fs_fsbtodb = fs->fs_fsbtodb;
375 memcpy(fs, newfs, (u_int)fs->fs_sbsize);
376 if (fs->fs_sbsize < SBSIZE)
377 bp->b_flags |= B_INVAL;
378 brelse(bp);
379 free(newfs, M_UFSMNT);
380 mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
381 ffs_oldfscompat(fs);
382 /*
383 * Step 3: re-read summary information from disk.
384 */
385 blks = howmany(fs->fs_cssize, fs->fs_fsize);
386 for (i = 0; i < blks; i += fs->fs_frag) {
387 size = fs->fs_bsize;
388 if (i + fs->fs_frag > blks)
389 size = (blks - i) * fs->fs_fsize;
390 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
391 NOCRED, &bp);
392 if (error) {
393 brelse(bp);
394 return (error);
395 }
396 #ifdef FFS_EI
397 if (UFS_MPNEEDSWAP(mountp))
398 ffs_csum_swap((struct csum*)bp->b_data,
399 (struct csum*)fs->fs_csp[fragstoblks(fs, i)], size);
400 else
401 #endif
402 memcpy(fs->fs_csp[fragstoblks(fs, i)], bp->b_data,
403 (size_t)size);
404 brelse(bp);
405 }
406 /*
407 * We no longer know anything about clusters per cylinder group.
408 */
409 if (fs->fs_contigsumsize > 0) {
410 lp = fs->fs_maxcluster;
411 for (i = 0; i < fs->fs_ncg; i++)
412 *lp++ = fs->fs_contigsumsize;
413 }
414
415 loop:
416 simple_lock(&mntvnode_slock);
417 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
418 if (vp->v_mount != mountp) {
419 simple_unlock(&mntvnode_slock);
420 goto loop;
421 }
422 nvp = vp->v_mntvnodes.le_next;
423 /*
424 * Step 4: invalidate all inactive vnodes.
425 */
426 if (vrecycle(vp, &mntvnode_slock, p))
427 goto loop;
428 /*
429 * Step 5: invalidate all cached file data.
430 */
431 simple_lock(&vp->v_interlock);
432 simple_unlock(&mntvnode_slock);
433 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
434 goto loop;
435 if (vinvalbuf(vp, 0, cred, p, 0, 0))
436 panic("ffs_reload: dirty2");
437 /*
438 * Step 6: re-read inode data for all active vnodes.
439 */
440 ip = VTOI(vp);
441 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
442 (int)fs->fs_bsize, NOCRED, &bp);
443 if (error) {
444 brelse(bp);
445 vput(vp);
446 return (error);
447 }
448 cp = (caddr_t)bp->b_data +
449 (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
450 #ifdef FFS_EI
451 if (UFS_MPNEEDSWAP(mountp))
452 ffs_dinode_swap((struct dinode *)cp,
453 &ip->i_din.ffs_din);
454 else
455 #endif
456 memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
457 brelse(bp);
458 vput(vp);
459 simple_lock(&mntvnode_slock);
460 }
461 simple_unlock(&mntvnode_slock);
462 return (0);
463 }
464
465 /*
466 * Common code for mount and mountroot
467 */
468 int
469 ffs_mountfs(devvp, mp, p)
470 register struct vnode *devvp;
471 struct mount *mp;
472 struct proc *p;
473 {
474 struct ufsmount *ump;
475 struct buf *bp;
476 struct fs *fs;
477 dev_t dev;
478 caddr_t base, space;
479 int blks;
480 int error, i, size, ronly;
481 #ifdef FFS_EI
482 int needswap;
483 #endif
484 int32_t *lp;
485 struct ucred *cred;
486 extern struct vnode *rootvp;
487 u_int64_t maxfilesize; /* XXX */
488 u_int32_t sbsize;
489
490 dev = devvp->v_rdev;
491 cred = p ? p->p_ucred : NOCRED;
492 /*
493 * Disallow multiple mounts of the same device.
494 * Disallow mounting of a device that is currently in use
495 * (except for root, which might share swap device for miniroot).
496 * Flush out any old buffers remaining from a previous use.
497 */
498 if ((error = vfs_mountedon(devvp)) != 0)
499 return (error);
500 if (vcount(devvp) > 1 && devvp != rootvp)
501 return (EBUSY);
502 if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
503 return (error);
504
505 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
506 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
507 if (error)
508 return (error);
509 #if 0
510 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
511 size = DEV_BSIZE;
512 else
513 size = dpart.disklab->d_secsize;
514 #endif
515 if ((mp->mnt_bshift = devvp->v_specbshift) <= 0)
516 return (EINVAL);
517
518 bp = NULL;
519 ump = NULL;
520 error = bread(devvp, (ufs_daddr_t)(SBOFF >> mp->mnt_bshift), SBSIZE,
521 cred, &bp);
522 if (error)
523 goto out;
524
525 fs = (struct fs*)bp->b_data;
526 if (fs->fs_magic == FS_MAGIC) {
527 sbsize = fs->fs_sbsize;
528 #ifdef FFS_EI
529 needswap = 0;
530 } else if (fs->fs_magic == bswap32(FS_MAGIC)) {
531 sbsize = bswap32(fs->fs_sbsize);
532 needswap = 1;
533 #endif
534 } else {
535 error = EINVAL;
536 goto out;
537 }
538 if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs)) {
539 error = EINVAL;
540 goto out;
541 }
542
543 fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
544 memcpy(fs, bp->b_data, sbsize);
545 #ifdef FFS_EI
546 if (needswap)
547 ffs_sb_swap((struct fs*)bp->b_data, fs, 0);
548 #endif
549 if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
550 error = EINVAL;
551 goto out;
552 }
553 /* make sure cylinder group summary area is a reasonable size. */
554 if (fs->fs_cgsize == 0 || fs->fs_cpg == 0 ||
555 fs->fs_ncg > fs->fs_ncyl / fs->fs_cpg + 1 ||
556 fs->fs_cssize >
557 fragroundup(fs, fs->fs_ncg * sizeof(struct csum))) {
558 error = EINVAL; /* XXX needs translation */
559 goto out2;
560 }
561 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
562 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
563 error = EROFS; /* XXX what should be returned? */
564 goto out2;
565 }
566 /* XXX bread assumes b_blkno in DEV_BSIZE unit. Calculate fsbtosb */
567 /* XXX wrs - no, it doesn't. All we need to do is recalculate
568 * fs_fsbtodb based on our current media. */
569
570 /* Make sure at most one fs frag per disk block */
571 if (fs->fs_fshift < mp->mnt_bshift) {
572 error = EINVAL; /* XXX needs translation */
573 goto out2;
574 }
575 fs->fs_fsbtodb = fs->fs_fshift - mp->mnt_bshift;
576 #if 0
577 ssize = fs->fs_fsize / fs->fs_nspf;
578 fs->fs_fsbtosb = fs->fs_fsbtodb;
579 if (ssize >= size) {
580 ssize = ssize / size;
581 for (i = 0; ssize > 1; ssize >>= 1)
582 i ++;
583 fs->fs_fsbtosb += i;
584 } else {
585 ssize = size / ssize;
586 for (i = 0; ssize > 1; ssize >>= 1)
587 i ++;
588 fs->fs_fsbtosb -= i;
589 }
590 #endif
591 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
592 memset((caddr_t)ump, 0, sizeof *ump);
593 ump->um_fs = fs;
594 if (fs->fs_sbsize < SBSIZE)
595 bp->b_flags |= B_INVAL;
596 brelse(bp);
597 bp = NULL;
598 fs->fs_ronly = ronly;
599 if (ronly == 0) {
600 fs->fs_clean <<= 1;
601 fs->fs_fmod = 1;
602 }
603 size = fs->fs_cssize;
604 blks = howmany(size, fs->fs_fsize);
605 if (fs->fs_contigsumsize > 0)
606 size += fs->fs_ncg * sizeof(int32_t);
607 base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
608 for (i = 0; i < blks; i += fs->fs_frag) {
609 size = fs->fs_bsize;
610 if (i + fs->fs_frag > blks)
611 size = (blks - i) * fs->fs_fsize;
612 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
613 cred, &bp);
614 if (error) {
615 free(base, M_UFSMNT);
616 goto out2;
617 }
618 #ifdef FFS_EI
619 if (needswap)
620 ffs_csum_swap((struct csum*)bp->b_data,
621 (struct csum*)space, size);
622 else
623 #endif
624 memcpy(space, bp->b_data, (u_int)size);
625
626 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
627 space += size;
628 brelse(bp);
629 bp = NULL;
630 }
631 if (fs->fs_contigsumsize > 0) {
632 fs->fs_maxcluster = lp = (int32_t *)space;
633 for (i = 0; i < fs->fs_ncg; i++)
634 *lp++ = fs->fs_contigsumsize;
635 }
636 mp->mnt_data = (qaddr_t)ump;
637 mp->mnt_stat.f_fsid.val[0] = (long)dev;
638 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
639 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
640 mp->mnt_flag |= MNT_LOCAL;
641 #ifdef FFS_EI
642 if (needswap)
643 ump->um_flags |= UFS_NEEDSWAP;
644 #endif
645 ump->um_mountp = mp;
646 ump->um_dev = dev;
647 ump->um_devvp = devvp;
648 ump->um_nindir = fs->fs_nindir;
649 ump->um_bptrtodb = fs->fs_fsbtodb;
650 ump->um_seqinc = fs->fs_frag;
651 for (i = 0; i < MAXQUOTAS; i++)
652 ump->um_quotas[i] = NULLVP;
653 devvp->v_specflags |= SI_MOUNTEDON;
654 ffs_oldfscompat(fs);
655 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
656 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1; /* XXX */
657 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
658 fs->fs_maxfilesize = maxfilesize; /* XXX */
659 return (0);
660 out2:
661 free(fs, M_UFSMNT);
662 out:
663 if (bp)
664 brelse(bp);
665 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
666 if (ump) {
667 free(ump, M_UFSMNT);
668 mp->mnt_data = (qaddr_t)0;
669 }
670 return (error);
671 }
672
673 /*
674 * Sanity checks for old file systems.
675 *
676 * XXX - goes away some day.
677 */
678 int
679 ffs_oldfscompat(fs)
680 struct fs *fs;
681 {
682 int i;
683
684 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
685 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
686 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
687 fs->fs_nrpos = 8; /* XXX */
688 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
689 u_int64_t sizepb = fs->fs_bsize; /* XXX */
690 /* XXX */
691 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
692 for (i = 0; i < NIADDR; i++) { /* XXX */
693 sizepb *= NINDIR(fs); /* XXX */
694 fs->fs_maxfilesize += sizepb; /* XXX */
695 } /* XXX */
696 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
697 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
698 } /* XXX */
699 return (0);
700 }
701
702 /*
703 * unmount system call
704 */
705 int
706 ffs_unmount(mp, mntflags, p)
707 struct mount *mp;
708 int mntflags;
709 struct proc *p;
710 {
711 register struct ufsmount *ump;
712 register struct fs *fs;
713 int error, flags;
714
715 flags = 0;
716 if (mntflags & MNT_FORCE)
717 flags |= FORCECLOSE;
718 if ((error = ffs_flushfiles(mp, flags, p)) != 0)
719 return (error);
720 ump = VFSTOUFS(mp);
721 fs = ump->um_fs;
722 if (fs->fs_ronly == 0 &&
723 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
724 fs->fs_clean & FS_WASCLEAN) {
725 fs->fs_clean = FS_ISCLEAN;
726 (void) ffs_sbupdate(ump, MNT_WAIT);
727 }
728 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
729 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
730 NOCRED, p);
731 vrele(ump->um_devvp);
732 free(fs->fs_csp[0], M_UFSMNT);
733 free(fs, M_UFSMNT);
734 free(ump, M_UFSMNT);
735 mp->mnt_data = (qaddr_t)0;
736 mp->mnt_flag &= ~MNT_LOCAL;
737 return (error);
738 }
739
740 /*
741 * Flush out all the files in a filesystem.
742 */
743 int
744 ffs_flushfiles(mp, flags, p)
745 register struct mount *mp;
746 int flags;
747 struct proc *p;
748 {
749 extern int doforce;
750 register struct ufsmount *ump;
751 int error;
752
753 if (!doforce)
754 flags &= ~FORCECLOSE;
755 ump = VFSTOUFS(mp);
756 #ifdef QUOTA
757 if (mp->mnt_flag & MNT_QUOTA) {
758 int i;
759 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
760 return (error);
761 for (i = 0; i < MAXQUOTAS; i++) {
762 if (ump->um_quotas[i] == NULLVP)
763 continue;
764 quotaoff(p, mp, i);
765 }
766 /*
767 * Here we fall through to vflush again to ensure
768 * that we have gotten rid of all the system vnodes.
769 */
770 }
771 #endif
772 error = vflush(mp, NULLVP, flags);
773 return (error);
774 }
775
776 /*
777 * Get file system statistics.
778 */
779 int
780 ffs_statfs(mp, sbp, p)
781 struct mount *mp;
782 register struct statfs *sbp;
783 struct proc *p;
784 {
785 register struct ufsmount *ump;
786 register struct fs *fs;
787
788 ump = VFSTOUFS(mp);
789 fs = ump->um_fs;
790 if (fs->fs_magic != FS_MAGIC)
791 panic("ffs_statfs");
792 #ifdef COMPAT_09
793 sbp->f_type = 1;
794 #else
795 sbp->f_type = 0;
796 #endif
797 sbp->f_bsize = fs->fs_fsize;
798 sbp->f_iosize = fs->fs_bsize;
799 sbp->f_blocks = fs->fs_dsize;
800 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
801 fs->fs_cstotal.cs_nffree;
802 sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
803 (100 - fs->fs_minfree) / (u_int64_t) 100) -
804 (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
805 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
806 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
807 if (sbp != &mp->mnt_stat) {
808 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
809 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
810 }
811 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
812 return (0);
813 }
814
815 /*
816 * Go through the disk queues to initiate sandbagged IO;
817 * go through the inodes to write those that have been modified;
818 * initiate the writing of the super block if it has been modified.
819 *
820 * Note: we are always called with the filesystem marked `MPBUSY'.
821 */
822 int
823 ffs_sync(mp, waitfor, cred, p)
824 struct mount *mp;
825 int waitfor;
826 struct ucred *cred;
827 struct proc *p;
828 {
829 struct vnode *vp, *nvp;
830 struct inode *ip;
831 struct ufsmount *ump = VFSTOUFS(mp);
832 struct fs *fs;
833 int error, allerror = 0;
834
835 fs = ump->um_fs;
836 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
837 printf("fs = %s\n", fs->fs_fsmnt);
838 panic("update: rofs mod");
839 }
840 /*
841 * Write back each (modified) inode.
842 */
843 simple_lock(&mntvnode_slock);
844 loop:
845 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
846 /*
847 * If the vnode that we are about to sync is no longer
848 * associated with this mount point, start over.
849 */
850 if (vp->v_mount != mp)
851 goto loop;
852 simple_lock(&vp->v_interlock);
853 nvp = vp->v_mntvnodes.le_next;
854 ip = VTOI(vp);
855 if ((ip->i_flag &
856 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
857 vp->v_dirtyblkhd.lh_first == NULL) {
858 simple_unlock(&vp->v_interlock);
859 continue;
860 }
861 simple_unlock(&mntvnode_slock);
862 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
863 if (error) {
864 simple_lock(&mntvnode_slock);
865 if (error == ENOENT)
866 goto loop;
867 continue;
868 }
869 if ((error = VOP_FSYNC(vp, cred,
870 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
871 allerror = error;
872 vput(vp);
873 simple_lock(&mntvnode_slock);
874 }
875 simple_unlock(&mntvnode_slock);
876 /*
877 * Force stale file system control information to be flushed.
878 */
879 if ((error = VOP_FSYNC(ump->um_devvp, cred,
880 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
881 allerror = error;
882 #ifdef QUOTA
883 qsync(mp);
884 #endif
885 /*
886 * Write back modified superblock.
887 */
888 if (fs->fs_fmod != 0) {
889 fs->fs_fmod = 0;
890 fs->fs_time = time.tv_sec;
891 allerror = ffs_cgupdate(ump, waitfor);
892 }
893 return (allerror);
894 }
895
896 /*
897 * Look up a FFS dinode number to find its incore vnode, otherwise read it
898 * in from disk. If it is in core, wait for the lock bit to clear, then
899 * return the inode locked. Detection and handling of mount points must be
900 * done by the calling routine.
901 */
902 int
903 ffs_vget(mp, ino, vpp)
904 struct mount *mp;
905 ino_t ino;
906 struct vnode **vpp;
907 {
908 struct fs *fs;
909 struct inode *ip;
910 struct ufsmount *ump;
911 struct buf *bp;
912 struct vnode *vp;
913 dev_t dev;
914 int error;
915 caddr_t cp;
916
917 ump = VFSTOUFS(mp);
918 dev = ump->um_dev;
919 do {
920 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
921 return (0);
922 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
923
924 /* Allocate a new vnode/inode. */
925 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
926 *vpp = NULL;
927 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
928 return (error);
929 }
930 /*
931 * XXX MFS ends up here, too, to allocate an inode. Should we
932 * XXX create another pool for MFS inodes?
933 */
934 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
935 memset((caddr_t)ip, 0, sizeof(struct inode));
936 vp->v_data = ip;
937 ip->i_vnode = vp;
938 ip->i_fs = fs = ump->um_fs;
939 ip->i_dev = dev;
940 ip->i_number = ino;
941 #ifdef QUOTA
942 {
943 int i;
944
945 for (i = 0; i < MAXQUOTAS; i++)
946 ip->i_dquot[i] = NODQUOT;
947 }
948 #endif
949 /*
950 * Put it onto its hash chain and lock it so that other requests for
951 * this inode will block if they arrive while we are sleeping waiting
952 * for old data structures to be purged or for the contents of the
953 * disk portion of this inode to be read.
954 */
955 ufs_ihashins(ip);
956 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
957
958 /* Read in the disk contents for the inode, copy into the inode. */
959 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
960 (int)fs->fs_bsize, NOCRED, &bp);
961 if (error) {
962 /*
963 * The inode does not contain anything useful, so it would
964 * be misleading to leave it on its hash chain. With mode
965 * still zero, it will be unlinked and returned to the free
966 * list by vput().
967 */
968 vput(vp);
969 brelse(bp);
970 *vpp = NULL;
971 return (error);
972 }
973 cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
974 #ifdef FFS_EI
975 if (UFS_MPNEEDSWAP(mp))
976 ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
977 else
978 #endif
979 memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
980 brelse(bp);
981
982 /*
983 * Initialize the vnode from the inode, check for aliases.
984 * Note that the underlying vnode may have changed.
985 */
986 error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
987 if (error) {
988 vput(vp);
989 *vpp = NULL;
990 return (error);
991 }
992 /*
993 * Finish inode initialization now that aliasing has been resolved.
994 */
995 ip->i_devvp = ump->um_devvp;
996 VREF(ip->i_devvp);
997 /*
998 * Ensure that uid and gid are correct. This is a temporary
999 * fix until fsck has been changed to do the update.
1000 */
1001 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1002 ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid; /* XXX */
1003 ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid; /* XXX */
1004 } /* XXX */
1005
1006 *vpp = vp;
1007 return (0);
1008 }
1009
1010 /*
1011 * File handle to vnode
1012 *
1013 * Have to be really careful about stale file handles:
1014 * - check that the inode number is valid
1015 * - call ffs_vget() to get the locked inode
1016 * - check for an unallocated inode (i_mode == 0)
1017 * - check that the given client host has export rights and return
1018 * those rights via. exflagsp and credanonp
1019 */
1020 int
1021 ffs_fhtovp(mp, fhp, vpp)
1022 register struct mount *mp;
1023 struct fid *fhp;
1024 struct vnode **vpp;
1025 {
1026 register struct ufid *ufhp;
1027 struct fs *fs;
1028
1029 ufhp = (struct ufid *)fhp;
1030 fs = VFSTOUFS(mp)->um_fs;
1031 if (ufhp->ufid_ino < ROOTINO ||
1032 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1033 return (ESTALE);
1034 return (ufs_fhtovp(mp, ufhp, vpp));
1035 }
1036
1037 /*
1038 * Vnode pointer to File handle
1039 */
1040 /* ARGSUSED */
1041 int
1042 ffs_vptofh(vp, fhp)
1043 struct vnode *vp;
1044 struct fid *fhp;
1045 {
1046 register struct inode *ip;
1047 register struct ufid *ufhp;
1048
1049 ip = VTOI(vp);
1050 ufhp = (struct ufid *)fhp;
1051 ufhp->ufid_len = sizeof(struct ufid);
1052 ufhp->ufid_ino = ip->i_number;
1053 ufhp->ufid_gen = ip->i_ffs_gen;
1054 return (0);
1055 }
1056
1057 void
1058 ffs_init()
1059 {
1060 ufs_init();
1061
1062 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
1063 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FFSNODE);
1064 }
1065
1066 int
1067 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1068 int *name;
1069 u_int namelen;
1070 void *oldp;
1071 size_t *oldlenp;
1072 void *newp;
1073 size_t newlen;
1074 struct proc *p;
1075 {
1076 extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
1077
1078 /* all sysctl names at this level are terminal */
1079 if (namelen != 1)
1080 return (ENOTDIR); /* overloaded */
1081
1082 switch (name[0]) {
1083 case FFS_CLUSTERREAD:
1084 return (sysctl_int(oldp, oldlenp, newp, newlen,
1085 &doclusterread));
1086 case FFS_CLUSTERWRITE:
1087 return (sysctl_int(oldp, oldlenp, newp, newlen,
1088 &doclusterwrite));
1089 case FFS_REALLOCBLKS:
1090 return (sysctl_int(oldp, oldlenp, newp, newlen,
1091 &doreallocblks));
1092 case FFS_ASYNCFREE:
1093 return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
1094 default:
1095 return (EOPNOTSUPP);
1096 }
1097 /* NOTREACHED */
1098 }
1099
1100 /*
1101 * Write a superblock and associated information back to disk.
1102 */
1103 int
1104 ffs_sbupdate(mp, waitfor)
1105 struct ufsmount *mp;
1106 int waitfor;
1107 {
1108 register struct fs *fs = mp->um_fs;
1109 register struct buf *bp;
1110 int i, error = 0;
1111 int32_t saved_nrpos = fs->fs_nrpos;
1112 int64_t saved_qbmask = fs->fs_qbmask;
1113 int64_t saved_qfmask = fs->fs_qfmask;
1114 u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
1115
1116 /* Restore compatibility to old file systems. XXX */
1117 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1118 fs->fs_nrpos = -1; /* XXX */
1119 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1120 int32_t *lp, tmp; /* XXX */
1121 /* XXX */
1122 lp = (int32_t *)&fs->fs_qbmask; /* XXX nuke qfmask too */
1123 tmp = lp[4]; /* XXX */
1124 for (i = 4; i > 0; i--) /* XXX */
1125 lp[i] = lp[i-1]; /* XXX */
1126 lp[0] = tmp; /* XXX */
1127 } /* XXX */
1128 fs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1129
1130 bp = getblk(mp->um_devvp, SBOFF >> mp->um_mountp->mnt_bshift,
1131 (int)fs->fs_sbsize, 0, 0);
1132 memcpy(bp->b_data, fs, fs->fs_sbsize);
1133 #ifdef FFS_EI
1134 if (mp->um_flags & UFS_NEEDSWAP)
1135 ffs_sb_swap(fs, (struct fs*)bp->b_data, 1);
1136 #endif
1137
1138 fs->fs_nrpos = saved_nrpos; /* XXX */
1139 fs->fs_qbmask = saved_qbmask; /* XXX */
1140 fs->fs_qfmask = saved_qfmask; /* XXX */
1141 fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
1142
1143 if (waitfor == MNT_WAIT)
1144 error = bwrite(bp);
1145 else
1146 bawrite(bp);
1147 return (error);
1148 }
1149
1150 int
1151 ffs_cgupdate(mp, waitfor)
1152 struct ufsmount *mp;
1153 int waitfor;
1154 {
1155 register struct fs *fs = mp->um_fs;
1156 register struct buf *bp;
1157 int blks;
1158 caddr_t space;
1159 int i, size, error = 0, allerror = 0;
1160
1161 allerror = ffs_sbupdate(mp, waitfor);
1162 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1163 space = (caddr_t)fs->fs_csp[0];
1164 for (i = 0; i < blks; i += fs->fs_frag) {
1165 size = fs->fs_bsize;
1166 if (i + fs->fs_frag > blks)
1167 size = (blks - i) * fs->fs_fsize;
1168 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1169 size, 0, 0);
1170 #ifdef FFS_EI
1171 if (mp->um_flags & UFS_NEEDSWAP)
1172 ffs_csum_swap((struct csum*)space,
1173 (struct csum*)bp->b_data, size);
1174 else
1175 #endif
1176 memcpy(bp->b_data, space, (u_int)size);
1177 space += size;
1178 if (waitfor == MNT_WAIT)
1179 error = bwrite(bp);
1180 else
1181 bawrite(bp);
1182 }
1183 if (!allerror && error)
1184 allerror = error;
1185 return (allerror);
1186 }
1187