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