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