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