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