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