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