ffs_vfsops.c revision 1.56 1 /* $NetBSD: ffs_vfsops.c,v 1.56 1999/12/10 14:36:04 drochner 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 || (ip->i_flag &
890 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0) &&
891 (vp->v_dirtyblkhd.lh_first == NULL || waitfor == MNT_LAZY)){
892 simple_unlock(&vp->v_interlock);
893 continue;
894 }
895 simple_unlock(&mntvnode_slock);
896 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
897 if (error) {
898 simple_lock(&mntvnode_slock);
899 if (error == ENOENT)
900 goto loop;
901 continue;
902 }
903 if ((error = VOP_FSYNC(vp, cred,
904 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
905 allerror = error;
906 vput(vp);
907 simple_lock(&mntvnode_slock);
908 }
909 simple_unlock(&mntvnode_slock);
910 /*
911 * Force stale file system control information to be flushed.
912 */
913 if (waitfor != MNT_LAZY) {
914 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
915 waitfor = MNT_NOWAIT;
916 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
917 if ((error = VOP_FSYNC(ump->um_devvp, cred,
918 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
919 allerror = error;
920 VOP_UNLOCK(ump->um_devvp, 0);
921 }
922 #ifdef QUOTA
923 qsync(mp);
924 #endif
925 /*
926 * Write back modified superblock.
927 */
928 if (fs->fs_fmod != 0) {
929 fs->fs_fmod = 0;
930 fs->fs_time = time.tv_sec;
931 allerror = ffs_cgupdate(ump, waitfor);
932 }
933 return (allerror);
934 }
935
936 /*
937 * Look up a FFS dinode number to find its incore vnode, otherwise read it
938 * in from disk. If it is in core, wait for the lock bit to clear, then
939 * return the inode locked. Detection and handling of mount points must be
940 * done by the calling routine.
941 */
942 int
943 ffs_vget(mp, ino, vpp)
944 struct mount *mp;
945 ino_t ino;
946 struct vnode **vpp;
947 {
948 struct fs *fs;
949 struct inode *ip;
950 struct ufsmount *ump;
951 struct buf *bp;
952 struct vnode *vp;
953 dev_t dev;
954 int error;
955 caddr_t cp;
956
957 ump = VFSTOUFS(mp);
958 dev = ump->um_dev;
959 do {
960 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
961 return (0);
962 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
963
964 /* Allocate a new vnode/inode. */
965 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
966 *vpp = NULL;
967 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
968 return (error);
969 }
970 /*
971 * XXX MFS ends up here, too, to allocate an inode. Should we
972 * XXX create another pool for MFS inodes?
973 */
974 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
975 memset((caddr_t)ip, 0, sizeof(struct inode));
976 vp->v_data = ip;
977 ip->i_vnode = vp;
978 ip->i_fs = fs = ump->um_fs;
979 ip->i_dev = dev;
980 ip->i_number = ino;
981 #ifdef QUOTA
982 {
983 int i;
984
985 for (i = 0; i < MAXQUOTAS; i++)
986 ip->i_dquot[i] = NODQUOT;
987 }
988 #endif
989 /*
990 * Put it onto its hash chain and lock it so that other requests for
991 * this inode will block if they arrive while we are sleeping waiting
992 * for old data structures to be purged or for the contents of the
993 * disk portion of this inode to be read.
994 */
995 ufs_ihashins(ip);
996 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
997
998 /* Read in the disk contents for the inode, copy into the inode. */
999 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1000 (int)fs->fs_bsize, NOCRED, &bp);
1001 if (error) {
1002 /*
1003 * The inode does not contain anything useful, so it would
1004 * be misleading to leave it on its hash chain. With mode
1005 * still zero, it will be unlinked and returned to the free
1006 * list by vput().
1007 */
1008 vput(vp);
1009 brelse(bp);
1010 *vpp = NULL;
1011 return (error);
1012 }
1013 cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
1014 #ifdef FFS_EI
1015 if (UFS_FSNEEDSWAP(fs))
1016 ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
1017 else
1018 #endif
1019 memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
1020 if (DOINGSOFTDEP(vp))
1021 softdep_load_inodeblock(ip);
1022 else
1023 ip->i_ffs_effnlink = ip->i_ffs_nlink;
1024 brelse(bp);
1025
1026 /*
1027 * Initialize the vnode from the inode, check for aliases.
1028 * Note that the underlying vnode may have changed.
1029 */
1030 error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1031 if (error) {
1032 vput(vp);
1033 *vpp = NULL;
1034 return (error);
1035 }
1036 /*
1037 * Finish inode initialization now that aliasing has been resolved.
1038 */
1039 ip->i_devvp = ump->um_devvp;
1040 VREF(ip->i_devvp);
1041 /*
1042 * Ensure that uid and gid are correct. This is a temporary
1043 * fix until fsck has been changed to do the update.
1044 */
1045 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1046 ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid; /* XXX */
1047 ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid; /* XXX */
1048 } /* XXX */
1049
1050 *vpp = vp;
1051 return (0);
1052 }
1053
1054 /*
1055 * File handle to vnode
1056 *
1057 * Have to be really careful about stale file handles:
1058 * - check that the inode number is valid
1059 * - call ffs_vget() to get the locked inode
1060 * - check for an unallocated inode (i_mode == 0)
1061 * - check that the given client host has export rights and return
1062 * those rights via. exflagsp and credanonp
1063 */
1064 int
1065 ffs_fhtovp(mp, fhp, vpp)
1066 register struct mount *mp;
1067 struct fid *fhp;
1068 struct vnode **vpp;
1069 {
1070 register struct ufid *ufhp;
1071 struct fs *fs;
1072
1073 ufhp = (struct ufid *)fhp;
1074 fs = VFSTOUFS(mp)->um_fs;
1075 if (ufhp->ufid_ino < ROOTINO ||
1076 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1077 return (ESTALE);
1078 return (ufs_fhtovp(mp, ufhp, vpp));
1079 }
1080
1081 /*
1082 * Vnode pointer to File handle
1083 */
1084 /* ARGSUSED */
1085 int
1086 ffs_vptofh(vp, fhp)
1087 struct vnode *vp;
1088 struct fid *fhp;
1089 {
1090 register struct inode *ip;
1091 register struct ufid *ufhp;
1092
1093 ip = VTOI(vp);
1094 ufhp = (struct ufid *)fhp;
1095 ufhp->ufid_len = sizeof(struct ufid);
1096 ufhp->ufid_ino = ip->i_number;
1097 ufhp->ufid_gen = ip->i_ffs_gen;
1098 return (0);
1099 }
1100
1101 void
1102 ffs_init()
1103 {
1104 softdep_initialize();
1105 ufs_init();
1106
1107 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
1108 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FFSNODE);
1109 }
1110
1111 int
1112 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1113 int *name;
1114 u_int namelen;
1115 void *oldp;
1116 size_t *oldlenp;
1117 void *newp;
1118 size_t newlen;
1119 struct proc *p;
1120 {
1121 extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
1122
1123 /* all sysctl names at this level are terminal */
1124 if (namelen != 1)
1125 return (ENOTDIR); /* overloaded */
1126
1127 switch (name[0]) {
1128 case FFS_CLUSTERREAD:
1129 return (sysctl_int(oldp, oldlenp, newp, newlen,
1130 &doclusterread));
1131 case FFS_CLUSTERWRITE:
1132 return (sysctl_int(oldp, oldlenp, newp, newlen,
1133 &doclusterwrite));
1134 case FFS_REALLOCBLKS:
1135 return (sysctl_int(oldp, oldlenp, newp, newlen,
1136 &doreallocblks));
1137 case FFS_ASYNCFREE:
1138 return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
1139 default:
1140 return (EOPNOTSUPP);
1141 }
1142 /* NOTREACHED */
1143 }
1144
1145 /*
1146 * Write a superblock and associated information back to disk.
1147 */
1148 int
1149 ffs_sbupdate(mp, waitfor)
1150 struct ufsmount *mp;
1151 int waitfor;
1152 {
1153 register struct fs *fs = mp->um_fs;
1154 register struct buf *bp;
1155 int i, error = 0;
1156 int32_t saved_nrpos = fs->fs_nrpos;
1157 int64_t saved_qbmask = fs->fs_qbmask;
1158 int64_t saved_qfmask = fs->fs_qfmask;
1159 u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
1160 u_int8_t saveflag;
1161
1162 /* Restore compatibility to old file systems. XXX */
1163 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1164 fs->fs_nrpos = -1; /* XXX */
1165 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1166 int32_t *lp, tmp; /* XXX */
1167 /* XXX */
1168 lp = (int32_t *)&fs->fs_qbmask; /* XXX nuke qfmask too */
1169 tmp = lp[4]; /* XXX */
1170 for (i = 4; i > 0; i--) /* XXX */
1171 lp[i] = lp[i-1]; /* XXX */
1172 lp[0] = tmp; /* XXX */
1173 } /* XXX */
1174 fs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1175
1176 bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
1177 (int)fs->fs_sbsize, 0, 0);
1178 saveflag = fs->fs_flags & FS_INTERNAL;
1179 fs->fs_flags &= ~FS_INTERNAL;
1180 memcpy(bp->b_data, fs, fs->fs_sbsize);
1181 #ifdef FFS_EI
1182 if (mp->um_flags & UFS_NEEDSWAP)
1183 ffs_sb_swap(fs, (struct fs*)bp->b_data, 1);
1184 #endif
1185
1186 fs->fs_flags |= saveflag;
1187 fs->fs_nrpos = saved_nrpos; /* XXX */
1188 fs->fs_qbmask = saved_qbmask; /* XXX */
1189 fs->fs_qfmask = saved_qfmask; /* XXX */
1190 fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
1191
1192 if (waitfor == MNT_WAIT)
1193 error = bwrite(bp);
1194 else
1195 bawrite(bp);
1196 return (error);
1197 }
1198
1199 int
1200 ffs_cgupdate(mp, waitfor)
1201 struct ufsmount *mp;
1202 int waitfor;
1203 {
1204 register struct fs *fs = mp->um_fs;
1205 register struct buf *bp;
1206 int blks;
1207 caddr_t space;
1208 int i, size, error = 0, allerror = 0;
1209
1210 allerror = ffs_sbupdate(mp, waitfor);
1211 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1212 space = (caddr_t)fs->fs_csp[0];
1213 for (i = 0; i < blks; i += fs->fs_frag) {
1214 size = fs->fs_bsize;
1215 if (i + fs->fs_frag > blks)
1216 size = (blks - i) * fs->fs_fsize;
1217 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1218 size, 0, 0);
1219 #ifdef FFS_EI
1220 if (mp->um_flags & UFS_NEEDSWAP)
1221 ffs_csum_swap((struct csum*)space,
1222 (struct csum*)bp->b_data, size);
1223 else
1224 #endif
1225 memcpy(bp->b_data, space, (u_int)size);
1226 space += size;
1227 if (waitfor == MNT_WAIT)
1228 error = bwrite(bp);
1229 else
1230 bawrite(bp);
1231 }
1232 if (!allerror && error)
1233 allerror = error;
1234 return (allerror);
1235 }
1236