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