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