ffs_vfsops.c revision 1.55 1 /* $NetBSD: ffs_vfsops.c,v 1.55 1999/11/15 18:49:13 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 if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
579 error = EINVAL;
580 goto out;
581 }
582 /* make sure cylinder group summary area is a reasonable size. */
583 if (fs->fs_cgsize == 0 || fs->fs_cpg == 0 ||
584 fs->fs_ncg > fs->fs_ncyl / fs->fs_cpg + 1 ||
585 fs->fs_cssize >
586 fragroundup(fs, fs->fs_ncg * sizeof(struct csum))) {
587 error = EINVAL; /* XXX needs translation */
588 goto out2;
589 }
590 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
591 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
592 error = EROFS; /* XXX what should be returned? */
593 goto out2;
594 }
595 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
596 memset((caddr_t)ump, 0, sizeof *ump);
597 ump->um_fs = fs;
598 if (fs->fs_sbsize < SBSIZE)
599 bp->b_flags |= B_INVAL;
600 brelse(bp);
601 bp = NULL;
602 fs->fs_ronly = ronly;
603 if (ronly == 0) {
604 fs->fs_clean <<= 1;
605 fs->fs_fmod = 1;
606 }
607 size = fs->fs_cssize;
608 blks = howmany(size, fs->fs_fsize);
609 if (fs->fs_contigsumsize > 0)
610 size += fs->fs_ncg * sizeof(int32_t);
611 base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
612 for (i = 0; i < blks; i += fs->fs_frag) {
613 size = fs->fs_bsize;
614 if (i + fs->fs_frag > blks)
615 size = (blks - i) * fs->fs_fsize;
616 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
617 cred, &bp);
618 if (error) {
619 free(base, M_UFSMNT);
620 goto out2;
621 }
622 #ifdef FFS_EI
623 if (needswap)
624 ffs_csum_swap((struct csum*)bp->b_data,
625 (struct csum*)space, size);
626 else
627 #endif
628 memcpy(space, bp->b_data, (u_int)size);
629
630 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
631 space += size;
632 brelse(bp);
633 bp = NULL;
634 }
635 if (fs->fs_contigsumsize > 0) {
636 fs->fs_maxcluster = lp = (int32_t *)space;
637 for (i = 0; i < fs->fs_ncg; i++)
638 *lp++ = fs->fs_contigsumsize;
639 }
640 mp->mnt_data = (qaddr_t)ump;
641 mp->mnt_stat.f_fsid.val[0] = (long)dev;
642 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
643 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
644 mp->mnt_flag |= MNT_LOCAL;
645 #ifdef FFS_EI
646 if (needswap)
647 ump->um_flags |= UFS_NEEDSWAP;
648 #endif
649 ump->um_mountp = mp;
650 ump->um_dev = dev;
651 ump->um_devvp = devvp;
652 ump->um_nindir = fs->fs_nindir;
653 ump->um_bptrtodb = fs->fs_fsbtodb;
654 ump->um_seqinc = fs->fs_frag;
655 for (i = 0; i < MAXQUOTAS; i++)
656 ump->um_quotas[i] = NULLVP;
657 devvp->v_specmountpoint = mp;
658 ffs_oldfscompat(fs);
659 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
660 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1; /* XXX */
661 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
662 fs->fs_maxfilesize = maxfilesize; /* XXX */
663 if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
664 error = softdep_mount(devvp, mp, fs, cred);
665 if (error) {
666 free(base, M_UFSMNT);
667 goto out;
668 }
669 }
670 return (0);
671 out2:
672 free(fs, M_UFSMNT);
673 out:
674 devvp->v_specmountpoint = NULL;
675 if (bp)
676 brelse(bp);
677 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
678 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
679 VOP_UNLOCK(devvp, 0);
680 if (ump) {
681 free(ump, M_UFSMNT);
682 mp->mnt_data = (qaddr_t)0;
683 }
684 return (error);
685 }
686
687 /*
688 * Sanity checks for old file systems.
689 *
690 * XXX - goes away some day.
691 */
692 int
693 ffs_oldfscompat(fs)
694 struct fs *fs;
695 {
696 int i;
697
698 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
699 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
700 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
701 fs->fs_nrpos = 8; /* XXX */
702 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
703 u_int64_t sizepb = fs->fs_bsize; /* XXX */
704 /* XXX */
705 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
706 for (i = 0; i < NIADDR; i++) { /* XXX */
707 sizepb *= NINDIR(fs); /* XXX */
708 fs->fs_maxfilesize += sizepb; /* XXX */
709 } /* XXX */
710 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
711 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
712 } /* XXX */
713 return (0);
714 }
715
716 /*
717 * unmount system call
718 */
719 int
720 ffs_unmount(mp, mntflags, p)
721 struct mount *mp;
722 int mntflags;
723 struct proc *p;
724 {
725 register struct ufsmount *ump;
726 register struct fs *fs;
727 int error, flags;
728
729 flags = 0;
730 if (mntflags & MNT_FORCE)
731 flags |= FORCECLOSE;
732 if (mp->mnt_flag & MNT_SOFTDEP) {
733 if ((error = softdep_flushfiles(mp, flags, p)) != 0)
734 return (error);
735 } else {
736 if ((error = ffs_flushfiles(mp, flags, p)) != 0)
737 return (error);
738 }
739 ump = VFSTOUFS(mp);
740 fs = ump->um_fs;
741 if (fs->fs_ronly == 0 &&
742 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
743 fs->fs_clean & FS_WASCLEAN) {
744 fs->fs_clean = FS_ISCLEAN;
745 (void) ffs_sbupdate(ump, MNT_WAIT);
746 }
747 if (ump->um_devvp->v_type != VBAD)
748 ump->um_devvp->v_specmountpoint = NULL;
749 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
750 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
751 NOCRED, p);
752 vput(ump->um_devvp);
753 free(fs->fs_csp[0], M_UFSMNT);
754 free(fs, M_UFSMNT);
755 free(ump, M_UFSMNT);
756 mp->mnt_data = (qaddr_t)0;
757 mp->mnt_flag &= ~MNT_LOCAL;
758 return (error);
759 }
760
761 /*
762 * Flush out all the files in a filesystem.
763 */
764 int
765 ffs_flushfiles(mp, flags, p)
766 register struct mount *mp;
767 int flags;
768 struct proc *p;
769 {
770 extern int doforce;
771 register struct ufsmount *ump;
772 int error;
773
774 if (!doforce)
775 flags &= ~FORCECLOSE;
776 ump = VFSTOUFS(mp);
777 #ifdef QUOTA
778 if (mp->mnt_flag & MNT_QUOTA) {
779 int i;
780 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
781 return (error);
782 for (i = 0; i < MAXQUOTAS; i++) {
783 if (ump->um_quotas[i] == NULLVP)
784 continue;
785 quotaoff(p, mp, i);
786 }
787 /*
788 * Here we fall through to vflush again to ensure
789 * that we have gotten rid of all the system vnodes.
790 */
791 }
792 #endif
793 /*
794 * Flush all the files.
795 */
796 error = vflush(mp, NULLVP, flags);
797 if (error)
798 return (error);
799 /*
800 * Flush filesystem metadata.
801 */
802 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
803 error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, p);
804 VOP_UNLOCK(ump->um_devvp, 0);
805 return (error);
806 }
807
808 /*
809 * Get file system statistics.
810 */
811 int
812 ffs_statfs(mp, sbp, p)
813 struct mount *mp;
814 register struct statfs *sbp;
815 struct proc *p;
816 {
817 register struct ufsmount *ump;
818 register struct fs *fs;
819
820 ump = VFSTOUFS(mp);
821 fs = ump->um_fs;
822 if (fs->fs_magic != FS_MAGIC)
823 panic("ffs_statfs");
824 #ifdef COMPAT_09
825 sbp->f_type = 1;
826 #else
827 sbp->f_type = 0;
828 #endif
829 sbp->f_bsize = fs->fs_fsize;
830 sbp->f_iosize = fs->fs_bsize;
831 sbp->f_blocks = fs->fs_dsize;
832 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
833 fs->fs_cstotal.cs_nffree;
834 sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
835 (100 - fs->fs_minfree) / (u_int64_t) 100) -
836 (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
837 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
838 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
839 if (sbp != &mp->mnt_stat) {
840 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
841 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
842 }
843 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
844 return (0);
845 }
846
847 /*
848 * Go through the disk queues to initiate sandbagged IO;
849 * go through the inodes to write those that have been modified;
850 * initiate the writing of the super block if it has been modified.
851 *
852 * Note: we are always called with the filesystem marked `MPBUSY'.
853 */
854 int
855 ffs_sync(mp, waitfor, cred, p)
856 struct mount *mp;
857 int waitfor;
858 struct ucred *cred;
859 struct proc *p;
860 {
861 struct vnode *vp, *nvp;
862 struct inode *ip;
863 struct ufsmount *ump = VFSTOUFS(mp);
864 struct fs *fs;
865 int error, allerror = 0;
866
867 fs = ump->um_fs;
868 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
869 printf("fs = %s\n", fs->fs_fsmnt);
870 panic("update: rofs mod");
871 }
872 /*
873 * Write back each (modified) inode.
874 */
875 simple_lock(&mntvnode_slock);
876 loop:
877 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
878 /*
879 * If the vnode that we are about to sync is no longer
880 * associated with this mount point, start over.
881 */
882 if (vp->v_mount != mp)
883 goto loop;
884 simple_lock(&vp->v_interlock);
885 nvp = vp->v_mntvnodes.le_next;
886 ip = VTOI(vp);
887 if ((vp->v_type == VNON || (ip->i_flag &
888 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0) &&
889 (vp->v_dirtyblkhd.lh_first == NULL || waitfor == MNT_LAZY)){
890 simple_unlock(&vp->v_interlock);
891 continue;
892 }
893 simple_unlock(&mntvnode_slock);
894 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
895 if (error) {
896 simple_lock(&mntvnode_slock);
897 if (error == ENOENT)
898 goto loop;
899 continue;
900 }
901 if ((error = VOP_FSYNC(vp, cred,
902 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
903 allerror = error;
904 vput(vp);
905 simple_lock(&mntvnode_slock);
906 }
907 simple_unlock(&mntvnode_slock);
908 /*
909 * Force stale file system control information to be flushed.
910 */
911 if (waitfor != MNT_LAZY) {
912 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
913 waitfor = MNT_NOWAIT;
914 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
915 if ((error = VOP_FSYNC(ump->um_devvp, cred,
916 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
917 allerror = error;
918 VOP_UNLOCK(ump->um_devvp, 0);
919 }
920 #ifdef QUOTA
921 qsync(mp);
922 #endif
923 /*
924 * Write back modified superblock.
925 */
926 if (fs->fs_fmod != 0) {
927 fs->fs_fmod = 0;
928 fs->fs_time = time.tv_sec;
929 allerror = ffs_cgupdate(ump, waitfor);
930 }
931 return (allerror);
932 }
933
934 /*
935 * Look up a FFS dinode number to find its incore vnode, otherwise read it
936 * in from disk. If it is in core, wait for the lock bit to clear, then
937 * return the inode locked. Detection and handling of mount points must be
938 * done by the calling routine.
939 */
940 int
941 ffs_vget(mp, ino, vpp)
942 struct mount *mp;
943 ino_t ino;
944 struct vnode **vpp;
945 {
946 struct fs *fs;
947 struct inode *ip;
948 struct ufsmount *ump;
949 struct buf *bp;
950 struct vnode *vp;
951 dev_t dev;
952 int error;
953 caddr_t cp;
954
955 ump = VFSTOUFS(mp);
956 dev = ump->um_dev;
957 do {
958 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
959 return (0);
960 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
961
962 /* Allocate a new vnode/inode. */
963 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
964 *vpp = NULL;
965 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
966 return (error);
967 }
968 /*
969 * XXX MFS ends up here, too, to allocate an inode. Should we
970 * XXX create another pool for MFS inodes?
971 */
972 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
973 memset((caddr_t)ip, 0, sizeof(struct inode));
974 vp->v_data = ip;
975 ip->i_vnode = vp;
976 ip->i_fs = fs = ump->um_fs;
977 ip->i_dev = dev;
978 ip->i_number = ino;
979 #ifdef QUOTA
980 {
981 int i;
982
983 for (i = 0; i < MAXQUOTAS; i++)
984 ip->i_dquot[i] = NODQUOT;
985 }
986 #endif
987 /*
988 * Put it onto its hash chain and lock it so that other requests for
989 * this inode will block if they arrive while we are sleeping waiting
990 * for old data structures to be purged or for the contents of the
991 * disk portion of this inode to be read.
992 */
993 ufs_ihashins(ip);
994 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
995
996 /* Read in the disk contents for the inode, copy into the inode. */
997 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
998 (int)fs->fs_bsize, NOCRED, &bp);
999 if (error) {
1000 /*
1001 * The inode does not contain anything useful, so it would
1002 * be misleading to leave it on its hash chain. With mode
1003 * still zero, it will be unlinked and returned to the free
1004 * list by vput().
1005 */
1006 vput(vp);
1007 brelse(bp);
1008 *vpp = NULL;
1009 return (error);
1010 }
1011 cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
1012 #ifdef FFS_EI
1013 if (UFS_FSNEEDSWAP(fs))
1014 ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
1015 else
1016 #endif
1017 memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
1018 if (DOINGSOFTDEP(vp))
1019 softdep_load_inodeblock(ip);
1020 else
1021 ip->i_ffs_effnlink = ip->i_ffs_nlink;
1022 brelse(bp);
1023
1024 /*
1025 * Initialize the vnode from the inode, check for aliases.
1026 * Note that the underlying vnode may have changed.
1027 */
1028 error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1029 if (error) {
1030 vput(vp);
1031 *vpp = NULL;
1032 return (error);
1033 }
1034 /*
1035 * Finish inode initialization now that aliasing has been resolved.
1036 */
1037 ip->i_devvp = ump->um_devvp;
1038 VREF(ip->i_devvp);
1039 /*
1040 * Ensure that uid and gid are correct. This is a temporary
1041 * fix until fsck has been changed to do the update.
1042 */
1043 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1044 ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid; /* XXX */
1045 ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid; /* XXX */
1046 } /* XXX */
1047
1048 *vpp = vp;
1049 return (0);
1050 }
1051
1052 /*
1053 * File handle to vnode
1054 *
1055 * Have to be really careful about stale file handles:
1056 * - check that the inode number is valid
1057 * - call ffs_vget() to get the locked inode
1058 * - check for an unallocated inode (i_mode == 0)
1059 * - check that the given client host has export rights and return
1060 * those rights via. exflagsp and credanonp
1061 */
1062 int
1063 ffs_fhtovp(mp, fhp, vpp)
1064 register struct mount *mp;
1065 struct fid *fhp;
1066 struct vnode **vpp;
1067 {
1068 register struct ufid *ufhp;
1069 struct fs *fs;
1070
1071 ufhp = (struct ufid *)fhp;
1072 fs = VFSTOUFS(mp)->um_fs;
1073 if (ufhp->ufid_ino < ROOTINO ||
1074 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1075 return (ESTALE);
1076 return (ufs_fhtovp(mp, ufhp, vpp));
1077 }
1078
1079 /*
1080 * Vnode pointer to File handle
1081 */
1082 /* ARGSUSED */
1083 int
1084 ffs_vptofh(vp, fhp)
1085 struct vnode *vp;
1086 struct fid *fhp;
1087 {
1088 register struct inode *ip;
1089 register struct ufid *ufhp;
1090
1091 ip = VTOI(vp);
1092 ufhp = (struct ufid *)fhp;
1093 ufhp->ufid_len = sizeof(struct ufid);
1094 ufhp->ufid_ino = ip->i_number;
1095 ufhp->ufid_gen = ip->i_ffs_gen;
1096 return (0);
1097 }
1098
1099 void
1100 ffs_init()
1101 {
1102 softdep_initialize();
1103 ufs_init();
1104
1105 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
1106 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FFSNODE);
1107 }
1108
1109 int
1110 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1111 int *name;
1112 u_int namelen;
1113 void *oldp;
1114 size_t *oldlenp;
1115 void *newp;
1116 size_t newlen;
1117 struct proc *p;
1118 {
1119 extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
1120
1121 /* all sysctl names at this level are terminal */
1122 if (namelen != 1)
1123 return (ENOTDIR); /* overloaded */
1124
1125 switch (name[0]) {
1126 case FFS_CLUSTERREAD:
1127 return (sysctl_int(oldp, oldlenp, newp, newlen,
1128 &doclusterread));
1129 case FFS_CLUSTERWRITE:
1130 return (sysctl_int(oldp, oldlenp, newp, newlen,
1131 &doclusterwrite));
1132 case FFS_REALLOCBLKS:
1133 return (sysctl_int(oldp, oldlenp, newp, newlen,
1134 &doreallocblks));
1135 case FFS_ASYNCFREE:
1136 return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
1137 default:
1138 return (EOPNOTSUPP);
1139 }
1140 /* NOTREACHED */
1141 }
1142
1143 /*
1144 * Write a superblock and associated information back to disk.
1145 */
1146 int
1147 ffs_sbupdate(mp, waitfor)
1148 struct ufsmount *mp;
1149 int waitfor;
1150 {
1151 register struct fs *fs = mp->um_fs;
1152 register struct buf *bp;
1153 int i, error = 0;
1154 int32_t saved_nrpos = fs->fs_nrpos;
1155 int64_t saved_qbmask = fs->fs_qbmask;
1156 int64_t saved_qfmask = fs->fs_qfmask;
1157 u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
1158 u_int8_t saveflag;
1159
1160 /* Restore compatibility to old file systems. XXX */
1161 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1162 fs->fs_nrpos = -1; /* XXX */
1163 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1164 int32_t *lp, tmp; /* XXX */
1165 /* XXX */
1166 lp = (int32_t *)&fs->fs_qbmask; /* XXX nuke qfmask too */
1167 tmp = lp[4]; /* XXX */
1168 for (i = 4; i > 0; i--) /* XXX */
1169 lp[i] = lp[i-1]; /* XXX */
1170 lp[0] = tmp; /* XXX */
1171 } /* XXX */
1172 fs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1173
1174 bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
1175 (int)fs->fs_sbsize, 0, 0);
1176 saveflag = fs->fs_flags & FS_INTERNAL;
1177 fs->fs_flags &= ~FS_INTERNAL;
1178 memcpy(bp->b_data, fs, fs->fs_sbsize);
1179 #ifdef FFS_EI
1180 if (mp->um_flags & UFS_NEEDSWAP)
1181 ffs_sb_swap(fs, (struct fs*)bp->b_data, 1);
1182 #endif
1183
1184 fs->fs_flags |= saveflag;
1185 fs->fs_nrpos = saved_nrpos; /* XXX */
1186 fs->fs_qbmask = saved_qbmask; /* XXX */
1187 fs->fs_qfmask = saved_qfmask; /* XXX */
1188 fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
1189
1190 if (waitfor == MNT_WAIT)
1191 error = bwrite(bp);
1192 else
1193 bawrite(bp);
1194 return (error);
1195 }
1196
1197 int
1198 ffs_cgupdate(mp, waitfor)
1199 struct ufsmount *mp;
1200 int waitfor;
1201 {
1202 register struct fs *fs = mp->um_fs;
1203 register struct buf *bp;
1204 int blks;
1205 caddr_t space;
1206 int i, size, error = 0, allerror = 0;
1207
1208 allerror = ffs_sbupdate(mp, waitfor);
1209 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1210 space = (caddr_t)fs->fs_csp[0];
1211 for (i = 0; i < blks; i += fs->fs_frag) {
1212 size = fs->fs_bsize;
1213 if (i + fs->fs_frag > blks)
1214 size = (blks - i) * fs->fs_fsize;
1215 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1216 size, 0, 0);
1217 #ifdef FFS_EI
1218 if (mp->um_flags & UFS_NEEDSWAP)
1219 ffs_csum_swap((struct csum*)space,
1220 (struct csum*)bp->b_data, size);
1221 else
1222 #endif
1223 memcpy(bp->b_data, space, (u_int)size);
1224 space += size;
1225 if (waitfor == MNT_WAIT)
1226 error = bwrite(bp);
1227 else
1228 bawrite(bp);
1229 }
1230 if (!allerror && error)
1231 allerror = error;
1232 return (allerror);
1233 }
1234