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