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