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