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