ffs_vfsops.c revision 1.15 1 /* $NetBSD: ffs_vfsops.c,v 1.15 1995/04/12 21:21:00 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 ufs_vfsops = {
67 MOUNT_UFS,
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 = &ufs_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_UFS);
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 sbp->f_fstypename[MFSNAMELEN] = '\0';
656 return (0);
657 }
658
659 /*
660 * Go through the disk queues to initiate sandbagged IO;
661 * go through the inodes to write those that have been modified;
662 * initiate the writing of the super block if it has been modified.
663 *
664 * Note: we are always called with the filesystem marked `MPBUSY'.
665 */
666 int
667 ffs_sync(mp, waitfor, cred, p)
668 struct mount *mp;
669 int waitfor;
670 struct ucred *cred;
671 struct proc *p;
672 {
673 register struct vnode *vp;
674 register struct inode *ip;
675 register struct ufsmount *ump = VFSTOUFS(mp);
676 register struct fs *fs;
677 int error, allerror = 0;
678
679 fs = ump->um_fs;
680 /*
681 * Write back modified superblock.
682 * Consistency check that the superblock
683 * is still in the buffer cache.
684 */
685 if (fs->fs_fmod != 0) {
686 if (fs->fs_ronly != 0) { /* XXX */
687 printf("fs = %s\n", fs->fs_fsmnt);
688 panic("update: rofs mod");
689 }
690 fs->fs_fmod = 0;
691 fs->fs_time = time.tv_sec;
692 allerror = ffs_cgupdate(ump, waitfor);
693 }
694 /*
695 * Write back each (modified) inode.
696 */
697 loop:
698 for (vp = mp->mnt_vnodelist.lh_first;
699 vp != NULL;
700 vp = vp->v_mntvnodes.le_next) {
701 /*
702 * If the vnode that we are about to sync is no longer
703 * associated with this mount point, start over.
704 */
705 if (vp->v_mount != mp)
706 goto loop;
707 if (VOP_ISLOCKED(vp))
708 continue;
709 ip = VTOI(vp);
710 if ((ip->i_flag &
711 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
712 vp->v_dirtyblkhd.lh_first == NULL)
713 continue;
714 if (vget(vp, 1))
715 goto loop;
716 if (error = VOP_FSYNC(vp, cred, waitfor, p))
717 allerror = error;
718 vput(vp);
719 }
720 /*
721 * Force stale file system control information to be flushed.
722 */
723 if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p))
724 allerror = error;
725 #ifdef QUOTA
726 qsync(mp);
727 #endif
728 return (allerror);
729 }
730
731 /*
732 * Look up a FFS dinode number to find its incore vnode, otherwise read it
733 * in from disk. If it is in core, wait for the lock bit to clear, then
734 * return the inode locked. Detection and handling of mount points must be
735 * done by the calling routine.
736 */
737 int
738 ffs_vget(mp, ino, vpp)
739 struct mount *mp;
740 ino_t ino;
741 struct vnode **vpp;
742 {
743 register struct fs *fs;
744 register struct inode *ip;
745 struct ufsmount *ump;
746 struct buf *bp;
747 struct vnode *vp;
748 dev_t dev;
749 int i, type, error;
750
751 ump = VFSTOUFS(mp);
752 dev = ump->um_dev;
753 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
754 return (0);
755
756 /* Allocate a new vnode/inode. */
757 if (error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) {
758 *vpp = NULL;
759 return (error);
760 }
761 type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */
762 MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK);
763 bzero((caddr_t)ip, sizeof(struct inode));
764 vp->v_data = ip;
765 ip->i_vnode = vp;
766 ip->i_fs = fs = ump->um_fs;
767 ip->i_dev = dev;
768 ip->i_number = ino;
769 #ifdef QUOTA
770 for (i = 0; i < MAXQUOTAS; i++)
771 ip->i_dquot[i] = NODQUOT;
772 #endif
773 /*
774 * Put it onto its hash chain and lock it so that other requests for
775 * this inode will block if they arrive while we are sleeping waiting
776 * for old data structures to be purged or for the contents of the
777 * disk portion of this inode to be read.
778 */
779 ufs_ihashins(ip);
780
781 /* Read in the disk contents for the inode, copy into the inode. */
782 if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
783 (int)fs->fs_bsize, NOCRED, &bp)) {
784 /*
785 * The inode does not contain anything useful, so it would
786 * be misleading to leave it on its hash chain. With mode
787 * still zero, it will be unlinked and returned to the free
788 * list by vput().
789 */
790 vput(vp);
791 brelse(bp);
792 *vpp = NULL;
793 return (error);
794 }
795 ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
796 brelse(bp);
797
798 /*
799 * Initialize the vnode from the inode, check for aliases.
800 * Note that the underlying vnode may have changed.
801 */
802 if (error = ufs_vinit(mp, ffs_specop_p, FFS_FIFOOPS, &vp)) {
803 vput(vp);
804 *vpp = NULL;
805 return (error);
806 }
807 /*
808 * Finish inode initialization now that aliasing has been resolved.
809 */
810 ip->i_devvp = ump->um_devvp;
811 VREF(ip->i_devvp);
812 /*
813 * Set up a generation number for this inode if it does not
814 * already have one. This should only happen on old filesystems.
815 */
816 if (ip->i_gen == 0) {
817 if (++nextgennumber < (u_long)time.tv_sec)
818 nextgennumber = time.tv_sec;
819 ip->i_gen = nextgennumber;
820 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
821 ip->i_flag |= IN_MODIFIED;
822 }
823 /*
824 * Ensure that uid and gid are correct. This is a temporary
825 * fix until fsck has been changed to do the update.
826 */
827 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
828 ip->i_uid = ip->i_din.di_ouid; /* XXX */
829 ip->i_gid = ip->i_din.di_ogid; /* XXX */
830 } /* XXX */
831
832 *vpp = vp;
833 return (0);
834 }
835
836 /*
837 * File handle to vnode
838 *
839 * Have to be really careful about stale file handles:
840 * - check that the inode number is valid
841 * - call ffs_vget() to get the locked inode
842 * - check for an unallocated inode (i_mode == 0)
843 * - check that the given client host has export rights and return
844 * those rights via. exflagsp and credanonp
845 */
846 int
847 ffs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
848 register struct mount *mp;
849 struct fid *fhp;
850 struct mbuf *nam;
851 struct vnode **vpp;
852 int *exflagsp;
853 struct ucred **credanonp;
854 {
855 register struct ufid *ufhp;
856 struct fs *fs;
857
858 ufhp = (struct ufid *)fhp;
859 fs = VFSTOUFS(mp)->um_fs;
860 if (ufhp->ufid_ino < ROOTINO ||
861 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
862 return (ESTALE);
863 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
864 }
865
866 /*
867 * Vnode pointer to File handle
868 */
869 /* ARGSUSED */
870 ffs_vptofh(vp, fhp)
871 struct vnode *vp;
872 struct fid *fhp;
873 {
874 register struct inode *ip;
875 register struct ufid *ufhp;
876
877 ip = VTOI(vp);
878 ufhp = (struct ufid *)fhp;
879 ufhp->ufid_len = sizeof(struct ufid);
880 ufhp->ufid_ino = ip->i_number;
881 ufhp->ufid_gen = ip->i_gen;
882 return (0);
883 }
884
885 /*
886 * Write a superblock and associated information back to disk.
887 */
888 int
889 ffs_sbupdate(mp, waitfor)
890 struct ufsmount *mp;
891 int waitfor;
892 {
893 register struct fs *dfs, *fs = mp->um_fs;
894 register struct buf *bp;
895 int i, error = 0;
896
897 bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
898 (int)fs->fs_sbsize, 0, 0);
899 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
900 /* Restore compatibility to old file systems. XXX */
901 dfs = (struct fs *)bp->b_data; /* XXX */
902 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
903 dfs->fs_nrpos = -1; /* XXX */
904 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
905 int32_t *lp, tmp; /* XXX */
906 /* XXX */
907 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */
908 tmp = lp[4]; /* XXX */
909 for (i = 4; i > 0; i--) /* XXX */
910 lp[i] = lp[i-1]; /* XXX */
911 lp[0] = tmp; /* XXX */
912 } /* XXX */
913 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
914 if (waitfor == MNT_WAIT)
915 error = bwrite(bp);
916 else
917 bawrite(bp);
918 return (error);
919 }
920
921 int
922 ffs_cgupdate(mp, waitfor)
923 struct ufsmount *mp;
924 int waitfor;
925 {
926 register struct fs *fs = mp->um_fs;
927 register struct buf *bp;
928 int blks;
929 caddr_t space;
930 int i, size, error = 0, allerror = 0;
931
932 allerror = ffs_sbupdate(mp, waitfor);
933 blks = howmany(fs->fs_cssize, fs->fs_fsize);
934 space = (caddr_t)fs->fs_csp[0];
935 for (i = 0; i < blks; i += fs->fs_frag) {
936 size = fs->fs_bsize;
937 if (i + fs->fs_frag > blks)
938 size = (blks - i) * fs->fs_fsize;
939 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
940 size, 0, 0);
941 bcopy(space, bp->b_data, (u_int)size);
942 space += size;
943 if (waitfor == MNT_WAIT)
944 error = bwrite(bp);
945 else
946 bawrite(bp);
947 }
948 if (!allerror && error)
949 allerror = error;
950 return (allerror);
951 }
952