ffs_vfsops.c revision 1.44 1 /* $NetBSD: ffs_vfsops.c,v 1.44 1998/10/23 00:31:29 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
36 */
37
38 #if defined(_KERNEL) && !defined(_LKM)
39 #include "opt_quota.h"
40 #include "opt_compat_netbsd.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/proc.h>
47 #include <sys/kernel.h>
48 #include <sys/vnode.h>
49 #include <sys/socket.h>
50 #include <sys/mount.h>
51 #include <sys/buf.h>
52 #include <sys/device.h>
53 #include <sys/mbuf.h>
54 #include <sys/file.h>
55 #include <sys/disklabel.h>
56 #include <sys/ioctl.h>
57 #include <sys/errno.h>
58 #include <sys/malloc.h>
59 #include <sys/pool.h>
60 #include <sys/lock.h>
61 #include <vm/vm.h>
62 #include <sys/sysctl.h>
63
64 #include <miscfs/specfs/specdev.h>
65
66 #include <ufs/ufs/quota.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ufs/inode.h>
69 #include <ufs/ufs/dir.h>
70 #include <ufs/ufs/ufs_extern.h>
71 #include <ufs/ufs/ufs_bswap.h>
72
73 #include <ufs/ffs/fs.h>
74 #include <ufs/ffs/ffs_extern.h>
75
76 extern struct lock ufs_hashlock;
77
78 int ffs_sbupdate __P((struct ufsmount *, int));
79
80 extern struct vnodeopv_desc ffs_vnodeop_opv_desc;
81 extern struct vnodeopv_desc ffs_specop_opv_desc;
82 extern struct vnodeopv_desc ffs_fifoop_opv_desc;
83
84 struct vnodeopv_desc *ffs_vnodeopv_descs[] = {
85 &ffs_vnodeop_opv_desc,
86 &ffs_specop_opv_desc,
87 &ffs_fifoop_opv_desc,
88 NULL,
89 };
90
91 struct vfsops ffs_vfsops = {
92 MOUNT_FFS,
93 ffs_mount,
94 ufs_start,
95 ffs_unmount,
96 ufs_root,
97 ufs_quotactl,
98 ffs_statfs,
99 ffs_sync,
100 ffs_vget,
101 ffs_fhtovp,
102 ffs_vptofh,
103 ffs_init,
104 ffs_sysctl,
105 ffs_mountroot,
106 ffs_vnodeopv_descs,
107 };
108
109 struct pool ffs_inode_pool;
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 memset(fs->fs_fsmnt, 0, 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 memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
280 memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
281 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
282 &size);
283 memset(mp->mnt_stat.f_mntfromname + size, 0, 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 caddr_t cp;
323
324 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
325 return (EINVAL);
326 /*
327 * Step 1: invalidate all cached meta-data.
328 */
329 devvp = VFSTOUFS(mountp)->um_devvp;
330 if (vinvalbuf(devvp, 0, cred, p, 0, 0))
331 panic("ffs_reload: dirty1");
332 /*
333 * Step 2: re-read superblock from disk.
334 */
335 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
336 size = DEV_BSIZE;
337 else
338 size = dpart.disklab->d_secsize;
339 error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
340 if (error)
341 return (error);
342 fs = VFSTOUFS(mountp)->um_fs;
343 newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
344 memcpy(newfs, bp->b_data, fs->fs_sbsize);
345 #ifdef FFS_EI
346 if (VFSTOUFS(mountp)->um_flags & UFS_NEEDSWAP)
347 ffs_sb_swap((struct fs*)bp->b_data, newfs, 0);
348 #endif
349 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
350 newfs->fs_bsize < sizeof(struct fs)) {
351 brelse(bp);
352 free(newfs, M_UFSMNT);
353 return (EIO); /* XXX needs translation */
354 }
355 /*
356 * Copy pointer fields back into superblock before copying in XXX
357 * new superblock. These should really be in the ufsmount. XXX
358 * Note that important parameters (eg fs_ncg) are unchanged.
359 */
360 memcpy(&newfs->fs_csp[0], &fs->fs_csp[0], sizeof(fs->fs_csp));
361 newfs->fs_maxcluster = fs->fs_maxcluster;
362 memcpy(fs, newfs, (u_int)fs->fs_sbsize);
363 if (fs->fs_sbsize < SBSIZE)
364 bp->b_flags |= B_INVAL;
365 brelse(bp);
366 free(newfs, M_UFSMNT);
367 mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
368 ffs_oldfscompat(fs);
369 /*
370 * Step 3: re-read summary information from disk.
371 */
372 blks = howmany(fs->fs_cssize, fs->fs_fsize);
373 for (i = 0; i < blks; i += fs->fs_frag) {
374 size = fs->fs_bsize;
375 if (i + fs->fs_frag > blks)
376 size = (blks - i) * fs->fs_fsize;
377 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
378 NOCRED, &bp);
379 if (error)
380 return (error);
381 #ifdef FFS_EI
382 if (UFS_MPNEEDSWAP(mountp))
383 ffs_csum_swap((struct csum*)bp->b_data,
384 (struct csum*)fs->fs_csp[fragstoblks(fs, i)], size);
385 else
386 #endif
387 memcpy(fs->fs_csp[fragstoblks(fs, i)], bp->b_data,
388 (size_t)size);
389 brelse(bp);
390 }
391 /*
392 * We no longer know anything about clusters per cylinder group.
393 */
394 if (fs->fs_contigsumsize > 0) {
395 lp = fs->fs_maxcluster;
396 for (i = 0; i < fs->fs_ncg; i++)
397 *lp++ = fs->fs_contigsumsize;
398 }
399
400 loop:
401 simple_lock(&mntvnode_slock);
402 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
403 if (vp->v_mount != mountp) {
404 simple_unlock(&mntvnode_slock);
405 goto loop;
406 }
407 nvp = vp->v_mntvnodes.le_next;
408 /*
409 * Step 4: invalidate all inactive vnodes.
410 */
411 if (vrecycle(vp, &mntvnode_slock, p))
412 goto loop;
413 /*
414 * Step 5: invalidate all cached file data.
415 */
416 simple_lock(&vp->v_interlock);
417 simple_unlock(&mntvnode_slock);
418 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
419 goto loop;
420 if (vinvalbuf(vp, 0, cred, p, 0, 0))
421 panic("ffs_reload: dirty2");
422 /*
423 * Step 6: re-read inode data for all active vnodes.
424 */
425 ip = VTOI(vp);
426 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
427 (int)fs->fs_bsize, NOCRED, &bp);
428 if (error) {
429 vput(vp);
430 return (error);
431 }
432 cp = (caddr_t)bp->b_data +
433 (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
434 #ifdef FFS_EI
435 if (UFS_MPNEEDSWAP(mountp))
436 ffs_dinode_swap((struct dinode *)cp,
437 &ip->i_din.ffs_din);
438 else
439 #endif
440 memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
441 brelse(bp);
442 vput(vp);
443 simple_lock(&mntvnode_slock);
444 }
445 simple_unlock(&mntvnode_slock);
446 return (0);
447 }
448
449 /*
450 * Common code for mount and mountroot
451 */
452 int
453 ffs_mountfs(devvp, mp, p)
454 register struct vnode *devvp;
455 struct mount *mp;
456 struct proc *p;
457 {
458 struct ufsmount *ump;
459 struct buf *bp;
460 struct fs *fs;
461 dev_t dev;
462 struct partinfo dpart;
463 caddr_t base, space;
464 int blks;
465 int error, i, size, ronly, needswap;
466 int32_t *lp;
467 struct ucred *cred;
468 extern struct vnode *rootvp;
469 u_int64_t maxfilesize; /* XXX */
470 u_int32_t sbsize;
471
472 dev = devvp->v_rdev;
473 cred = p ? p->p_ucred : NOCRED;
474 /*
475 * Disallow multiple mounts of the same device.
476 * Disallow mounting of a device that is currently in use
477 * (except for root, which might share swap device for miniroot).
478 * Flush out any old buffers remaining from a previous use.
479 */
480 if ((error = vfs_mountedon(devvp)) != 0)
481 return (error);
482 if (vcount(devvp) > 1 && devvp != rootvp)
483 return (EBUSY);
484 if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
485 return (error);
486
487 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
488 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
489 if (error)
490 return (error);
491 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
492 size = DEV_BSIZE;
493 else
494 size = dpart.disklab->d_secsize;
495
496 bp = NULL;
497 ump = NULL;
498 error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, cred, &bp);
499 if (error)
500 goto out;
501
502 fs = (struct fs*)bp->b_data;
503 if (fs->fs_magic == FS_MAGIC) {
504 needswap = 0;
505 sbsize = fs->fs_sbsize;
506 #ifdef FFS_EI
507 } else if (fs->fs_magic == bswap32(FS_MAGIC)) {
508 needswap = 1;
509 sbsize = bswap32(fs->fs_sbsize);
510 #endif
511 } else {
512 error = EINVAL;
513 goto out;
514
515 }
516
517 fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
518 memcpy(fs, bp->b_data, sbsize);
519 #ifdef FFS_EI
520 if (needswap)
521 ffs_sb_swap((struct fs*)bp->b_data, fs, 0);
522 #endif
523
524 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
525 fs->fs_bsize < sizeof(struct fs)) {
526 error = EINVAL; /* XXX needs translation */
527 goto out2;
528 }
529 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
530 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
531 error = EROFS; /* XXX what should be returned? */
532 goto out2;
533 }
534 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
535 memset((caddr_t)ump, 0, sizeof *ump);
536 ump->um_fs = fs;
537 if (fs->fs_sbsize < SBSIZE)
538 bp->b_flags |= B_INVAL;
539 brelse(bp);
540 bp = NULL;
541 fs->fs_ronly = ronly;
542 if (ronly == 0) {
543 fs->fs_clean <<= 1;
544 fs->fs_fmod = 1;
545 }
546 size = fs->fs_cssize;
547 blks = howmany(size, fs->fs_fsize);
548 if (fs->fs_contigsumsize > 0)
549 size += fs->fs_ncg * sizeof(int32_t);
550 base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
551 for (i = 0; i < blks; i += fs->fs_frag) {
552 size = fs->fs_bsize;
553 if (i + fs->fs_frag > blks)
554 size = (blks - i) * fs->fs_fsize;
555 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
556 cred, &bp);
557 if (error) {
558 free(base, M_UFSMNT);
559 goto out2;
560 }
561 #ifdef FFS_EI
562 if (needswap)
563 ffs_csum_swap((struct csum*)bp->b_data,
564 (struct csum*)space, size);
565 else
566 #endif
567 memcpy(space, bp->b_data, (u_int)size);
568
569 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
570 space += size;
571 brelse(bp);
572 bp = NULL;
573 }
574 if (fs->fs_contigsumsize > 0) {
575 fs->fs_maxcluster = lp = (int32_t *)space;
576 for (i = 0; i < fs->fs_ncg; i++)
577 *lp++ = fs->fs_contigsumsize;
578 }
579 mp->mnt_data = (qaddr_t)ump;
580 mp->mnt_stat.f_fsid.val[0] = (long)dev;
581 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
582 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
583 mp->mnt_flag |= MNT_LOCAL;
584 #ifdef FFS_EI
585 if (needswap)
586 ump->um_flags |= UFS_NEEDSWAP;
587 #endif
588 ump->um_mountp = mp;
589 ump->um_dev = dev;
590 ump->um_devvp = devvp;
591 ump->um_nindir = fs->fs_nindir;
592 ump->um_bptrtodb = fs->fs_fsbtodb;
593 ump->um_seqinc = fs->fs_frag;
594 for (i = 0; i < MAXQUOTAS; i++)
595 ump->um_quotas[i] = NULLVP;
596 devvp->v_specflags |= SI_MOUNTEDON;
597 ffs_oldfscompat(fs);
598 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
599 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1; /* XXX */
600 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
601 fs->fs_maxfilesize = maxfilesize; /* XXX */
602 return (0);
603 out2:
604 free(fs, M_UFSMNT);
605 out:
606 if (bp)
607 brelse(bp);
608 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
609 if (ump) {
610 free(ump, M_UFSMNT);
611 mp->mnt_data = (qaddr_t)0;
612 }
613 return (error);
614 }
615
616 /*
617 * Sanity checks for old file systems.
618 *
619 * XXX - goes away some day.
620 */
621 int
622 ffs_oldfscompat(fs)
623 struct fs *fs;
624 {
625 int i;
626
627 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
628 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
629 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
630 fs->fs_nrpos = 8; /* XXX */
631 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
632 u_int64_t sizepb = fs->fs_bsize; /* XXX */
633 /* XXX */
634 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
635 for (i = 0; i < NIADDR; i++) { /* XXX */
636 sizepb *= NINDIR(fs); /* XXX */
637 fs->fs_maxfilesize += sizepb; /* XXX */
638 } /* XXX */
639 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
640 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
641 } /* XXX */
642 return (0);
643 }
644
645 /*
646 * unmount system call
647 */
648 int
649 ffs_unmount(mp, mntflags, p)
650 struct mount *mp;
651 int mntflags;
652 struct proc *p;
653 {
654 register struct ufsmount *ump;
655 register struct fs *fs;
656 int error, flags;
657
658 flags = 0;
659 if (mntflags & MNT_FORCE)
660 flags |= FORCECLOSE;
661 if ((error = ffs_flushfiles(mp, flags, p)) != 0)
662 return (error);
663 ump = VFSTOUFS(mp);
664 fs = ump->um_fs;
665 if (fs->fs_ronly == 0 &&
666 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
667 fs->fs_clean & FS_WASCLEAN) {
668 fs->fs_clean = FS_ISCLEAN;
669 (void) ffs_sbupdate(ump, MNT_WAIT);
670 }
671 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
672 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
673 NOCRED, p);
674 vrele(ump->um_devvp);
675 free(fs->fs_csp[0], M_UFSMNT);
676 free(fs, M_UFSMNT);
677 free(ump, M_UFSMNT);
678 mp->mnt_data = (qaddr_t)0;
679 mp->mnt_flag &= ~MNT_LOCAL;
680 return (error);
681 }
682
683 /*
684 * Flush out all the files in a filesystem.
685 */
686 int
687 ffs_flushfiles(mp, flags, p)
688 register struct mount *mp;
689 int flags;
690 struct proc *p;
691 {
692 extern int doforce;
693 register struct ufsmount *ump;
694 int error;
695
696 if (!doforce)
697 flags &= ~FORCECLOSE;
698 ump = VFSTOUFS(mp);
699 #ifdef QUOTA
700 if (mp->mnt_flag & MNT_QUOTA) {
701 int i;
702 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
703 return (error);
704 for (i = 0; i < MAXQUOTAS; i++) {
705 if (ump->um_quotas[i] == NULLVP)
706 continue;
707 quotaoff(p, mp, i);
708 }
709 /*
710 * Here we fall through to vflush again to ensure
711 * that we have gotten rid of all the system vnodes.
712 */
713 }
714 #endif
715 error = vflush(mp, NULLVP, flags);
716 return (error);
717 }
718
719 /*
720 * Get file system statistics.
721 */
722 int
723 ffs_statfs(mp, sbp, p)
724 struct mount *mp;
725 register struct statfs *sbp;
726 struct proc *p;
727 {
728 register struct ufsmount *ump;
729 register struct fs *fs;
730
731 ump = VFSTOUFS(mp);
732 fs = ump->um_fs;
733 if (fs->fs_magic != FS_MAGIC)
734 panic("ffs_statfs");
735 #ifdef COMPAT_09
736 sbp->f_type = 1;
737 #else
738 sbp->f_type = 0;
739 #endif
740 sbp->f_bsize = fs->fs_fsize;
741 sbp->f_iosize = fs->fs_bsize;
742 sbp->f_blocks = fs->fs_dsize;
743 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
744 fs->fs_cstotal.cs_nffree;
745 sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
746 (100 - fs->fs_minfree) / (u_int64_t) 100) -
747 (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
748 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
749 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
750 if (sbp != &mp->mnt_stat) {
751 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
752 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
753 }
754 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
755 return (0);
756 }
757
758 /*
759 * Go through the disk queues to initiate sandbagged IO;
760 * go through the inodes to write those that have been modified;
761 * initiate the writing of the super block if it has been modified.
762 *
763 * Note: we are always called with the filesystem marked `MPBUSY'.
764 */
765 int
766 ffs_sync(mp, waitfor, cred, p)
767 struct mount *mp;
768 int waitfor;
769 struct ucred *cred;
770 struct proc *p;
771 {
772 struct vnode *vp, *nvp;
773 struct inode *ip;
774 struct ufsmount *ump = VFSTOUFS(mp);
775 struct fs *fs;
776 int error, allerror = 0;
777
778 fs = ump->um_fs;
779 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
780 printf("fs = %s\n", fs->fs_fsmnt);
781 panic("update: rofs mod");
782 }
783 /*
784 * Write back each (modified) inode.
785 */
786 simple_lock(&mntvnode_slock);
787 loop:
788 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
789 /*
790 * If the vnode that we are about to sync is no longer
791 * associated with this mount point, start over.
792 */
793 if (vp->v_mount != mp)
794 goto loop;
795 simple_lock(&vp->v_interlock);
796 nvp = vp->v_mntvnodes.le_next;
797 ip = VTOI(vp);
798 if ((ip->i_flag &
799 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
800 vp->v_dirtyblkhd.lh_first == NULL) {
801 simple_unlock(&vp->v_interlock);
802 continue;
803 }
804 simple_unlock(&mntvnode_slock);
805 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
806 if (error) {
807 simple_lock(&mntvnode_slock);
808 if (error == ENOENT)
809 goto loop;
810 continue;
811 }
812 if ((error = VOP_FSYNC(vp, cred,
813 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
814 allerror = error;
815 vput(vp);
816 simple_lock(&mntvnode_slock);
817 }
818 simple_unlock(&mntvnode_slock);
819 /*
820 * Force stale file system control information to be flushed.
821 */
822 if ((error = VOP_FSYNC(ump->um_devvp, cred,
823 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
824 allerror = error;
825 #ifdef QUOTA
826 qsync(mp);
827 #endif
828 /*
829 * Write back modified superblock.
830 */
831 if (fs->fs_fmod != 0) {
832 fs->fs_fmod = 0;
833 fs->fs_time = time.tv_sec;
834 allerror = ffs_cgupdate(ump, waitfor);
835 }
836 return (allerror);
837 }
838
839 /*
840 * Look up a FFS dinode number to find its incore vnode, otherwise read it
841 * in from disk. If it is in core, wait for the lock bit to clear, then
842 * return the inode locked. Detection and handling of mount points must be
843 * done by the calling routine.
844 */
845 int
846 ffs_vget(mp, ino, vpp)
847 struct mount *mp;
848 ino_t ino;
849 struct vnode **vpp;
850 {
851 struct fs *fs;
852 struct inode *ip;
853 struct ufsmount *ump;
854 struct buf *bp;
855 struct vnode *vp;
856 dev_t dev;
857 int error;
858 caddr_t cp;
859
860 ump = VFSTOUFS(mp);
861 dev = ump->um_dev;
862 do {
863 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
864 return (0);
865 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
866
867 /* Allocate a new vnode/inode. */
868 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
869 *vpp = NULL;
870 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
871 return (error);
872 }
873 /*
874 * XXX MFS ends up here, too, to allocate an inode. Should we
875 * XXX create another pool for MFS inodes?
876 */
877 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
878 memset((caddr_t)ip, 0, sizeof(struct inode));
879 lockinit(&ip->i_lock, PINOD, "inode", 0, 0);
880 vp->v_data = ip;
881 ip->i_vnode = vp;
882 ip->i_fs = fs = ump->um_fs;
883 ip->i_dev = dev;
884 ip->i_number = ino;
885 #ifdef QUOTA
886 {
887 int i;
888
889 for (i = 0; i < MAXQUOTAS; i++)
890 ip->i_dquot[i] = NODQUOT;
891 }
892 #endif
893 /*
894 * Put it onto its hash chain and lock it so that other requests for
895 * this inode will block if they arrive while we are sleeping waiting
896 * for old data structures to be purged or for the contents of the
897 * disk portion of this inode to be read.
898 */
899 ufs_ihashins(ip);
900 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
901
902 /* Read in the disk contents for the inode, copy into the inode. */
903 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
904 (int)fs->fs_bsize, NOCRED, &bp);
905 if (error) {
906 /*
907 * The inode does not contain anything useful, so it would
908 * be misleading to leave it on its hash chain. With mode
909 * still zero, it will be unlinked and returned to the free
910 * list by vput().
911 */
912 vput(vp);
913 brelse(bp);
914 *vpp = NULL;
915 return (error);
916 }
917 cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
918 #ifdef FFS_EI
919 if (UFS_MPNEEDSWAP(mp))
920 ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
921 else
922 #endif
923 memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
924 brelse(bp);
925
926 /*
927 * Initialize the vnode from the inode, check for aliases.
928 * Note that the underlying vnode may have changed.
929 */
930 error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
931 if (error) {
932 vput(vp);
933 *vpp = NULL;
934 return (error);
935 }
936 /*
937 * Finish inode initialization now that aliasing has been resolved.
938 */
939 ip->i_devvp = ump->um_devvp;
940 VREF(ip->i_devvp);
941 /*
942 * Ensure that uid and gid are correct. This is a temporary
943 * fix until fsck has been changed to do the update.
944 */
945 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
946 ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid; /* XXX */
947 ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid; /* XXX */
948 } /* XXX */
949
950 *vpp = vp;
951 return (0);
952 }
953
954 /*
955 * File handle to vnode
956 *
957 * Have to be really careful about stale file handles:
958 * - check that the inode number is valid
959 * - call ffs_vget() to get the locked inode
960 * - check for an unallocated inode (i_mode == 0)
961 * - check that the given client host has export rights and return
962 * those rights via. exflagsp and credanonp
963 */
964 int
965 ffs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
966 register struct mount *mp;
967 struct fid *fhp;
968 struct mbuf *nam;
969 struct vnode **vpp;
970 int *exflagsp;
971 struct ucred **credanonp;
972 {
973 register struct ufid *ufhp;
974 struct fs *fs;
975
976 ufhp = (struct ufid *)fhp;
977 fs = VFSTOUFS(mp)->um_fs;
978 if (ufhp->ufid_ino < ROOTINO ||
979 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
980 return (ESTALE);
981 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
982 }
983
984 /*
985 * Vnode pointer to File handle
986 */
987 /* ARGSUSED */
988 int
989 ffs_vptofh(vp, fhp)
990 struct vnode *vp;
991 struct fid *fhp;
992 {
993 register struct inode *ip;
994 register struct ufid *ufhp;
995
996 ip = VTOI(vp);
997 ufhp = (struct ufid *)fhp;
998 ufhp->ufid_len = sizeof(struct ufid);
999 ufhp->ufid_ino = ip->i_number;
1000 ufhp->ufid_gen = ip->i_ffs_gen;
1001 return (0);
1002 }
1003
1004 void
1005 ffs_init()
1006 {
1007 ufs_init();
1008
1009 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
1010 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FFSNODE);
1011 }
1012
1013 int
1014 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1015 int *name;
1016 u_int namelen;
1017 void *oldp;
1018 size_t *oldlenp;
1019 void *newp;
1020 size_t newlen;
1021 struct proc *p;
1022 {
1023 extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
1024
1025 /* all sysctl names at this level are terminal */
1026 if (namelen != 1)
1027 return (ENOTDIR); /* overloaded */
1028
1029 switch (name[0]) {
1030 case FFS_CLUSTERREAD:
1031 return (sysctl_int(oldp, oldlenp, newp, newlen,
1032 &doclusterread));
1033 case FFS_CLUSTERWRITE:
1034 return (sysctl_int(oldp, oldlenp, newp, newlen,
1035 &doclusterwrite));
1036 case FFS_REALLOCBLKS:
1037 return (sysctl_int(oldp, oldlenp, newp, newlen,
1038 &doreallocblks));
1039 case FFS_ASYNCFREE:
1040 return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
1041 default:
1042 return (EOPNOTSUPP);
1043 }
1044 /* NOTREACHED */
1045 }
1046
1047 /*
1048 * Write a superblock and associated information back to disk.
1049 */
1050 int
1051 ffs_sbupdate(mp, waitfor)
1052 struct ufsmount *mp;
1053 int waitfor;
1054 {
1055 register struct fs *fs = mp->um_fs;
1056 register struct buf *bp;
1057 int i, error = 0;
1058 int32_t saved_nrpos = fs->fs_nrpos;
1059 int64_t saved_qbmask = fs->fs_qbmask;
1060 int64_t saved_qfmask = fs->fs_qfmask;
1061 u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
1062
1063 /* Restore compatibility to old file systems. XXX */
1064 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1065 fs->fs_nrpos = -1; /* XXX */
1066 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1067 int32_t *lp, tmp; /* XXX */
1068 /* XXX */
1069 lp = (int32_t *)&fs->fs_qbmask; /* XXX nuke qfmask too */
1070 tmp = lp[4]; /* XXX */
1071 for (i = 4; i > 0; i--) /* XXX */
1072 lp[i] = lp[i-1]; /* XXX */
1073 lp[0] = tmp; /* XXX */
1074 } /* XXX */
1075 fs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1076
1077 bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
1078 (int)fs->fs_sbsize, 0, 0);
1079 memcpy(bp->b_data, fs, fs->fs_sbsize);
1080 #ifdef FFS_EI
1081 if (mp->um_flags & UFS_NEEDSWAP)
1082 ffs_sb_swap(fs, (struct fs*)bp->b_data, 1);
1083 #endif
1084
1085 fs->fs_nrpos = saved_nrpos; /* XXX */
1086 fs->fs_qbmask = saved_qbmask; /* XXX */
1087 fs->fs_qfmask = saved_qfmask; /* XXX */
1088 fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
1089
1090 if (waitfor == MNT_WAIT)
1091 error = bwrite(bp);
1092 else
1093 bawrite(bp);
1094 return (error);
1095 }
1096
1097 int
1098 ffs_cgupdate(mp, waitfor)
1099 struct ufsmount *mp;
1100 int waitfor;
1101 {
1102 register struct fs *fs = mp->um_fs;
1103 register struct buf *bp;
1104 int blks;
1105 caddr_t space;
1106 int i, size, error = 0, allerror = 0;
1107
1108 allerror = ffs_sbupdate(mp, waitfor);
1109 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1110 space = (caddr_t)fs->fs_csp[0];
1111 for (i = 0; i < blks; i += fs->fs_frag) {
1112 size = fs->fs_bsize;
1113 if (i + fs->fs_frag > blks)
1114 size = (blks - i) * fs->fs_fsize;
1115 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1116 size, 0, 0);
1117 #ifdef FFS_EI
1118 if (mp->um_flags & UFS_NEEDSWAP)
1119 ffs_csum_swap((struct csum*)space,
1120 (struct csum*)bp->b_data, size);
1121 else
1122 #endif
1123 memcpy(bp->b_data, space, (u_int)size);
1124 space += size;
1125 if (waitfor == MNT_WAIT)
1126 error = bwrite(bp);
1127 else
1128 bawrite(bp);
1129 }
1130 if (!allerror && error)
1131 allerror = error;
1132 return (allerror);
1133 }
1134