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