ffs_vfsops.c revision 1.40 1 /* $NetBSD: ffs_vfsops.c,v 1.40 1998/06/24 20:58:48 sommerfe 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 extern struct vnodeopv_desc ffs_fifoop_opv_desc;
81
82 struct vnodeopv_desc *ffs_vnodeopv_descs[] = {
83 &ffs_vnodeop_opv_desc,
84 &ffs_specop_opv_desc,
85 &ffs_fifoop_opv_desc,
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)],
383 (size_t)size);
384 brelse(bp);
385 }
386 /*
387 * We no longer know anything about clusters per cylinder group.
388 */
389 if (fs->fs_contigsumsize > 0) {
390 lp = fs->fs_maxcluster;
391 for (i = 0; i < fs->fs_ncg; i++)
392 *lp++ = fs->fs_contigsumsize;
393 }
394
395 loop:
396 simple_lock(&mntvnode_slock);
397 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
398 if (vp->v_mount != mountp) {
399 simple_unlock(&mntvnode_slock);
400 goto loop;
401 }
402 nvp = vp->v_mntvnodes.le_next;
403 /*
404 * Step 4: invalidate all inactive vnodes.
405 */
406 if (vrecycle(vp, &mntvnode_slock, p))
407 goto loop;
408 /*
409 * Step 5: invalidate all cached file data.
410 */
411 simple_lock(&vp->v_interlock);
412 simple_unlock(&mntvnode_slock);
413 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
414 goto loop;
415 if (vinvalbuf(vp, 0, cred, p, 0, 0))
416 panic("ffs_reload: dirty2");
417 /*
418 * Step 6: re-read inode data for all active vnodes.
419 */
420 ip = VTOI(vp);
421 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
422 (int)fs->fs_bsize, NOCRED, &bp);
423 if (error) {
424 vput(vp);
425 return (error);
426 }
427 #ifdef FFS_EI
428 if (UFS_MPNEEDSWAP(mountp))
429 ffs_dinode_swap((struct dinode *)bp->b_data +
430 ino_to_fsbo(fs, ip->i_number), &ip->i_din.ffs_din);
431 else
432 #endif
433 ip->i_din.ffs_din = *((struct dinode *)bp->b_data +
434 ino_to_fsbo(fs, ip->i_number));
435 brelse(bp);
436 vput(vp);
437 simple_lock(&mntvnode_slock);
438 }
439 simple_unlock(&mntvnode_slock);
440 return (0);
441 }
442
443 /*
444 * Common code for mount and mountroot
445 */
446 int
447 ffs_mountfs(devvp, mp, p)
448 register struct vnode *devvp;
449 struct mount *mp;
450 struct proc *p;
451 {
452 struct ufsmount *ump;
453 struct buf *bp;
454 struct fs *fs;
455 dev_t dev;
456 struct partinfo dpart;
457 caddr_t base, space;
458 int blks;
459 int error, i, size, ronly, needswap;
460 int32_t *lp;
461 struct ucred *cred;
462 extern struct vnode *rootvp;
463 u_int64_t maxfilesize; /* XXX */
464 u_int32_t sbsize;
465
466 dev = devvp->v_rdev;
467 cred = p ? p->p_ucred : NOCRED;
468 /*
469 * Disallow multiple mounts of the same device.
470 * Disallow mounting of a device that is currently in use
471 * (except for root, which might share swap device for miniroot).
472 * Flush out any old buffers remaining from a previous use.
473 */
474 if ((error = vfs_mountedon(devvp)) != 0)
475 return (error);
476 if (vcount(devvp) > 1 && devvp != rootvp)
477 return (EBUSY);
478 if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
479 return (error);
480
481 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
482 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
483 if (error)
484 return (error);
485 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
486 size = DEV_BSIZE;
487 else
488 size = dpart.disklab->d_secsize;
489
490 bp = NULL;
491 ump = NULL;
492 error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, cred, &bp);
493 if (error)
494 goto out;
495
496 fs = (struct fs*)bp->b_data;
497 if (fs->fs_magic == FS_MAGIC) {
498 needswap = 0;
499 sbsize = fs->fs_sbsize;
500 #ifdef FFS_EI
501 } else if (fs->fs_magic == bswap32(FS_MAGIC)) {
502 needswap = 1;
503 sbsize = bswap32(fs->fs_sbsize);
504 #endif
505 } else {
506 error = EINVAL;
507 goto out;
508
509 }
510
511 fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
512 bcopy(bp->b_data, fs, sbsize);
513 #ifdef FFS_EI
514 if (needswap)
515 ffs_sb_swap((struct fs*)bp->b_data, fs, 0);
516 #endif
517
518 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
519 fs->fs_bsize < sizeof(struct fs)) {
520 error = EINVAL; /* XXX needs translation */
521 goto out2;
522 }
523 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
524 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
525 error = EROFS; /* XXX what should be returned? */
526 goto out2;
527 }
528 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
529 bzero((caddr_t)ump, sizeof *ump);
530 ump->um_fs = fs;
531 if (fs->fs_sbsize < SBSIZE)
532 bp->b_flags |= B_INVAL;
533 brelse(bp);
534 bp = NULL;
535 fs->fs_ronly = ronly;
536 if (ronly == 0) {
537 fs->fs_clean <<= 1;
538 fs->fs_fmod = 1;
539 }
540 size = fs->fs_cssize;
541 blks = howmany(size, fs->fs_fsize);
542 if (fs->fs_contigsumsize > 0)
543 size += fs->fs_ncg * sizeof(int32_t);
544 base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
545 for (i = 0; i < blks; i += fs->fs_frag) {
546 size = fs->fs_bsize;
547 if (i + fs->fs_frag > blks)
548 size = (blks - i) * fs->fs_fsize;
549 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
550 cred, &bp);
551 if (error) {
552 free(base, M_UFSMNT);
553 goto out2;
554 }
555 #ifdef FFS_EI
556 if (needswap)
557 ffs_csum_swap((struct csum*)bp->b_data,
558 (struct csum*)space, size);
559 else
560 #endif
561 bcopy(bp->b_data, space, (u_int)size);
562
563 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
564 space += size;
565 brelse(bp);
566 bp = NULL;
567 }
568 if (fs->fs_contigsumsize > 0) {
569 fs->fs_maxcluster = lp = (int32_t *)space;
570 for (i = 0; i < fs->fs_ncg; i++)
571 *lp++ = fs->fs_contigsumsize;
572 }
573 mp->mnt_data = (qaddr_t)ump;
574 mp->mnt_stat.f_fsid.val[0] = (long)dev;
575 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
576 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
577 mp->mnt_flag |= MNT_LOCAL;
578 #ifdef FFS_EI
579 if (needswap)
580 ump->um_flags |= UFS_NEEDSWAP;
581 #endif
582 ump->um_mountp = mp;
583 ump->um_dev = dev;
584 ump->um_devvp = devvp;
585 ump->um_nindir = fs->fs_nindir;
586 ump->um_bptrtodb = fs->fs_fsbtodb;
587 ump->um_seqinc = fs->fs_frag;
588 for (i = 0; i < MAXQUOTAS; i++)
589 ump->um_quotas[i] = NULLVP;
590 devvp->v_specflags |= SI_MOUNTEDON;
591 ffs_oldfscompat(fs);
592 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
593 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1; /* XXX */
594 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
595 fs->fs_maxfilesize = maxfilesize; /* XXX */
596 return (0);
597 out2:
598 free(fs, M_UFSMNT);
599 out:
600 if (bp)
601 brelse(bp);
602 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
603 if (ump) {
604 free(ump, M_UFSMNT);
605 mp->mnt_data = (qaddr_t)0;
606 }
607 return (error);
608 }
609
610 /*
611 * Sanity checks for old file systems.
612 *
613 * XXX - goes away some day.
614 */
615 int
616 ffs_oldfscompat(fs)
617 struct fs *fs;
618 {
619 int i;
620
621 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
622 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
623 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
624 fs->fs_nrpos = 8; /* XXX */
625 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
626 u_int64_t sizepb = fs->fs_bsize; /* XXX */
627 /* XXX */
628 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
629 for (i = 0; i < NIADDR; i++) { /* XXX */
630 sizepb *= NINDIR(fs); /* XXX */
631 fs->fs_maxfilesize += sizepb; /* XXX */
632 } /* XXX */
633 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
634 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
635 } /* XXX */
636 return (0);
637 }
638
639 /*
640 * unmount system call
641 */
642 int
643 ffs_unmount(mp, mntflags, p)
644 struct mount *mp;
645 int mntflags;
646 struct proc *p;
647 {
648 register struct ufsmount *ump;
649 register struct fs *fs;
650 int error, flags;
651
652 flags = 0;
653 if (mntflags & MNT_FORCE)
654 flags |= FORCECLOSE;
655 if ((error = ffs_flushfiles(mp, flags, p)) != 0)
656 return (error);
657 ump = VFSTOUFS(mp);
658 fs = ump->um_fs;
659 if (fs->fs_ronly == 0 &&
660 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
661 fs->fs_clean & FS_WASCLEAN) {
662 fs->fs_clean = FS_ISCLEAN;
663 (void) ffs_sbupdate(ump, MNT_WAIT);
664 }
665 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
666 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
667 NOCRED, p);
668 vrele(ump->um_devvp);
669 free(fs->fs_csp[0], M_UFSMNT);
670 free(fs, M_UFSMNT);
671 free(ump, M_UFSMNT);
672 mp->mnt_data = (qaddr_t)0;
673 mp->mnt_flag &= ~MNT_LOCAL;
674 return (error);
675 }
676
677 /*
678 * Flush out all the files in a filesystem.
679 */
680 int
681 ffs_flushfiles(mp, flags, p)
682 register struct mount *mp;
683 int flags;
684 struct proc *p;
685 {
686 extern int doforce;
687 register struct ufsmount *ump;
688 int error;
689
690 if (!doforce)
691 flags &= ~FORCECLOSE;
692 ump = VFSTOUFS(mp);
693 #ifdef QUOTA
694 if (mp->mnt_flag & MNT_QUOTA) {
695 int i;
696 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
697 return (error);
698 for (i = 0; i < MAXQUOTAS; i++) {
699 if (ump->um_quotas[i] == NULLVP)
700 continue;
701 quotaoff(p, mp, i);
702 }
703 /*
704 * Here we fall through to vflush again to ensure
705 * that we have gotten rid of all the system vnodes.
706 */
707 }
708 #endif
709 error = vflush(mp, NULLVP, flags);
710 return (error);
711 }
712
713 /*
714 * Get file system statistics.
715 */
716 int
717 ffs_statfs(mp, sbp, p)
718 struct mount *mp;
719 register struct statfs *sbp;
720 struct proc *p;
721 {
722 register struct ufsmount *ump;
723 register struct fs *fs;
724
725 ump = VFSTOUFS(mp);
726 fs = ump->um_fs;
727 if (fs->fs_magic != FS_MAGIC)
728 panic("ffs_statfs");
729 #ifdef COMPAT_09
730 sbp->f_type = 1;
731 #else
732 sbp->f_type = 0;
733 #endif
734 sbp->f_bsize = fs->fs_fsize;
735 sbp->f_iosize = fs->fs_bsize;
736 sbp->f_blocks = fs->fs_dsize;
737 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
738 fs->fs_cstotal.cs_nffree;
739 sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
740 (100 - fs->fs_minfree) / (u_int64_t) 100) -
741 (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
742 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
743 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
744 if (sbp != &mp->mnt_stat) {
745 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
746 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
747 }
748 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
749 return (0);
750 }
751
752 /*
753 * Go through the disk queues to initiate sandbagged IO;
754 * go through the inodes to write those that have been modified;
755 * initiate the writing of the super block if it has been modified.
756 *
757 * Note: we are always called with the filesystem marked `MPBUSY'.
758 */
759 int
760 ffs_sync(mp, waitfor, cred, p)
761 struct mount *mp;
762 int waitfor;
763 struct ucred *cred;
764 struct proc *p;
765 {
766 struct vnode *vp, *nvp;
767 struct inode *ip;
768 struct ufsmount *ump = VFSTOUFS(mp);
769 struct fs *fs;
770 int error, allerror = 0;
771
772 fs = ump->um_fs;
773 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
774 printf("fs = %s\n", fs->fs_fsmnt);
775 panic("update: rofs mod");
776 }
777 /*
778 * Write back each (modified) inode.
779 */
780 simple_lock(&mntvnode_slock);
781 loop:
782 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
783 /*
784 * If the vnode that we are about to sync is no longer
785 * associated with this mount point, start over.
786 */
787 if (vp->v_mount != mp)
788 goto loop;
789 simple_lock(&vp->v_interlock);
790 nvp = vp->v_mntvnodes.le_next;
791 ip = VTOI(vp);
792 if ((ip->i_flag &
793 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
794 vp->v_dirtyblkhd.lh_first == NULL) {
795 simple_unlock(&vp->v_interlock);
796 continue;
797 }
798 simple_unlock(&mntvnode_slock);
799 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
800 if (error) {
801 simple_lock(&mntvnode_slock);
802 if (error == ENOENT)
803 goto loop;
804 continue;
805 }
806 if ((error = VOP_FSYNC(vp, cred,
807 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
808 allerror = error;
809 vput(vp);
810 simple_lock(&mntvnode_slock);
811 }
812 simple_unlock(&mntvnode_slock);
813 /*
814 * Force stale file system control information to be flushed.
815 */
816 if ((error = VOP_FSYNC(ump->um_devvp, cred,
817 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
818 allerror = error;
819 #ifdef QUOTA
820 qsync(mp);
821 #endif
822 /*
823 * Write back modified superblock.
824 */
825 if (fs->fs_fmod != 0) {
826 fs->fs_fmod = 0;
827 fs->fs_time = time.tv_sec;
828 allerror = ffs_cgupdate(ump, waitfor);
829 }
830 return (allerror);
831 }
832
833 /*
834 * Look up a FFS dinode number to find its incore vnode, otherwise read it
835 * in from disk. If it is in core, wait for the lock bit to clear, then
836 * return the inode locked. Detection and handling of mount points must be
837 * done by the calling routine.
838 */
839 int
840 ffs_vget(mp, ino, vpp)
841 struct mount *mp;
842 ino_t ino;
843 struct vnode **vpp;
844 {
845 struct fs *fs;
846 struct inode *ip;
847 struct ufsmount *ump;
848 struct buf *bp;
849 struct vnode *vp;
850 dev_t dev;
851 int type, error;
852
853 ump = VFSTOUFS(mp);
854 dev = ump->um_dev;
855 do {
856 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
857 return (0);
858 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
859
860 /* Allocate a new vnode/inode. */
861 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
862 *vpp = NULL;
863 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
864 return (error);
865 }
866 type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */
867 MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK);
868 bzero((caddr_t)ip, sizeof(struct inode));
869 lockinit(&ip->i_lock, PINOD, "inode", 0, 0);
870 vp->v_data = ip;
871 ip->i_vnode = vp;
872 ip->i_fs = fs = ump->um_fs;
873 ip->i_dev = dev;
874 ip->i_number = ino;
875 #ifdef QUOTA
876 {
877 int i;
878
879 for (i = 0; i < MAXQUOTAS; i++)
880 ip->i_dquot[i] = NODQUOT;
881 }
882 #endif
883 /*
884 * Put it onto its hash chain and lock it so that other requests for
885 * this inode will block if they arrive while we are sleeping waiting
886 * for old data structures to be purged or for the contents of the
887 * disk portion of this inode to be read.
888 */
889 ufs_ihashins(ip);
890 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
891
892 /* Read in the disk contents for the inode, copy into the inode. */
893 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
894 (int)fs->fs_bsize, NOCRED, &bp);
895 if (error) {
896 /*
897 * The inode does not contain anything useful, so it would
898 * be misleading to leave it on its hash chain. With mode
899 * still zero, it will be unlinked and returned to the free
900 * list by vput().
901 */
902 vput(vp);
903 brelse(bp);
904 *vpp = NULL;
905 return (error);
906 }
907 #ifdef FFS_EI
908 if (UFS_MPNEEDSWAP(mp))
909 ffs_dinode_swap((struct dinode *)bp->b_data +
910 ino_to_fsbo(fs, ino), &(ip->i_din.ffs_din));
911 else
912 #endif
913 ip->i_din.ffs_din =
914 *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
915 brelse(bp);
916
917 /*
918 * Initialize the vnode from the inode, check for aliases.
919 * Note that the underlying vnode may have changed.
920 */
921 error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
922 if (error) {
923 vput(vp);
924 *vpp = NULL;
925 return (error);
926 }
927 /*
928 * Finish inode initialization now that aliasing has been resolved.
929 */
930 ip->i_devvp = ump->um_devvp;
931 VREF(ip->i_devvp);
932 /*
933 * Ensure that uid and gid are correct. This is a temporary
934 * fix until fsck has been changed to do the update.
935 */
936 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
937 ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid; /* XXX */
938 ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid; /* XXX */
939 } /* XXX */
940
941 *vpp = vp;
942 return (0);
943 }
944
945 /*
946 * File handle to vnode
947 *
948 * Have to be really careful about stale file handles:
949 * - check that the inode number is valid
950 * - call ffs_vget() to get the locked inode
951 * - check for an unallocated inode (i_mode == 0)
952 * - check that the given client host has export rights and return
953 * those rights via. exflagsp and credanonp
954 */
955 int
956 ffs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
957 register struct mount *mp;
958 struct fid *fhp;
959 struct mbuf *nam;
960 struct vnode **vpp;
961 int *exflagsp;
962 struct ucred **credanonp;
963 {
964 register struct ufid *ufhp;
965 struct fs *fs;
966
967 ufhp = (struct ufid *)fhp;
968 fs = VFSTOUFS(mp)->um_fs;
969 if (ufhp->ufid_ino < ROOTINO ||
970 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
971 return (ESTALE);
972 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
973 }
974
975 /*
976 * Vnode pointer to File handle
977 */
978 /* ARGSUSED */
979 int
980 ffs_vptofh(vp, fhp)
981 struct vnode *vp;
982 struct fid *fhp;
983 {
984 register struct inode *ip;
985 register struct ufid *ufhp;
986
987 ip = VTOI(vp);
988 ufhp = (struct ufid *)fhp;
989 ufhp->ufid_len = sizeof(struct ufid);
990 ufhp->ufid_ino = ip->i_number;
991 ufhp->ufid_gen = ip->i_ffs_gen;
992 return (0);
993 }
994
995 void
996 ffs_init()
997 {
998 ufs_init();
999 }
1000
1001 int
1002 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1003 int *name;
1004 u_int namelen;
1005 void *oldp;
1006 size_t *oldlenp;
1007 void *newp;
1008 size_t newlen;
1009 struct proc *p;
1010 {
1011 extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
1012
1013 /* all sysctl names at this level are terminal */
1014 if (namelen != 1)
1015 return (ENOTDIR); /* overloaded */
1016
1017 switch (name[0]) {
1018 case FFS_CLUSTERREAD:
1019 return (sysctl_int(oldp, oldlenp, newp, newlen,
1020 &doclusterread));
1021 case FFS_CLUSTERWRITE:
1022 return (sysctl_int(oldp, oldlenp, newp, newlen,
1023 &doclusterwrite));
1024 case FFS_REALLOCBLKS:
1025 return (sysctl_int(oldp, oldlenp, newp, newlen,
1026 &doreallocblks));
1027 case FFS_ASYNCFREE:
1028 return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
1029 default:
1030 return (EOPNOTSUPP);
1031 }
1032 /* NOTREACHED */
1033 }
1034
1035 /*
1036 * Write a superblock and associated information back to disk.
1037 */
1038 int
1039 ffs_sbupdate(mp, waitfor)
1040 struct ufsmount *mp;
1041 int waitfor;
1042 {
1043 register struct fs *fs = mp->um_fs;
1044 register struct buf *bp;
1045 int i, error = 0;
1046 int32_t saved_nrpos = fs->fs_nrpos;
1047 int64_t saved_qbmask = fs->fs_qbmask;
1048 int64_t saved_qfmask = fs->fs_qfmask;
1049 u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
1050
1051 /* Restore compatibility to old file systems. XXX */
1052 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1053 fs->fs_nrpos = -1; /* XXX */
1054 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1055 int32_t *lp, tmp; /* XXX */
1056 /* XXX */
1057 lp = (int32_t *)&fs->fs_qbmask; /* XXX nuke qfmask too */
1058 tmp = lp[4]; /* XXX */
1059 for (i = 4; i > 0; i--) /* XXX */
1060 lp[i] = lp[i-1]; /* XXX */
1061 lp[0] = tmp; /* XXX */
1062 } /* XXX */
1063 fs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1064
1065 bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
1066 (int)fs->fs_sbsize, 0, 0);
1067 bcopy(fs, bp->b_data, fs->fs_sbsize);
1068 #ifdef FFS_EI
1069 if (mp->um_flags & UFS_NEEDSWAP)
1070 ffs_sb_swap(fs, (struct fs*)bp->b_data, 1);
1071 #endif
1072
1073 fs->fs_nrpos = saved_nrpos; /* XXX */
1074 fs->fs_qbmask = saved_qbmask; /* XXX */
1075 fs->fs_qfmask = saved_qfmask; /* XXX */
1076 fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
1077
1078 if (waitfor == MNT_WAIT)
1079 error = bwrite(bp);
1080 else
1081 bawrite(bp);
1082 return (error);
1083 }
1084
1085 int
1086 ffs_cgupdate(mp, waitfor)
1087 struct ufsmount *mp;
1088 int waitfor;
1089 {
1090 register struct fs *fs = mp->um_fs;
1091 register struct buf *bp;
1092 int blks;
1093 caddr_t space;
1094 int i, size, error = 0, allerror = 0;
1095
1096 allerror = ffs_sbupdate(mp, waitfor);
1097 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1098 space = (caddr_t)fs->fs_csp[0];
1099 for (i = 0; i < blks; i += fs->fs_frag) {
1100 size = fs->fs_bsize;
1101 if (i + fs->fs_frag > blks)
1102 size = (blks - i) * fs->fs_fsize;
1103 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1104 size, 0, 0);
1105 #ifdef FFS_EI
1106 if (mp->um_flags & UFS_NEEDSWAP)
1107 ffs_csum_swap((struct csum*)space,
1108 (struct csum*)bp->b_data, size);
1109 else
1110 #endif
1111 bcopy(space, bp->b_data, (u_int)size);
1112 space += size;
1113 if (waitfor == MNT_WAIT)
1114 error = bwrite(bp);
1115 else
1116 bawrite(bp);
1117 }
1118 if (!allerror && error)
1119 allerror = error;
1120 return (allerror);
1121 }
1122