ffs_vfsops.c revision 1.229.2.1 1 /* $NetBSD: ffs_vfsops.c,v 1.229.2.1 2008/06/10 14:51:23 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.1 2008/06/10 14:51:23 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 /*
1089 * If the file system is not clean, don't allow it to be mounted
1090 * unless MNT_FORCE is specified. (Note: MNT_FORCE is always set
1091 * for the root file system.)
1092 */
1093 if (fs->fs_flags & FS_DOWAPBL) {
1094 /*
1095 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
1096 * bit is set, although there's a window in unmount where it
1097 * could be FS_ISCLEAN
1098 */
1099 if ((mp->mnt_flag & MNT_FORCE) == 0 &&
1100 (fs->fs_clean & (FS_WASCLEAN|FS_ISCLEAN)) == 0) {
1101 error = EPERM;
1102 goto out;
1103 }
1104 } else
1105 if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
1106 (mp->mnt_flag & MNT_FORCE) == 0) {
1107 error = EPERM;
1108 goto out;
1109 }
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