ffs_vfsops.c revision 1.229.2.4 1 /* $NetBSD: ffs_vfsops.c,v 1.229.2.4 2008/06/12 08:39:22 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1989, 1991, 1993, 1994
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.229.2.4 2008/06/12 08:39:22 martin Exp $");
65
66 #if defined(_KERNEL_OPT)
67 #include "opt_ffs.h"
68 #include "opt_quota.h"
69 #include "opt_softdep.h"
70 #include "opt_wapbl.h"
71 #endif
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/namei.h>
76 #include <sys/proc.h>
77 #include <sys/kernel.h>
78 #include <sys/vnode.h>
79 #include <sys/socket.h>
80 #include <sys/mount.h>
81 #include <sys/buf.h>
82 #include <sys/device.h>
83 #include <sys/mbuf.h>
84 #include <sys/file.h>
85 #include <sys/disklabel.h>
86 #include <sys/ioctl.h>
87 #include <sys/errno.h>
88 #include <sys/malloc.h>
89 #include <sys/pool.h>
90 #include <sys/lock.h>
91 #include <sys/sysctl.h>
92 #include <sys/conf.h>
93 #include <sys/kauth.h>
94 #include <sys/wapbl.h>
95 #include <sys/fstrans.h>
96 #include <sys/module.h>
97
98 #include <miscfs/genfs/genfs.h>
99 #include <miscfs/specfs/specdev.h>
100
101 #include <ufs/ufs/quota.h>
102 #include <ufs/ufs/ufsmount.h>
103 #include <ufs/ufs/inode.h>
104 #include <ufs/ufs/dir.h>
105 #include <ufs/ufs/ufs_extern.h>
106 #include <ufs/ufs/ufs_bswap.h>
107 #include <ufs/ufs/ufs_wapbl.h>
108
109 #include <ufs/ffs/fs.h>
110 #include <ufs/ffs/ffs_extern.h>
111
112 MODULE(MODULE_CLASS_VFS, ffs, NULL);
113
114 /* how many times ffs_init() was called */
115 int ffs_initcount = 0;
116
117 extern kmutex_t ufs_hashlock;
118
119 extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
120 extern const struct vnodeopv_desc ffs_specop_opv_desc;
121 extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
122
123 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
124 &ffs_vnodeop_opv_desc,
125 &ffs_specop_opv_desc,
126 &ffs_fifoop_opv_desc,
127 NULL,
128 };
129
130 struct vfsops ffs_vfsops = {
131 MOUNT_FFS,
132 sizeof (struct ufs_args),
133 ffs_mount,
134 ufs_start,
135 ffs_unmount,
136 ufs_root,
137 ufs_quotactl,
138 ffs_statvfs,
139 ffs_sync,
140 ffs_vget,
141 ffs_fhtovp,
142 ffs_vptofh,
143 ffs_init,
144 ffs_reinit,
145 ffs_done,
146 ffs_mountroot,
147 ffs_snapshot,
148 ffs_extattrctl,
149 ffs_suspendctl,
150 genfs_renamelock_enter,
151 genfs_renamelock_exit,
152 ffs_full_fsync,
153 ffs_vnodeopv_descs,
154 0,
155 { NULL, NULL },
156 };
157
158 static const struct genfs_ops ffs_genfsops = {
159 .gop_size = ffs_gop_size,
160 .gop_alloc = ufs_gop_alloc,
161 .gop_write = genfs_gop_write,
162 .gop_markupdate = ufs_gop_markupdate,
163 };
164
165 static const struct ufs_ops ffs_ufsops = {
166 .uo_itimes = ffs_itimes,
167 .uo_update = ffs_update,
168 .uo_truncate = ffs_truncate,
169 .uo_valloc = ffs_valloc,
170 .uo_vfree = ffs_vfree,
171 .uo_balloc = ffs_balloc,
172 };
173
174 static int
175 ffs_modcmd(modcmd_t cmd, void *arg)
176 {
177
178 switch (cmd) {
179 case MODULE_CMD_INIT:
180 return vfs_attach(&ffs_vfsops);
181 case MODULE_CMD_FINI:
182 return vfs_detach(&ffs_vfsops);
183 default:
184 return ENOTTY;
185 }
186 }
187
188 pool_cache_t ffs_inode_cache;
189 pool_cache_t ffs_dinode1_cache;
190 pool_cache_t ffs_dinode2_cache;
191
192 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
193 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
194
195 /*
196 * Called by main() when ffs is going to be mounted as root.
197 */
198
199 int
200 ffs_mountroot(void)
201 {
202 struct fs *fs;
203 struct mount *mp;
204 struct lwp *l = curlwp; /* XXX */
205 struct ufsmount *ump;
206 int error;
207
208 if (device_class(root_device) != DV_DISK)
209 return (ENODEV);
210
211 if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
212 vrele(rootvp);
213 return (error);
214 }
215
216 /*
217 * We always need to be able to mount the root file system.
218 */
219 mp->mnt_flag |= MNT_FORCE;
220 if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
221 vfs_unbusy(mp, false, NULL);
222 vfs_destroy(mp);
223 return (error);
224 }
225 mp->mnt_flag &= ~MNT_FORCE;
226 mutex_enter(&mountlist_lock);
227 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
228 mutex_exit(&mountlist_lock);
229 ump = VFSTOUFS(mp);
230 fs = ump->um_fs;
231 memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
232 (void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
233 (void)ffs_statvfs(mp, &mp->mnt_stat);
234 vfs_unbusy(mp, false, NULL);
235 setrootfstime((time_t)fs->fs_time);
236 return (0);
237 }
238
239 static int dolog;
240
241 /*
242 * VFS Operations.
243 *
244 * mount system call
245 */
246 int
247 ffs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
248 {
249 struct lwp *l = curlwp;
250 struct nameidata nd;
251 struct vnode *vp, *devvp = NULL;
252 struct ufs_args *args = data;
253 struct ufsmount *ump = NULL;
254 struct fs *fs;
255 int error = 0, flags, update;
256 mode_t accessmode;
257
258 if (dolog)
259 mp->mnt_flag |= MNT_LOG;
260
261 if (*data_len < sizeof *args)
262 return EINVAL;
263
264 if (mp->mnt_flag & MNT_GETARGS) {
265 ump = VFSTOUFS(mp);
266 if (ump == NULL)
267 return EIO;
268 args->fspec = NULL;
269 *data_len = sizeof *args;
270 return 0;
271 }
272
273 #if !defined(SOFTDEP)
274 mp->mnt_flag &= ~MNT_SOFTDEP;
275 #endif
276
277 update = mp->mnt_flag & MNT_UPDATE;
278
279 /* Check arguments */
280 if (args->fspec != NULL) {
281 /*
282 * Look up the name and verify that it's sane.
283 */
284 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
285 if ((error = namei(&nd)) != 0)
286 return (error);
287 devvp = nd.ni_vp;
288
289 if (!update) {
290 /*
291 * Be sure this is a valid block device
292 */
293 if (devvp->v_type != VBLK)
294 error = ENOTBLK;
295 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
296 error = ENXIO;
297 } else {
298 /*
299 * Be sure we're still naming the same device
300 * used for our initial mount
301 */
302 ump = VFSTOUFS(mp);
303 if (devvp != ump->um_devvp) {
304 if (devvp->v_rdev != ump->um_devvp->v_rdev)
305 error = EINVAL;
306 else {
307 vrele(devvp);
308 devvp = ump->um_devvp;
309 vref(devvp);
310 }
311 }
312 }
313 } else {
314 if (!update) {
315 /* New mounts must have a filename for the device */
316 return (EINVAL);
317 } else {
318 /* Use the extant mount */
319 ump = VFSTOUFS(mp);
320 devvp = ump->um_devvp;
321 vref(devvp);
322 }
323 }
324
325 /*
326 * Mark the device and any existing vnodes as involved in
327 * softdep processing.
328 */
329 if ((mp->mnt_flag & MNT_SOFTDEP) != 0) {
330 devvp->v_uflag |= VU_SOFTDEP;
331 mutex_enter(&mntvnode_lock);
332 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
333 if (vp->v_mount != mp || vismarker(vp))
334 continue;
335 vp->v_uflag |= VU_SOFTDEP;
336 }
337 mutex_exit(&mntvnode_lock);
338 }
339
340 /*
341 * If mount by non-root, then verify that user has necessary
342 * permissions on the device.
343 */
344 if (error == 0 && kauth_authorize_generic(l->l_cred,
345 KAUTH_GENERIC_ISSUSER, NULL) != 0) {
346 accessmode = VREAD;
347 if (update ?
348 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
349 (mp->mnt_flag & MNT_RDONLY) == 0)
350 accessmode |= VWRITE;
351 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
352 error = VOP_ACCESS(devvp, accessmode, l->l_cred);
353 VOP_UNLOCK(devvp, 0);
354 }
355
356 if (error) {
357 vrele(devvp);
358 return (error);
359 }
360
361 #ifdef WAPBL
362 /*
363 * WAPBL can only be enabled on a r/w mount
364 * that does not use softdep.
365 */
366 if ((mp->mnt_flag & MNT_RDONLY) && !(mp->mnt_iflag & IMNT_WANTRDWR)) {
367 mp->mnt_flag &= ~MNT_LOG;
368 }
369 if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_LOG)) ==
370 (MNT_SOFTDEP | MNT_LOG)) {
371 printf("%s fs is journalled, ignoring soft update mode\n",
372 VFSTOUFS(mp)->um_fs->fs_fsmnt);
373 mp->mnt_flag &= ~MNT_SOFTDEP;
374 }
375 #else /* !WAPBL */
376 mp->mnt_flag &= ~MNT_LOG;
377 #endif /* !WAPBL */
378
379 if (!update) {
380 int xflags;
381
382 if (mp->mnt_flag & MNT_RDONLY)
383 xflags = FREAD;
384 else
385 xflags = FREAD | FWRITE;
386 error = VOP_OPEN(devvp, xflags, FSCRED);
387 if (error)
388 goto fail;
389 error = ffs_mountfs(devvp, mp, l);
390 if (error) {
391 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
392 (void)VOP_CLOSE(devvp, xflags, NOCRED);
393 VOP_UNLOCK(devvp, 0);
394 goto fail;
395 }
396
397 ump = VFSTOUFS(mp);
398 fs = ump->um_fs;
399 if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
400 (MNT_SOFTDEP | MNT_ASYNC)) {
401 printf("%s fs uses soft updates, "
402 "ignoring async mode\n",
403 fs->fs_fsmnt);
404 mp->mnt_flag &= ~MNT_ASYNC;
405 }
406 } else {
407 /*
408 * Update the mount.
409 */
410
411 /*
412 * The initial mount got a reference on this
413 * device, so drop the one obtained via
414 * namei(), above.
415 */
416 vrele(devvp);
417
418 ump = VFSTOUFS(mp);
419 fs = ump->um_fs;
420 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
421 /*
422 * Changing from r/w to r/o
423 */
424 flags = WRITECLOSE;
425 if (mp->mnt_flag & MNT_FORCE)
426 flags |= FORCECLOSE;
427 if (mp->mnt_flag & MNT_SOFTDEP)
428 error = softdep_flushfiles(mp, flags, l);
429 else
430 error = ffs_flushfiles(mp, flags, l);
431 if (fs->fs_pendingblocks != 0 ||
432 fs->fs_pendinginodes != 0) {
433 printf("%s: update error: blocks %" PRId64
434 " files %d\n",
435 fs->fs_fsmnt, fs->fs_pendingblocks,
436 fs->fs_pendinginodes);
437 fs->fs_pendingblocks = 0;
438 fs->fs_pendinginodes = 0;
439 }
440 if (error == 0)
441 error = UFS_WAPBL_BEGIN(mp);
442 if (error == 0 &&
443 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
444 fs->fs_clean & FS_WASCLEAN) {
445 if (mp->mnt_flag & MNT_SOFTDEP)
446 fs->fs_flags &= ~FS_DOSOFTDEP;
447 fs->fs_clean = FS_ISCLEAN;
448 (void) ffs_sbupdate(ump, MNT_WAIT);
449 }
450 if (error == 0)
451 UFS_WAPBL_END(mp);
452 if (error)
453 return (error);
454 }
455
456 #ifdef WAPBL
457 if ((mp->mnt_flag & MNT_LOG) == 0) {
458 error = ffs_wapbl_stop(mp, mp->mnt_flag & MNT_FORCE);
459 if (error)
460 return error;
461 }
462 #endif /* WAPBL */
463
464 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
465 /*
466 * Finish change from r/w to r/o
467 */
468 fs->fs_ronly = 1;
469 fs->fs_fmod = 0;
470 }
471
472 /*
473 * Flush soft dependencies if disabling it via an update
474 * mount. This may leave some items to be processed,
475 * so don't do this yet XXX.
476 */
477 if ((fs->fs_flags & FS_DOSOFTDEP) &&
478 !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
479 #ifdef notyet
480 flags = WRITECLOSE;
481 if (mp->mnt_flag & MNT_FORCE)
482 flags |= FORCECLOSE;
483 error = softdep_flushfiles(mp, flags, l);
484 if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
485 fs->fs_flags &= ~FS_DOSOFTDEP;
486 (void) ffs_sbupdate(ump, MNT_WAIT);
487 #elif defined(SOFTDEP)
488 mp->mnt_flag |= MNT_SOFTDEP;
489 #endif
490 }
491
492 /*
493 * When upgrading to a softdep mount, we must first flush
494 * all vnodes. (not done yet -- see above)
495 */
496 if (!(fs->fs_flags & FS_DOSOFTDEP) &&
497 (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
498 #ifdef notyet
499 flags = WRITECLOSE;
500 if (mp->mnt_flag & MNT_FORCE)
501 flags |= FORCECLOSE;
502 error = ffs_flushfiles(mp, flags, l);
503 #else
504 mp->mnt_flag &= ~MNT_SOFTDEP;
505 #endif
506 }
507
508 if (mp->mnt_flag & MNT_RELOAD) {
509 error = ffs_reload(mp, l->l_cred, l);
510 if (error)
511 return (error);
512 }
513
514 if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
515 /*
516 * Changing from read-only to read/write
517 */
518 fs->fs_ronly = 0;
519 fs->fs_clean <<= 1;
520 fs->fs_fmod = 1;
521 if ((fs->fs_flags & FS_DOSOFTDEP)) {
522 error = softdep_mount(devvp, mp, fs,
523 l->l_cred);
524 if (error)
525 return (error);
526 }
527 #ifdef WAPBL
528 if (fs->fs_flags & FS_DOWAPBL) {
529 printf("%s: replaying log to disk\n",
530 fs->fs_fsmnt);
531 KDASSERT(mp->mnt_wapbl_replay);
532 error = wapbl_replay_write(mp->mnt_wapbl_replay,
533 devvp);
534 if (error) {
535 return error;
536 }
537 wapbl_replay_stop(mp->mnt_wapbl_replay);
538 }
539 #endif /* WAPBL */
540 if (fs->fs_snapinum[0] != 0)
541 ffs_snapshot_mount(mp);
542 }
543
544 #ifdef WAPBL
545 error = ffs_wapbl_start(mp);
546 if (error)
547 return error;
548 #endif /* WAPBL */
549
550 if (args->fspec == NULL)
551 return EINVAL;
552 if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
553 (MNT_SOFTDEP | MNT_ASYNC)) {
554 printf("%s fs uses soft updates, ignoring async mode\n",
555 fs->fs_fsmnt);
556 mp->mnt_flag &= ~MNT_ASYNC;
557 }
558 }
559
560 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
561 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
562 if (error == 0)
563 (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
564 sizeof(fs->fs_fsmnt));
565 if (mp->mnt_flag & MNT_SOFTDEP)
566 fs->fs_flags |= FS_DOSOFTDEP;
567 else
568 fs->fs_flags &= ~FS_DOSOFTDEP;
569 if (fs->fs_fmod != 0) { /* XXX */
570 int err;
571
572 fs->fs_fmod = 0;
573 if (fs->fs_clean & FS_WASCLEAN)
574 fs->fs_time = time_second;
575 else {
576 printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
577 mp->mnt_stat.f_mntfromname, fs->fs_clean);
578 printf("%s: lost blocks %" PRId64 " files %d\n",
579 mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
580 fs->fs_pendinginodes);
581 }
582 err = UFS_WAPBL_BEGIN(mp);
583 if (err == 0) {
584 (void) ffs_cgupdate(ump, MNT_WAIT);
585 UFS_WAPBL_END(mp);
586 }
587 }
588 return (error);
589
590 fail:
591 vrele(devvp);
592 return (error);
593 }
594
595 /*
596 * Reload all incore data for a filesystem (used after running fsck on
597 * the root filesystem and finding things to fix). The filesystem must
598 * be mounted read-only.
599 *
600 * Things to do to update the mount:
601 * 1) invalidate all cached meta-data.
602 * 2) re-read superblock from disk.
603 * 3) re-read summary information from disk.
604 * 4) invalidate all inactive vnodes.
605 * 5) invalidate all cached file data.
606 * 6) re-read inode data for all active vnodes.
607 */
608 int
609 ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
610 {
611 struct vnode *vp, *mvp, *devvp;
612 struct inode *ip;
613 void *space;
614 struct buf *bp;
615 struct fs *fs, *newfs;
616 struct partinfo dpart;
617 int i, blks, size, error;
618 int32_t *lp;
619 struct ufsmount *ump;
620 daddr_t sblockloc;
621
622 if ((mp->mnt_flag & MNT_RDONLY) == 0)
623 return (EINVAL);
624
625 ump = VFSTOUFS(mp);
626 /*
627 * Step 1: invalidate all cached meta-data.
628 */
629 devvp = ump->um_devvp;
630 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
631 error = vinvalbuf(devvp, 0, cred, l, 0, 0);
632 VOP_UNLOCK(devvp, 0);
633 if (error)
634 panic("ffs_reload: dirty1");
635 /*
636 * Step 2: re-read superblock from disk.
637 */
638 fs = ump->um_fs;
639 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED) != 0)
640 size = DEV_BSIZE;
641 else
642 size = dpart.disklab->d_secsize;
643 /* XXX we don't handle possibility that superblock moved. */
644 error = bread(devvp, fs->fs_sblockloc / size, fs->fs_sbsize,
645 NOCRED, 0, &bp);
646 if (error) {
647 brelse(bp, 0);
648 return (error);
649 }
650 newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
651 memcpy(newfs, bp->b_data, fs->fs_sbsize);
652 #ifdef FFS_EI
653 if (ump->um_flags & UFS_NEEDSWAP) {
654 ffs_sb_swap((struct fs*)bp->b_data, newfs);
655 fs->fs_flags |= FS_SWAPPED;
656 } else
657 #endif
658 fs->fs_flags &= ~FS_SWAPPED;
659 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
660 newfs->fs_magic != FS_UFS2_MAGIC)||
661 newfs->fs_bsize > MAXBSIZE ||
662 newfs->fs_bsize < sizeof(struct fs)) {
663 brelse(bp, 0);
664 free(newfs, M_UFSMNT);
665 return (EIO); /* XXX needs translation */
666 }
667 /* Store off old fs_sblockloc for fs_oldfscompat_read. */
668 sblockloc = fs->fs_sblockloc;
669 /*
670 * Copy pointer fields back into superblock before copying in XXX
671 * new superblock. These should really be in the ufsmount. XXX
672 * Note that important parameters (eg fs_ncg) are unchanged.
673 */
674 newfs->fs_csp = fs->fs_csp;
675 newfs->fs_maxcluster = fs->fs_maxcluster;
676 newfs->fs_contigdirs = fs->fs_contigdirs;
677 newfs->fs_ronly = fs->fs_ronly;
678 newfs->fs_active = fs->fs_active;
679 memcpy(fs, newfs, (u_int)fs->fs_sbsize);
680 brelse(bp, 0);
681 free(newfs, M_UFSMNT);
682
683 /* Recheck for apple UFS filesystem */
684 ump->um_flags &= ~UFS_ISAPPLEUFS;
685 /* First check to see if this is tagged as an Apple UFS filesystem
686 * in the disklabel
687 */
688 if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) == 0) &&
689 (dpart.part->p_fstype == FS_APPLEUFS)) {
690 ump->um_flags |= UFS_ISAPPLEUFS;
691 }
692 #ifdef APPLE_UFS
693 else {
694 /* Manually look for an apple ufs label, and if a valid one
695 * is found, then treat it like an Apple UFS filesystem anyway
696 */
697 error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
698 APPLEUFS_LABEL_SIZE, cred, 0, &bp);
699 if (error) {
700 brelse(bp, 0);
701 return (error);
702 }
703 error = ffs_appleufs_validate(fs->fs_fsmnt,
704 (struct appleufslabel *)bp->b_data, NULL);
705 if (error == 0)
706 ump->um_flags |= UFS_ISAPPLEUFS;
707 brelse(bp, 0);
708 bp = NULL;
709 }
710 #else
711 if (ump->um_flags & UFS_ISAPPLEUFS)
712 return (EIO);
713 #endif
714
715 if (UFS_MPISAPPLEUFS(ump)) {
716 /* see comment about NeXT below */
717 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
718 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
719 mp->mnt_iflag |= IMNT_DTYPE;
720 } else {
721 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
722 ump->um_dirblksiz = DIRBLKSIZ;
723 if (ump->um_maxsymlinklen > 0)
724 mp->mnt_iflag |= IMNT_DTYPE;
725 else
726 mp->mnt_iflag &= ~IMNT_DTYPE;
727 }
728 ffs_oldfscompat_read(fs, ump, sblockloc);
729 mutex_enter(&ump->um_lock);
730 ump->um_maxfilesize = fs->fs_maxfilesize;
731
732 if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
733 uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
734 mp->mnt_stat.f_mntonname, fs->fs_flags,
735 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
736 if ((mp->mnt_flag & MNT_FORCE) == 0) {
737 mutex_exit(&ump->um_lock);
738 return (EINVAL);
739 }
740 }
741
742 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
743 fs->fs_pendingblocks = 0;
744 fs->fs_pendinginodes = 0;
745 }
746 mutex_exit(&ump->um_lock);
747
748 ffs_statvfs(mp, &mp->mnt_stat);
749 /*
750 * Step 3: re-read summary information from disk.
751 */
752 blks = howmany(fs->fs_cssize, fs->fs_fsize);
753 space = fs->fs_csp;
754 for (i = 0; i < blks; i += fs->fs_frag) {
755 size = fs->fs_bsize;
756 if (i + fs->fs_frag > blks)
757 size = (blks - i) * fs->fs_fsize;
758 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
759 NOCRED, 0, &bp);
760 if (error) {
761 brelse(bp, 0);
762 return (error);
763 }
764 #ifdef FFS_EI
765 if (UFS_FSNEEDSWAP(fs))
766 ffs_csum_swap((struct csum *)bp->b_data,
767 (struct csum *)space, size);
768 else
769 #endif
770 memcpy(space, bp->b_data, (size_t)size);
771 space = (char *)space + size;
772 brelse(bp, 0);
773 }
774 if ((fs->fs_flags & FS_DOSOFTDEP))
775 softdep_mount(devvp, mp, fs, cred);
776 if (fs->fs_snapinum[0] != 0)
777 ffs_snapshot_mount(mp);
778 /*
779 * We no longer know anything about clusters per cylinder group.
780 */
781 if (fs->fs_contigsumsize > 0) {
782 lp = fs->fs_maxcluster;
783 for (i = 0; i < fs->fs_ncg; i++)
784 *lp++ = fs->fs_contigsumsize;
785 }
786
787 /* Allocate a marker vnode. */
788 if ((mvp = vnalloc(mp)) == NULL)
789 return ENOMEM;
790 /*
791 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
792 * and vclean() can be called indirectly
793 */
794 mutex_enter(&mntvnode_lock);
795 loop:
796 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
797 vmark(mvp, vp);
798 if (vp->v_mount != mp || vismarker(vp))
799 continue;
800 /*
801 * Step 4: invalidate all inactive vnodes.
802 */
803 if (vrecycle(vp, &mntvnode_lock, l)) {
804 mutex_enter(&mntvnode_lock);
805 (void)vunmark(mvp);
806 goto loop;
807 }
808 /*
809 * Step 5: invalidate all cached file data.
810 */
811 mutex_enter(&vp->v_interlock);
812 mutex_exit(&mntvnode_lock);
813 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
814 (void)vunmark(mvp);
815 goto loop;
816 }
817 if (vinvalbuf(vp, 0, cred, l, 0, 0))
818 panic("ffs_reload: dirty2");
819 /*
820 * Step 6: re-read inode data for all active vnodes.
821 */
822 ip = VTOI(vp);
823 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
824 (int)fs->fs_bsize, NOCRED, 0, &bp);
825 if (error) {
826 brelse(bp, 0);
827 vput(vp);
828 (void)vunmark(mvp);
829 break;
830 }
831 ffs_load_inode(bp, ip, fs, ip->i_number);
832 ip->i_ffs_effnlink = ip->i_nlink;
833 brelse(bp, 0);
834 vput(vp);
835 mutex_enter(&mntvnode_lock);
836 }
837 mutex_exit(&mntvnode_lock);
838 vnfree(mvp);
839 return (error);
840 }
841
842 /*
843 * Possible superblock locations ordered from most to least likely.
844 */
845 static const int sblock_try[] = SBLOCKSEARCH;
846
847 /*
848 * Common code for mount and mountroot
849 */
850 int
851 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
852 {
853 struct ufsmount *ump;
854 struct buf *bp;
855 struct fs *fs;
856 dev_t dev;
857 struct partinfo dpart;
858 void *space;
859 daddr_t sblockloc, fsblockloc;
860 int blks, fstype;
861 int error, i, size, ronly, bset = 0;
862 #ifdef FFS_EI
863 int needswap = 0; /* keep gcc happy */
864 #endif
865 int32_t *lp;
866 kauth_cred_t cred;
867 u_int32_t sbsize = 8192; /* keep gcc happy*/
868
869 dev = devvp->v_rdev;
870 cred = l ? l->l_cred : NOCRED;
871
872 /* Flush out any old buffers remaining from a previous use. */
873 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
874 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
875 VOP_UNLOCK(devvp, 0);
876 if (error)
877 return (error);
878
879 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
880 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) != 0)
881 size = DEV_BSIZE;
882 else
883 size = dpart.disklab->d_secsize;
884
885 bp = NULL;
886 ump = NULL;
887 fs = NULL;
888 sblockloc = 0;
889 fstype = 0;
890
891 error = fstrans_mount(mp);
892 if (error)
893 return error;
894
895 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
896 memset(ump, 0, sizeof *ump);
897 mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
898 error = ffs_snapshot_init(ump);
899 if (error)
900 goto out;
901 ump->um_ops = &ffs_ufsops;
902
903 #ifdef WAPBL
904 sbagain:
905 #endif
906 /*
907 * Try reading the superblock in each of its possible locations.
908 */
909 for (i = 0; ; i++) {
910 if (bp != NULL) {
911 brelse(bp, BC_NOCACHE);
912 bp = NULL;
913 }
914 if (sblock_try[i] == -1) {
915 error = EINVAL;
916 fs = NULL;
917 goto out;
918 }
919 error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
920 0, &bp);
921 if (error) {
922 fs = NULL;
923 goto out;
924 }
925 fs = (struct fs*)bp->b_data;
926 fsblockloc = sblockloc = sblock_try[i];
927 if (fs->fs_magic == FS_UFS1_MAGIC) {
928 sbsize = fs->fs_sbsize;
929 fstype = UFS1;
930 #ifdef FFS_EI
931 needswap = 0;
932 } else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
933 sbsize = bswap32(fs->fs_sbsize);
934 fstype = UFS1;
935 needswap = 1;
936 #endif
937 } else if (fs->fs_magic == FS_UFS2_MAGIC) {
938 sbsize = fs->fs_sbsize;
939 fstype = UFS2;
940 #ifdef FFS_EI
941 needswap = 0;
942 } else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
943 sbsize = bswap32(fs->fs_sbsize);
944 fstype = UFS2;
945 needswap = 1;
946 #endif
947 } else
948 continue;
949
950
951 /* fs->fs_sblockloc isn't defined for old filesystems */
952 if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
953 if (sblockloc == SBLOCK_UFS2)
954 /*
955 * This is likely to be the first alternate
956 * in a filesystem with 64k blocks.
957 * Don't use it.
958 */
959 continue;
960 fsblockloc = sblockloc;
961 } else {
962 fsblockloc = fs->fs_sblockloc;
963 #ifdef FFS_EI
964 if (needswap)
965 fsblockloc = bswap64(fsblockloc);
966 #endif
967 }
968
969 /* Check we haven't found an alternate superblock */
970 if (fsblockloc != sblockloc)
971 continue;
972
973 /* Validate size of superblock */
974 if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
975 continue;
976
977 /* Ok seems to be a good superblock */
978 break;
979 }
980
981 fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
982 memcpy(fs, bp->b_data, sbsize);
983 ump->um_fs = fs;
984
985 #ifdef FFS_EI
986 if (needswap) {
987 ffs_sb_swap((struct fs*)bp->b_data, fs);
988 fs->fs_flags |= FS_SWAPPED;
989 } else
990 #endif
991 fs->fs_flags &= ~FS_SWAPPED;
992
993 #ifdef WAPBL
994 if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
995 error = ffs_wapbl_replay_start(mp, fs, devvp);
996 if (error)
997 goto out;
998
999 if (!ronly) {
1000 /* XXX fsmnt may be stale. */
1001 printf("%s: replaying log to disk\n", fs->fs_fsmnt);
1002 error = wapbl_replay_write(mp->mnt_wapbl_replay, devvp);
1003 if (error)
1004 goto out;
1005 wapbl_replay_stop(mp->mnt_wapbl_replay);
1006 } else {
1007 /* XXX fsmnt may be stale */
1008 printf("%s: replaying log to memory\n", fs->fs_fsmnt);
1009 }
1010
1011 /* Force a re-read of the superblock */
1012 brelse(bp, BC_INVAL);
1013 bp = NULL;
1014 free(fs, M_UFSMNT);
1015 fs = NULL;
1016 goto sbagain;
1017 }
1018 #else /* !WAPBL */
1019 if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
1020 error = EPERM;
1021 goto out;
1022 }
1023 #endif /* !WAPBL */
1024
1025 ffs_oldfscompat_read(fs, ump, sblockloc);
1026 ump->um_maxfilesize = fs->fs_maxfilesize;
1027
1028 if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
1029 uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
1030 mp->mnt_stat.f_mntonname, fs->fs_flags,
1031 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
1032 if ((mp->mnt_flag & MNT_FORCE) == 0) {
1033 error = EINVAL;
1034 goto out;
1035 }
1036 }
1037
1038 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1039 fs->fs_pendingblocks = 0;
1040 fs->fs_pendinginodes = 0;
1041 }
1042
1043 ump->um_fstype = fstype;
1044 if (fs->fs_sbsize < SBLOCKSIZE)
1045 brelse(bp, BC_INVAL);
1046 else
1047 brelse(bp, 0);
1048 bp = NULL;
1049
1050 /* First check to see if this is tagged as an Apple UFS filesystem
1051 * in the disklabel
1052 */
1053 if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) == 0) &&
1054 (dpart.part->p_fstype == FS_APPLEUFS)) {
1055 ump->um_flags |= UFS_ISAPPLEUFS;
1056 }
1057 #ifdef APPLE_UFS
1058 else {
1059 /* Manually look for an apple ufs label, and if a valid one
1060 * is found, then treat it like an Apple UFS filesystem anyway
1061 */
1062 error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
1063 APPLEUFS_LABEL_SIZE, cred, 0, &bp);
1064 if (error)
1065 goto out;
1066 error = ffs_appleufs_validate(fs->fs_fsmnt,
1067 (struct appleufslabel *)bp->b_data, NULL);
1068 if (error == 0) {
1069 ump->um_flags |= UFS_ISAPPLEUFS;
1070 }
1071 brelse(bp, 0);
1072 bp = NULL;
1073 }
1074 #else
1075 if (ump->um_flags & UFS_ISAPPLEUFS) {
1076 error = EINVAL;
1077 goto out;
1078 }
1079 #endif
1080
1081 #if 0
1082 /*
1083 * XXX This code changes the behaviour of mounting dirty filesystems, to
1084 * XXX require "mount -f ..." to mount them. This doesn't match what
1085 * XXX mount(8) describes and is disabled for now.
1086 */
1087 /*
1088 * If the file system is not clean, don't allow it to be mounted
1089 * unless MNT_FORCE is specified. (Note: MNT_FORCE is always set
1090 * for the root file system.)
1091 */
1092 if (fs->fs_flags & FS_DOWAPBL) {
1093 /*
1094 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
1095 * bit is set, although there's a window in unmount where it
1096 * could be FS_ISCLEAN
1097 */
1098 if ((mp->mnt_flag & MNT_FORCE) == 0 &&
1099 (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
1100 error = EPERM;
1101 goto out;
1102 }
1103 } else
1104 if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
1105 (mp->mnt_flag & MNT_FORCE) == 0) {
1106 error = EPERM;
1107 goto out;
1108 }
1109 #endif
1110
1111 /*
1112 * verify that we can access the last block in the fs
1113 * if we're mounting read/write.
1114 */
1115
1116 if (!ronly) {
1117 error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
1118 cred, 0, &bp);
1119 if (bp->b_bcount != fs->fs_fsize)
1120 error = EINVAL;
1121 if (error) {
1122 bset = BC_INVAL;
1123 goto out;
1124 }
1125 brelse(bp, BC_INVAL);
1126 bp = NULL;
1127 }
1128
1129 fs->fs_ronly = ronly;
1130 /* Don't bump fs_clean if we're replaying journal */
1131 if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN)))
1132 if (ronly == 0) {
1133 fs->fs_clean <<= 1;
1134 fs->fs_fmod = 1;
1135 }
1136 size = fs->fs_cssize;
1137 blks = howmany(size, fs->fs_fsize);
1138 if (fs->fs_contigsumsize > 0)
1139 size += fs->fs_ncg * sizeof(int32_t);
1140 size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1141 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
1142 fs->fs_csp = space;
1143 for (i = 0; i < blks; i += fs->fs_frag) {
1144 size = fs->fs_bsize;
1145 if (i + fs->fs_frag > blks)
1146 size = (blks - i) * fs->fs_fsize;
1147 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
1148 cred, 0, &bp);
1149 if (error) {
1150 free(fs->fs_csp, M_UFSMNT);
1151 goto out;
1152 }
1153 #ifdef FFS_EI
1154 if (needswap)
1155 ffs_csum_swap((struct csum *)bp->b_data,
1156 (struct csum *)space, size);
1157 else
1158 #endif
1159 memcpy(space, bp->b_data, (u_int)size);
1160
1161 space = (char *)space + size;
1162 brelse(bp, 0);
1163 bp = NULL;
1164 }
1165 if (fs->fs_contigsumsize > 0) {
1166 fs->fs_maxcluster = lp = space;
1167 for (i = 0; i < fs->fs_ncg; i++)
1168 *lp++ = fs->fs_contigsumsize;
1169 space = lp;
1170 }
1171 size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1172 fs->fs_contigdirs = space;
1173 space = (char *)space + size;
1174 memset(fs->fs_contigdirs, 0, size);
1175 /* Compatibility for old filesystems - XXX */
1176 if (fs->fs_avgfilesize <= 0)
1177 fs->fs_avgfilesize = AVFILESIZ;
1178 if (fs->fs_avgfpdir <= 0)
1179 fs->fs_avgfpdir = AFPDIR;
1180 fs->fs_active = NULL;
1181 mp->mnt_data = ump;
1182 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
1183 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
1184 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
1185 mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
1186 if (UFS_MPISAPPLEUFS(ump)) {
1187 /* NeXT used to keep short symlinks in the inode even
1188 * when using FS_42INODEFMT. In that case fs->fs_maxsymlinklen
1189 * is probably -1, but we still need to be able to identify
1190 * short symlinks.
1191 */
1192 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
1193 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
1194 mp->mnt_iflag |= IMNT_DTYPE;
1195 } else {
1196 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
1197 ump->um_dirblksiz = DIRBLKSIZ;
1198 if (ump->um_maxsymlinklen > 0)
1199 mp->mnt_iflag |= IMNT_DTYPE;
1200 else
1201 mp->mnt_iflag &= ~IMNT_DTYPE;
1202 }
1203 mp->mnt_fs_bshift = fs->fs_bshift;
1204 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
1205 mp->mnt_flag |= MNT_LOCAL;
1206 mp->mnt_iflag |= IMNT_MPSAFE;
1207 #ifdef FFS_EI
1208 if (needswap)
1209 ump->um_flags |= UFS_NEEDSWAP;
1210 #endif
1211 ump->um_mountp = mp;
1212 ump->um_dev = dev;
1213 ump->um_devvp = devvp;
1214 ump->um_nindir = fs->fs_nindir;
1215 ump->um_lognindir = ffs(fs->fs_nindir) - 1;
1216 ump->um_bptrtodb = fs->fs_fsbtodb;
1217 ump->um_seqinc = fs->fs_frag;
1218 for (i = 0; i < MAXQUOTAS; i++)
1219 ump->um_quotas[i] = NULLVP;
1220 devvp->v_specmountpoint = mp;
1221 if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
1222 error = softdep_mount(devvp, mp, fs, cred);
1223 if (error) {
1224 free(fs->fs_csp, M_UFSMNT);
1225 goto out;
1226 }
1227 }
1228
1229 #ifdef WAPBL
1230 if (!ronly) {
1231 KDASSERT(fs->fs_ronly == 0);
1232 error = ffs_wapbl_start(mp);
1233 if (error) {
1234 free(fs->fs_csp, M_UFSMNT);
1235 goto out;
1236 }
1237 }
1238 #endif /* WAPBL */
1239
1240 if (ronly == 0 && fs->fs_snapinum[0] != 0)
1241 ffs_snapshot_mount(mp);
1242 #ifdef UFS_EXTATTR
1243 /*
1244 * Initialize file-backed extended attributes on UFS1 file
1245 * systems.
1246 */
1247 if (ump->um_fstype == UFS1) {
1248 ufs_extattr_uepm_init(&ump->um_extattr);
1249 #ifdef UFS_EXTATTR_AUTOSTART
1250 /*
1251 * XXX Just ignore errors. Not clear that we should
1252 * XXX fail the mount in this case.
1253 */
1254 (void) ufs_extattr_autostart(mp, l);
1255 #endif
1256 }
1257 #endif /* UFS_EXTATTR */
1258 return (0);
1259 out:
1260 #ifdef WAPBL
1261 if (mp->mnt_wapbl_replay) {
1262 if (wapbl_replay_isopen(mp->mnt_wapbl_replay))
1263 wapbl_replay_stop(mp->mnt_wapbl_replay);
1264 wapbl_replay_free(mp->mnt_wapbl_replay);
1265 mp->mnt_wapbl_replay = 0;
1266 }
1267 #endif
1268
1269 fstrans_unmount(mp);
1270 if (fs)
1271 free(fs, M_UFSMNT);
1272 devvp->v_specmountpoint = NULL;
1273 if (bp)
1274 brelse(bp, bset);
1275 if (ump) {
1276 if (ump->um_oldfscompat)
1277 free(ump->um_oldfscompat, M_UFSMNT);
1278 mutex_destroy(&ump->um_lock);
1279 free(ump, M_UFSMNT);
1280 mp->mnt_data = NULL;
1281 }
1282 return (error);
1283 }
1284
1285 /*
1286 * Sanity checks for loading old filesystem superblocks.
1287 * See ffs_oldfscompat_write below for unwound actions.
1288 *
1289 * XXX - Parts get retired eventually.
1290 * Unfortunately new bits get added.
1291 */
1292 static void
1293 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
1294 {
1295 off_t maxfilesize;
1296 int32_t *extrasave;
1297
1298 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1299 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1300 return;
1301
1302 if (!ump->um_oldfscompat)
1303 ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
1304 M_UFSMNT, M_WAITOK);
1305
1306 memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
1307 extrasave = ump->um_oldfscompat;
1308 extrasave += 512/sizeof(int32_t);
1309 extrasave[0] = fs->fs_old_npsect;
1310 extrasave[1] = fs->fs_old_interleave;
1311 extrasave[2] = fs->fs_old_trackskew;
1312
1313 /* These fields will be overwritten by their
1314 * original values in fs_oldfscompat_write, so it is harmless
1315 * to modify them here.
1316 */
1317 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1318 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1319 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1320 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1321
1322 fs->fs_maxbsize = fs->fs_bsize;
1323 fs->fs_time = fs->fs_old_time;
1324 fs->fs_size = fs->fs_old_size;
1325 fs->fs_dsize = fs->fs_old_dsize;
1326 fs->fs_csaddr = fs->fs_old_csaddr;
1327 fs->fs_sblockloc = sblockloc;
1328
1329 fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
1330
1331 if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
1332 fs->fs_old_nrpos = 8;
1333 fs->fs_old_npsect = fs->fs_old_nsect;
1334 fs->fs_old_interleave = 1;
1335 fs->fs_old_trackskew = 0;
1336 }
1337
1338 if (fs->fs_old_inodefmt < FS_44INODEFMT) {
1339 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
1340 fs->fs_qbmask = ~fs->fs_bmask;
1341 fs->fs_qfmask = ~fs->fs_fmask;
1342 }
1343
1344 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1345 if (fs->fs_maxfilesize > maxfilesize)
1346 fs->fs_maxfilesize = maxfilesize;
1347
1348 /* Compatibility for old filesystems */
1349 if (fs->fs_avgfilesize <= 0)
1350 fs->fs_avgfilesize = AVFILESIZ;
1351 if (fs->fs_avgfpdir <= 0)
1352 fs->fs_avgfpdir = AFPDIR;
1353
1354 #if 0
1355 if (bigcgs) {
1356 fs->fs_save_cgsize = fs->fs_cgsize;
1357 fs->fs_cgsize = fs->fs_bsize;
1358 }
1359 #endif
1360 }
1361
1362 /*
1363 * Unwinding superblock updates for old filesystems.
1364 * See ffs_oldfscompat_read above for details.
1365 *
1366 * XXX - Parts get retired eventually.
1367 * Unfortunately new bits get added.
1368 */
1369 static void
1370 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
1371 {
1372 int32_t *extrasave;
1373
1374 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1375 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1376 return;
1377
1378 fs->fs_old_time = fs->fs_time;
1379 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1380 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1381 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1382 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1383 fs->fs_old_flags = fs->fs_flags;
1384
1385 #if 0
1386 if (bigcgs) {
1387 fs->fs_cgsize = fs->fs_save_cgsize;
1388 }
1389 #endif
1390
1391 memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
1392 extrasave = ump->um_oldfscompat;
1393 extrasave += 512/sizeof(int32_t);
1394 fs->fs_old_npsect = extrasave[0];
1395 fs->fs_old_interleave = extrasave[1];
1396 fs->fs_old_trackskew = extrasave[2];
1397
1398 }
1399
1400 /*
1401 * unmount system call
1402 */
1403 int
1404 ffs_unmount(struct mount *mp, int mntflags)
1405 {
1406 struct lwp *l = curlwp;
1407 struct ufsmount *ump = VFSTOUFS(mp);
1408 struct fs *fs = ump->um_fs;
1409 int error, flags, penderr;
1410 #ifdef WAPBL
1411 extern int doforce;
1412 #endif
1413
1414 penderr = 0;
1415 flags = 0;
1416 if (mntflags & MNT_FORCE)
1417 flags |= FORCECLOSE;
1418 #ifdef UFS_EXTATTR
1419 if (ump->um_fstype == UFS1) {
1420 ufs_extattr_stop(mp, l);
1421 ufs_extattr_uepm_destroy(&ump->um_extattr);
1422 }
1423 #endif /* UFS_EXTATTR */
1424 if (mp->mnt_flag & MNT_SOFTDEP) {
1425 if ((error = softdep_flushfiles(mp, flags, l)) != 0)
1426 return (error);
1427 } else {
1428 if ((error = ffs_flushfiles(mp, flags, l)) != 0)
1429 return (error);
1430 }
1431 mutex_enter(&ump->um_lock);
1432 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1433 printf("%s: unmount pending error: blocks %" PRId64
1434 " files %d\n",
1435 fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
1436 fs->fs_pendingblocks = 0;
1437 fs->fs_pendinginodes = 0;
1438 penderr = 1;
1439 }
1440 mutex_exit(&ump->um_lock);
1441 error = UFS_WAPBL_BEGIN(mp);
1442 if (error == 0)
1443 if (fs->fs_ronly == 0 &&
1444 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1445 fs->fs_clean & FS_WASCLEAN) {
1446 /*
1447 * XXXX don't mark fs clean in the case of softdep
1448 * pending block errors, until they are fixed.
1449 */
1450 if (penderr == 0) {
1451 if (mp->mnt_flag & MNT_SOFTDEP)
1452 fs->fs_flags &= ~FS_DOSOFTDEP;
1453 fs->fs_clean = FS_ISCLEAN;
1454 }
1455 fs->fs_fmod = 0;
1456 (void) ffs_sbupdate(ump, MNT_WAIT);
1457 }
1458 if (error == 0)
1459 UFS_WAPBL_END(mp);
1460 #ifdef WAPBL
1461 KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
1462 if (mp->mnt_wapbl_replay) {
1463 KDASSERT(fs->fs_ronly);
1464 wapbl_replay_stop(mp->mnt_wapbl_replay);
1465 wapbl_replay_free(mp->mnt_wapbl_replay);
1466 mp->mnt_wapbl_replay = 0;
1467 }
1468 error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
1469 if (error) {
1470 return error;
1471 }
1472 #endif /* WAPBL */
1473 if (ump->um_devvp->v_type != VBAD)
1474 ump->um_devvp->v_specmountpoint = NULL;
1475 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1476 (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
1477 NOCRED);
1478 vput(ump->um_devvp);
1479 free(fs->fs_csp, M_UFSMNT);
1480 free(fs, M_UFSMNT);
1481 if (ump->um_oldfscompat != NULL)
1482 free(ump->um_oldfscompat, M_UFSMNT);
1483 softdep_unmount(mp);
1484 mutex_destroy(&ump->um_lock);
1485 ffs_snapshot_fini(ump);
1486 free(ump, M_UFSMNT);
1487 mp->mnt_data = NULL;
1488 mp->mnt_flag &= ~MNT_LOCAL;
1489 fstrans_unmount(mp);
1490 return (0);
1491 }
1492
1493 /*
1494 * Flush out all the files in a filesystem.
1495 */
1496 int
1497 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
1498 {
1499 extern int doforce;
1500 struct ufsmount *ump;
1501 int error;
1502
1503 if (!doforce)
1504 flags &= ~FORCECLOSE;
1505 ump = VFSTOUFS(mp);
1506 #ifdef QUOTA
1507 if (mp->mnt_flag & MNT_QUOTA) {
1508 int i;
1509 if ((error = vflush(mp, NULLVP, SKIPSYSTEM | flags)) != 0)
1510 return (error);
1511 for (i = 0; i < MAXQUOTAS; i++) {
1512 if (ump->um_quotas[i] == NULLVP)
1513 continue;
1514 quotaoff(l, mp, i);
1515 }
1516 /*
1517 * Here we fall through to vflush again to ensure
1518 * that we have gotten rid of all the system vnodes.
1519 */
1520 }
1521 #endif
1522 if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
1523 return (error);
1524 ffs_snapshot_unmount(mp);
1525 /*
1526 * Flush all the files.
1527 */
1528 error = vflush(mp, NULLVP, flags);
1529 if (error)
1530 return (error);
1531 /*
1532 * Flush filesystem metadata.
1533 */
1534 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1535 error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
1536 VOP_UNLOCK(ump->um_devvp, 0);
1537 if (flags & FORCECLOSE) /* XXXDBJ */
1538 error = 0;
1539
1540 #ifdef WAPBL
1541 if (error)
1542 return error;
1543 if (mp->mnt_wapbl) {
1544 error = wapbl_flush(mp->mnt_wapbl, 1);
1545 if (flags & FORCECLOSE)
1546 error = 0;
1547 }
1548 #endif
1549
1550 return (error);
1551 }
1552
1553 /*
1554 * Get file system statistics.
1555 */
1556 int
1557 ffs_statvfs(struct mount *mp, struct statvfs *sbp)
1558 {
1559 struct ufsmount *ump;
1560 struct fs *fs;
1561
1562 ump = VFSTOUFS(mp);
1563 fs = ump->um_fs;
1564 mutex_enter(&ump->um_lock);
1565 sbp->f_bsize = fs->fs_bsize;
1566 sbp->f_frsize = fs->fs_fsize;
1567 sbp->f_iosize = fs->fs_bsize;
1568 sbp->f_blocks = fs->fs_dsize;
1569 sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1570 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1571 sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
1572 fs->fs_minfree) / (u_int64_t) 100;
1573 if (sbp->f_bfree > sbp->f_bresvd)
1574 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1575 else
1576 sbp->f_bavail = 0;
1577 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1578 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1579 sbp->f_favail = sbp->f_ffree;
1580 sbp->f_fresvd = 0;
1581 mutex_exit(&ump->um_lock);
1582 copy_statvfs_info(sbp, mp);
1583
1584 return (0);
1585 }
1586
1587 /*
1588 * Go through the disk queues to initiate sandbagged IO;
1589 * go through the inodes to write those that have been modified;
1590 * initiate the writing of the super block if it has been modified.
1591 *
1592 * Note: we are always called with the filesystem marked `MPBUSY'.
1593 */
1594 int
1595 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
1596 {
1597 struct lwp *l = curlwp;
1598 struct vnode *vp, *mvp;
1599 struct inode *ip;
1600 struct ufsmount *ump = VFSTOUFS(mp);
1601 struct fs *fs;
1602 int error, count, allerror = 0;
1603
1604 fs = ump->um_fs;
1605 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1606 printf("fs = %s\n", fs->fs_fsmnt);
1607 panic("update: rofs mod");
1608 }
1609
1610 /* Allocate a marker vnode. */
1611 if ((mvp = vnalloc(mp)) == NULL)
1612 return (ENOMEM);
1613
1614 fstrans_start(mp, FSTRANS_SHARED);
1615 /*
1616 * Write back each (modified) inode.
1617 */
1618 mutex_enter(&mntvnode_lock);
1619 loop:
1620 /*
1621 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
1622 * and vclean() can be called indirectly
1623 */
1624 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
1625 vmark(mvp, vp);
1626 /*
1627 * If the vnode that we are about to sync is no longer
1628 * associated with this mount point, start over.
1629 */
1630 if (vp->v_mount != mp || vismarker(vp))
1631 continue;
1632 mutex_enter(&vp->v_interlock);
1633 ip = VTOI(vp);
1634 /* XXXpooka: why wapbl check? */
1635 if (ip == NULL || (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0 ||
1636 vp->v_type == VNON || ((ip->i_flag &
1637 (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
1638 (LIST_EMPTY(&vp->v_dirtyblkhd) || (mp->mnt_wapbl)) &&
1639 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
1640 {
1641 mutex_exit(&vp->v_interlock);
1642 continue;
1643 }
1644 if (vp->v_type == VBLK &&
1645 fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
1646 mutex_exit(&vp->v_interlock);
1647 continue;
1648 }
1649 mutex_exit(&mntvnode_lock);
1650 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1651 if (error) {
1652 mutex_enter(&mntvnode_lock);
1653 if (error == ENOENT) {
1654 (void)vunmark(mvp);
1655 goto loop;
1656 }
1657 continue;
1658 }
1659 if (vp->v_type == VREG && waitfor == MNT_LAZY) {
1660 error = UFS_WAPBL_BEGIN(vp->v_mount);
1661 if (!error) {
1662 error = ffs_update(vp, NULL, NULL, 0);
1663 UFS_WAPBL_END(vp->v_mount);
1664 }
1665 } else {
1666 error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
1667 (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
1668 }
1669 if (error)
1670 allerror = error;
1671 vput(vp);
1672 mutex_enter(&mntvnode_lock);
1673 }
1674 mutex_exit(&mntvnode_lock);
1675 /*
1676 * Force stale file system control information to be flushed.
1677 */
1678 if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
1679 if ((error = softdep_flushworklist(ump->um_mountp, &count, l)))
1680 allerror = error;
1681 /* Flushed work items may create new vnodes to clean */
1682 if (allerror == 0 && count) {
1683 mutex_enter(&mntvnode_lock);
1684 goto loop;
1685 }
1686 }
1687 if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
1688 !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
1689 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1690 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1691 (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
1692 0, 0)) != 0)
1693 allerror = error;
1694 VOP_UNLOCK(ump->um_devvp, 0);
1695 if (allerror == 0 && waitfor == MNT_WAIT && !mp->mnt_wapbl) {
1696 mutex_enter(&mntvnode_lock);
1697 goto loop;
1698 }
1699 }
1700 #ifdef QUOTA
1701 qsync(mp);
1702 #endif
1703 /*
1704 * Write back modified superblock.
1705 */
1706 if (fs->fs_fmod != 0) {
1707 fs->fs_fmod = 0;
1708 fs->fs_time = time_second;
1709 error = UFS_WAPBL_BEGIN(mp);
1710 if (error)
1711 allerror = error;
1712 else {
1713 if ((error = ffs_cgupdate(ump, waitfor)))
1714 allerror = error;
1715 UFS_WAPBL_END(mp);
1716 }
1717 }
1718
1719 #ifdef WAPBL
1720 if (mp->mnt_wapbl) {
1721 error = wapbl_flush(mp->mnt_wapbl, 0);
1722 if (error)
1723 allerror = error;
1724 }
1725 #endif
1726
1727 fstrans_done(mp);
1728 vnfree(mvp);
1729 return (allerror);
1730 }
1731
1732 /*
1733 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1734 * in from disk. If it is in core, wait for the lock bit to clear, then
1735 * return the inode locked. Detection and handling of mount points must be
1736 * done by the calling routine.
1737 */
1738 int
1739 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1740 {
1741 struct fs *fs;
1742 struct inode *ip;
1743 struct ufsmount *ump;
1744 struct buf *bp;
1745 struct vnode *vp;
1746 dev_t dev;
1747 int error;
1748
1749 ump = VFSTOUFS(mp);
1750 dev = ump->um_dev;
1751
1752 retry:
1753 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1754 return (0);
1755
1756 /* Allocate a new vnode/inode. */
1757 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1758 *vpp = NULL;
1759 return (error);
1760 }
1761 ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
1762
1763 /*
1764 * If someone beat us to it, put back the freshly allocated
1765 * vnode/inode pair and retry.
1766 */
1767 mutex_enter(&ufs_hashlock);
1768 if (ufs_ihashget(dev, ino, 0) != NULL) {
1769 mutex_exit(&ufs_hashlock);
1770 ungetnewvnode(vp);
1771 pool_cache_put(ffs_inode_cache, ip);
1772 goto retry;
1773 }
1774
1775 vp->v_vflag |= VV_LOCKSWORK;
1776 if ((mp->mnt_flag & MNT_SOFTDEP) != 0)
1777 vp->v_uflag |= VU_SOFTDEP;
1778
1779 /*
1780 * XXX MFS ends up here, too, to allocate an inode. Should we
1781 * XXX create another pool for MFS inodes?
1782 */
1783
1784 memset(ip, 0, sizeof(struct inode));
1785 vp->v_data = ip;
1786 ip->i_vnode = vp;
1787 ip->i_ump = ump;
1788 ip->i_fs = fs = ump->um_fs;
1789 ip->i_dev = dev;
1790 ip->i_number = ino;
1791 LIST_INIT(&ip->i_pcbufhd);
1792 #ifdef QUOTA
1793 ufsquota_init(ip);
1794 #endif
1795
1796 /*
1797 * Initialize genfs node, we might proceed to destroy it in
1798 * error branches.
1799 */
1800 genfs_node_init(vp, &ffs_genfsops);
1801
1802 /*
1803 * Put it onto its hash chain and lock it so that other requests for
1804 * this inode will block if they arrive while we are sleeping waiting
1805 * for old data structures to be purged or for the contents of the
1806 * disk portion of this inode to be read.
1807 */
1808
1809 ufs_ihashins(ip);
1810 mutex_exit(&ufs_hashlock);
1811
1812 /* Read in the disk contents for the inode, copy into the inode. */
1813 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1814 (int)fs->fs_bsize, NOCRED, 0, &bp);
1815 if (error) {
1816
1817 /*
1818 * The inode does not contain anything useful, so it would
1819 * be misleading to leave it on its hash chain. With mode
1820 * still zero, it will be unlinked and returned to the free
1821 * list by vput().
1822 */
1823
1824 vput(vp);
1825 brelse(bp, 0);
1826 *vpp = NULL;
1827 return (error);
1828 }
1829 if (ip->i_ump->um_fstype == UFS1)
1830 ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
1831 PR_WAITOK);
1832 else
1833 ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
1834 PR_WAITOK);
1835 ffs_load_inode(bp, ip, fs, ino);
1836 if (DOINGSOFTDEP(vp))
1837 softdep_load_inodeblock(ip);
1838 else
1839 ip->i_ffs_effnlink = ip->i_nlink;
1840 brelse(bp, 0);
1841
1842 /*
1843 * Initialize the vnode from the inode, check for aliases.
1844 * Note that the underlying vnode may have changed.
1845 */
1846
1847 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1848
1849 /*
1850 * Finish inode initialization now that aliasing has been resolved.
1851 */
1852
1853 ip->i_devvp = ump->um_devvp;
1854 VREF(ip->i_devvp);
1855
1856 /*
1857 * Ensure that uid and gid are correct. This is a temporary
1858 * fix until fsck has been changed to do the update.
1859 */
1860
1861 if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1862 ip->i_uid = ip->i_ffs1_ouid; /* XXX */
1863 ip->i_gid = ip->i_ffs1_ogid; /* XXX */
1864 } /* XXX */
1865 uvm_vnp_setsize(vp, ip->i_size);
1866 *vpp = vp;
1867 return (0);
1868 }
1869
1870 /*
1871 * File handle to vnode
1872 *
1873 * Have to be really careful about stale file handles:
1874 * - check that the inode number is valid
1875 * - call ffs_vget() to get the locked inode
1876 * - check for an unallocated inode (i_mode == 0)
1877 * - check that the given client host has export rights and return
1878 * those rights via. exflagsp and credanonp
1879 */
1880 int
1881 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1882 {
1883 struct ufid ufh;
1884 struct fs *fs;
1885
1886 if (fhp->fid_len != sizeof(struct ufid))
1887 return EINVAL;
1888
1889 memcpy(&ufh, fhp, sizeof(ufh));
1890 fs = VFSTOUFS(mp)->um_fs;
1891 if (ufh.ufid_ino < ROOTINO ||
1892 ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1893 return (ESTALE);
1894 return (ufs_fhtovp(mp, &ufh, vpp));
1895 }
1896
1897 /*
1898 * Vnode pointer to File handle
1899 */
1900 /* ARGSUSED */
1901 int
1902 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1903 {
1904 struct inode *ip;
1905 struct ufid ufh;
1906
1907 if (*fh_size < sizeof(struct ufid)) {
1908 *fh_size = sizeof(struct ufid);
1909 return E2BIG;
1910 }
1911 ip = VTOI(vp);
1912 *fh_size = sizeof(struct ufid);
1913 memset(&ufh, 0, sizeof(ufh));
1914 ufh.ufid_len = sizeof(struct ufid);
1915 ufh.ufid_ino = ip->i_number;
1916 ufh.ufid_gen = ip->i_gen;
1917 memcpy(fhp, &ufh, sizeof(ufh));
1918 return (0);
1919 }
1920
1921 void
1922 ffs_init(void)
1923 {
1924 if (ffs_initcount++ > 0)
1925 return;
1926
1927 ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
1928 "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
1929 ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
1930 "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
1931 ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
1932 "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
1933 softdep_initialize();
1934 ufs_init();
1935 }
1936
1937 void
1938 ffs_reinit(void)
1939 {
1940 softdep_reinitialize();
1941 ufs_reinit();
1942 }
1943
1944 void
1945 ffs_done(void)
1946 {
1947 if (--ffs_initcount > 0)
1948 return;
1949
1950 /* XXX softdep cleanup ? */
1951 ufs_done();
1952 pool_cache_destroy(ffs_dinode2_cache);
1953 pool_cache_destroy(ffs_dinode1_cache);
1954 pool_cache_destroy(ffs_inode_cache);
1955 }
1956
1957 SYSCTL_SETUP(sysctl_vfs_ffs_setup, "sysctl vfs.ffs subtree setup")
1958 {
1959 #if 0
1960 extern int doasyncfree;
1961 #endif
1962 extern int ffs_log_changeopt;
1963
1964 sysctl_createv(clog, 0, NULL, NULL,
1965 CTLFLAG_PERMANENT,
1966 CTLTYPE_NODE, "vfs", NULL,
1967 NULL, 0, NULL, 0,
1968 CTL_VFS, CTL_EOL);
1969 sysctl_createv(clog, 0, NULL, NULL,
1970 CTLFLAG_PERMANENT,
1971 CTLTYPE_NODE, "ffs",
1972 SYSCTL_DESCR("Berkeley Fast File System"),
1973 NULL, 0, NULL, 0,
1974 CTL_VFS, 1, CTL_EOL);
1975
1976 /*
1977 * @@@ should we even bother with these first three?
1978 */
1979 sysctl_createv(clog, 0, NULL, NULL,
1980 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1981 CTLTYPE_INT, "doclusterread", NULL,
1982 sysctl_notavail, 0, NULL, 0,
1983 CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
1984 sysctl_createv(clog, 0, NULL, NULL,
1985 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1986 CTLTYPE_INT, "doclusterwrite", NULL,
1987 sysctl_notavail, 0, NULL, 0,
1988 CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
1989 sysctl_createv(clog, 0, NULL, NULL,
1990 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1991 CTLTYPE_INT, "doreallocblks", NULL,
1992 sysctl_notavail, 0, NULL, 0,
1993 CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
1994 #if 0
1995 sysctl_createv(clog, 0, NULL, NULL,
1996 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1997 CTLTYPE_INT, "doasyncfree",
1998 SYSCTL_DESCR("Release dirty blocks asynchronously"),
1999 NULL, 0, &doasyncfree, 0,
2000 CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
2001 #endif
2002 sysctl_createv(clog, 0, NULL, NULL,
2003 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2004 CTLTYPE_INT, "log_changeopt",
2005 SYSCTL_DESCR("Log changes in optimization strategy"),
2006 NULL, 0, &ffs_log_changeopt, 0,
2007 CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
2008 }
2009
2010 /*
2011 * Write a superblock and associated information back to disk.
2012 */
2013 int
2014 ffs_sbupdate(struct ufsmount *mp, int waitfor)
2015 {
2016 struct fs *fs = mp->um_fs;
2017 struct buf *bp;
2018 int error = 0;
2019 u_int32_t saveflag;
2020
2021 error = ffs_getblk(mp->um_devvp,
2022 fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb), FFS_NOBLK,
2023 fs->fs_sbsize, false, &bp);
2024 if (error)
2025 return error;
2026 saveflag = fs->fs_flags & FS_INTERNAL;
2027 fs->fs_flags &= ~FS_INTERNAL;
2028
2029 memcpy(bp->b_data, fs, fs->fs_sbsize);
2030
2031 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
2032 #ifdef FFS_EI
2033 if (mp->um_flags & UFS_NEEDSWAP)
2034 ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
2035 #endif
2036 fs->fs_flags |= saveflag;
2037
2038 if (waitfor == MNT_WAIT)
2039 error = bwrite(bp);
2040 else
2041 bawrite(bp);
2042 return (error);
2043 }
2044
2045 int
2046 ffs_cgupdate(struct ufsmount *mp, int waitfor)
2047 {
2048 struct fs *fs = mp->um_fs;
2049 struct buf *bp;
2050 int blks;
2051 void *space;
2052 int i, size, error = 0, allerror = 0;
2053
2054 allerror = ffs_sbupdate(mp, waitfor);
2055 blks = howmany(fs->fs_cssize, fs->fs_fsize);
2056 space = fs->fs_csp;
2057 for (i = 0; i < blks; i += fs->fs_frag) {
2058 size = fs->fs_bsize;
2059 if (i + fs->fs_frag > blks)
2060 size = (blks - i) * fs->fs_fsize;
2061 error = ffs_getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
2062 FFS_NOBLK, size, false, &bp);
2063 if (error)
2064 break;
2065 #ifdef FFS_EI
2066 if (mp->um_flags & UFS_NEEDSWAP)
2067 ffs_csum_swap((struct csum*)space,
2068 (struct csum*)bp->b_data, size);
2069 else
2070 #endif
2071 memcpy(bp->b_data, space, (u_int)size);
2072 space = (char *)space + size;
2073 if (waitfor == MNT_WAIT)
2074 error = bwrite(bp);
2075 else
2076 bawrite(bp);
2077 }
2078 if (!allerror && error)
2079 allerror = error;
2080 return (allerror);
2081 }
2082
2083 int
2084 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
2085 int attrnamespace, const char *attrname)
2086 {
2087 #ifdef UFS_EXTATTR
2088 /*
2089 * File-backed extended attributes are only supported on UFS1.
2090 * UFS2 has native extended attributes.
2091 */
2092 if (VFSTOUFS(mp)->um_fstype == UFS1)
2093 return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
2094 #endif
2095 return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
2096 }
2097
2098 int
2099 ffs_suspendctl(struct mount *mp, int cmd)
2100 {
2101 int error;
2102 struct lwp *l = curlwp;
2103
2104 switch (cmd) {
2105 case SUSPEND_SUSPEND:
2106 if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
2107 return error;
2108 error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
2109 if (error == 0)
2110 error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
2111 if (error != 0) {
2112 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
2113 return error;
2114 }
2115 return 0;
2116
2117 case SUSPEND_RESUME:
2118 return fstrans_setstate(mp, FSTRANS_NORMAL);
2119
2120 default:
2121 return EINVAL;
2122 }
2123 }
2124