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