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