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