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