1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: stable/12/sys/ufs/ffs/ffs_vfsops.c 371940 2022-04-10 05:02:22Z git2svn $");
36
37 #include "opt_quota.h"
38 #include "opt_ufs.h"
39 #include "opt_ffs.h"
40 #include "opt_ddb.h"
41
42 #include <sys/param.h>
43 #include <sys/gsb_crc32.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/taskqueue.h>
49 #include <sys/kernel.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/bio.h>
53 #include <sys/buf.h>
54 #include <sys/conf.h>
55 #include <sys/fcntl.h>
56 #include <sys/ioccom.h>
57 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/rwlock.h>
60 #include <sys/vmmeter.h>
61
62 #include <security/mac/mac_framework.h>
63
64 #include <ufs/ufs/dir.h>
65 #include <ufs/ufs/extattr.h>
66 #include <ufs/ufs/gjournal.h>
67 #include <ufs/ufs/quota.h>
68 #include <ufs/ufs/ufsmount.h>
69 #include <ufs/ufs/inode.h>
70 #include <ufs/ufs/ufs_extern.h>
71
72 #include <ufs/ffs/fs.h>
73 #include <ufs/ffs/ffs_extern.h>
74
75 #include <vm/vm.h>
76 #include <vm/uma.h>
77 #include <vm/vm_page.h>
78
79 #include <geom/geom.h>
80 #include <geom/geom_vfs.h>
81
82 #include <ddb/ddb.h>
83
84 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
85
86 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
87 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
88 ufs2_daddr_t);
89 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
90 static int ffs_sync_lazy(struct mount *mp);
91 static int ffs_use_bread(void *devfd, off_t loc, void **bufp, int size);
92 static int ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size);
93
94 static vfs_init_t ffs_init;
95 static vfs_uninit_t ffs_uninit;
96 static vfs_extattrctl_t ffs_extattrctl;
97 static vfs_cmount_t ffs_cmount;
98 static vfs_unmount_t ffs_unmount;
99 static vfs_mount_t ffs_mount;
100 static vfs_statfs_t ffs_statfs;
101 static vfs_fhtovp_t ffs_fhtovp;
102 static vfs_sync_t ffs_sync;
103
104 static struct vfsops ufs_vfsops = {
105 .vfs_extattrctl = ffs_extattrctl,
106 .vfs_fhtovp = ffs_fhtovp,
107 .vfs_init = ffs_init,
108 .vfs_mount = ffs_mount,
109 .vfs_cmount = ffs_cmount,
110 .vfs_quotactl = ufs_quotactl,
111 .vfs_root = ufs_root,
112 .vfs_statfs = ffs_statfs,
113 .vfs_sync = ffs_sync,
114 .vfs_uninit = ffs_uninit,
115 .vfs_unmount = ffs_unmount,
116 .vfs_vget = ffs_vget,
117 .vfs_susp_clean = process_deferred_inactive,
118 };
119
120 VFS_SET(ufs_vfsops, ufs, 0);
121 MODULE_VERSION(ufs, 1);
122
123 static b_strategy_t ffs_geom_strategy;
124 static b_write_t ffs_bufwrite;
125
126 static struct buf_ops ffs_ops = {
127 .bop_name = "FFS",
128 .bop_write = ffs_bufwrite,
129 .bop_strategy = ffs_geom_strategy,
130 .bop_sync = bufsync,
131 #ifdef NO_FFS_SNAPSHOT
132 .bop_bdflush = bufbdflush,
133 #else
134 .bop_bdflush = ffs_bdflush,
135 #endif
136 };
137
138 /*
139 * Note that userquota and groupquota options are not currently used
140 * by UFS/FFS code and generally mount(8) does not pass those options
141 * from userland, but they can be passed by loader(8) via
142 * vfs.root.mountfrom.options.
143 */
144 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
145 "noclusterw", "noexec", "export", "force", "from", "groupquota",
146 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
147 "nosymfollow", "sync", "union", "userquota", "untrusted", NULL };
148
149 static int
ffs_mount(struct mount * mp)150 ffs_mount(struct mount *mp)
151 {
152 struct vnode *devvp;
153 struct thread *td;
154 struct ufsmount *ump = NULL;
155 struct fs *fs;
156 pid_t fsckpid = 0;
157 int error, error1, flags;
158 uint64_t mntorflags, saved_mnt_flag;
159 accmode_t accmode;
160 struct nameidata ndp;
161 char *fspec;
162
163 td = curthread;
164 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
165 return (EINVAL);
166 if (uma_inode == NULL) {
167 uma_inode = uma_zcreate("FFS inode",
168 sizeof(struct inode), NULL, NULL, NULL, NULL,
169 UMA_ALIGN_PTR, 0);
170 uma_ufs1 = uma_zcreate("FFS1 dinode",
171 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
172 UMA_ALIGN_PTR, 0);
173 uma_ufs2 = uma_zcreate("FFS2 dinode",
174 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
175 UMA_ALIGN_PTR, 0);
176 }
177
178 vfs_deleteopt(mp->mnt_optnew, "groupquota");
179 vfs_deleteopt(mp->mnt_optnew, "userquota");
180
181 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
182 if (error)
183 return (error);
184
185 mntorflags = 0;
186 if (vfs_getopt(mp->mnt_optnew, "untrusted", NULL, NULL) == 0)
187 mntorflags |= MNT_UNTRUSTED;
188
189 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
190 mntorflags |= MNT_ACLS;
191
192 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
193 mntorflags |= MNT_SNAPSHOT;
194 /*
195 * Once we have set the MNT_SNAPSHOT flag, do not
196 * persist "snapshot" in the options list.
197 */
198 vfs_deleteopt(mp->mnt_optnew, "snapshot");
199 vfs_deleteopt(mp->mnt_opt, "snapshot");
200 }
201
202 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
203 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
204 /*
205 * Once we have set the restricted PID, do not
206 * persist "fsckpid" in the options list.
207 */
208 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
209 vfs_deleteopt(mp->mnt_opt, "fsckpid");
210 if (mp->mnt_flag & MNT_UPDATE) {
211 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
212 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
213 vfs_mount_error(mp,
214 "Checker enable: Must be read-only");
215 return (EINVAL);
216 }
217 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
218 vfs_mount_error(mp,
219 "Checker enable: Must be read-only");
220 return (EINVAL);
221 }
222 /* Set to -1 if we are done */
223 if (fsckpid == 0)
224 fsckpid = -1;
225 }
226
227 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
228 if (mntorflags & MNT_ACLS) {
229 vfs_mount_error(mp,
230 "\"acls\" and \"nfsv4acls\" options "
231 "are mutually exclusive");
232 return (EINVAL);
233 }
234 mntorflags |= MNT_NFS4ACLS;
235 }
236
237 MNT_ILOCK(mp);
238 mp->mnt_flag |= mntorflags;
239 MNT_IUNLOCK(mp);
240 /*
241 * If updating, check whether changing from read-only to
242 * read/write; if there is no device name, that's all we do.
243 */
244 if (mp->mnt_flag & MNT_UPDATE) {
245 ump = VFSTOUFS(mp);
246 fs = ump->um_fs;
247 devvp = ump->um_devvp;
248 if (fsckpid == -1 && ump->um_fsckpid > 0) {
249 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
250 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
251 return (error);
252 g_topology_lock();
253 /*
254 * Return to normal read-only mode.
255 */
256 error = g_access(ump->um_cp, 0, -1, 0);
257 g_topology_unlock();
258 ump->um_fsckpid = 0;
259 }
260 if (fs->fs_ronly == 0 &&
261 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
262 /*
263 * Flush any dirty data and suspend filesystem.
264 */
265 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
266 return (error);
267 error = vfs_write_suspend_umnt(mp);
268 if (error != 0)
269 return (error);
270 /*
271 * Check for and optionally get rid of files open
272 * for writing.
273 */
274 flags = WRITECLOSE;
275 if (mp->mnt_flag & MNT_FORCE)
276 flags |= FORCECLOSE;
277 if (MOUNTEDSOFTDEP(mp)) {
278 error = softdep_flushfiles(mp, flags, td);
279 } else {
280 error = ffs_flushfiles(mp, flags, td);
281 }
282 if (error) {
283 vfs_write_resume(mp, 0);
284 return (error);
285 }
286 if (fs->fs_pendingblocks != 0 ||
287 fs->fs_pendinginodes != 0) {
288 printf("WARNING: %s Update error: blocks %jd "
289 "files %d\n", fs->fs_fsmnt,
290 (intmax_t)fs->fs_pendingblocks,
291 fs->fs_pendinginodes);
292 fs->fs_pendingblocks = 0;
293 fs->fs_pendinginodes = 0;
294 }
295 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
296 fs->fs_clean = 1;
297 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
298 fs->fs_ronly = 0;
299 fs->fs_clean = 0;
300 vfs_write_resume(mp, 0);
301 return (error);
302 }
303 if (MOUNTEDSOFTDEP(mp))
304 softdep_unmount(mp);
305 g_topology_lock();
306 /*
307 * Drop our write and exclusive access.
308 */
309 g_access(ump->um_cp, 0, -1, -1);
310 g_topology_unlock();
311 fs->fs_ronly = 1;
312 MNT_ILOCK(mp);
313 mp->mnt_flag |= MNT_RDONLY;
314 MNT_IUNLOCK(mp);
315 /*
316 * Allow the writers to note that filesystem
317 * is ro now.
318 */
319 vfs_write_resume(mp, 0);
320 }
321 if ((mp->mnt_flag & MNT_RELOAD) &&
322 (error = ffs_reload(mp, td, 0)) != 0)
323 return (error);
324 if (fs->fs_ronly &&
325 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
326 /*
327 * If we are running a checker, do not allow upgrade.
328 */
329 if (ump->um_fsckpid > 0) {
330 vfs_mount_error(mp,
331 "Active checker, cannot upgrade to write");
332 return (EINVAL);
333 }
334 /*
335 * If upgrade to read-write by non-root, then verify
336 * that user has necessary permissions on the device.
337 */
338 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
339 error = VOP_ACCESS(devvp, VREAD | VWRITE,
340 td->td_ucred, td);
341 if (error)
342 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
343 if (error) {
344 VOP_UNLOCK(devvp, 0);
345 return (error);
346 }
347 VOP_UNLOCK(devvp, 0);
348 fs->fs_flags &= ~FS_UNCLEAN;
349 if (fs->fs_clean == 0) {
350 fs->fs_flags |= FS_UNCLEAN;
351 if ((mp->mnt_flag & MNT_FORCE) ||
352 ((fs->fs_flags &
353 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
354 (fs->fs_flags & FS_DOSOFTDEP))) {
355 printf("WARNING: %s was not properly "
356 "dismounted\n", fs->fs_fsmnt);
357 } else {
358 vfs_mount_error(mp,
359 "R/W mount of %s denied. %s.%s",
360 fs->fs_fsmnt,
361 "Filesystem is not clean - run fsck",
362 (fs->fs_flags & FS_SUJ) == 0 ? "" :
363 " Forced mount will invalidate"
364 " journal contents");
365 return (EPERM);
366 }
367 }
368 g_topology_lock();
369 /*
370 * Request exclusive write access.
371 */
372 error = g_access(ump->um_cp, 0, 1, 1);
373 g_topology_unlock();
374 if (error)
375 return (error);
376 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
377 return (error);
378 error = vfs_write_suspend_umnt(mp);
379 if (error != 0)
380 return (error);
381 fs->fs_ronly = 0;
382 MNT_ILOCK(mp);
383 saved_mnt_flag = MNT_RDONLY;
384 if (MOUNTEDSOFTDEP(mp) && (mp->mnt_flag &
385 MNT_ASYNC) != 0)
386 saved_mnt_flag |= MNT_ASYNC;
387 mp->mnt_flag &= ~saved_mnt_flag;
388 MNT_IUNLOCK(mp);
389 fs->fs_mtime = time_second;
390 /* check to see if we need to start softdep */
391 if ((fs->fs_flags & FS_DOSOFTDEP) &&
392 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
393 fs->fs_ronly = 1;
394 MNT_ILOCK(mp);
395 mp->mnt_flag |= saved_mnt_flag;
396 MNT_IUNLOCK(mp);
397 vfs_write_resume(mp, 0);
398 return (error);
399 }
400 fs->fs_clean = 0;
401 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
402 fs->fs_ronly = 1;
403 MNT_ILOCK(mp);
404 mp->mnt_flag |= saved_mnt_flag;
405 MNT_IUNLOCK(mp);
406 vfs_write_resume(mp, 0);
407 return (error);
408 }
409 if (fs->fs_snapinum[0] != 0)
410 ffs_snapshot_mount(mp);
411 vfs_write_resume(mp, 0);
412 }
413 /*
414 * Soft updates is incompatible with "async",
415 * so if we are doing softupdates stop the user
416 * from setting the async flag in an update.
417 * Softdep_mount() clears it in an initial mount
418 * or ro->rw remount.
419 */
420 if (MOUNTEDSOFTDEP(mp)) {
421 /* XXX: Reset too late ? */
422 MNT_ILOCK(mp);
423 mp->mnt_flag &= ~MNT_ASYNC;
424 MNT_IUNLOCK(mp);
425 }
426 /*
427 * Keep MNT_ACLS flag if it is stored in superblock.
428 */
429 if ((fs->fs_flags & FS_ACLS) != 0) {
430 /* XXX: Set too late ? */
431 MNT_ILOCK(mp);
432 mp->mnt_flag |= MNT_ACLS;
433 MNT_IUNLOCK(mp);
434 }
435
436 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
437 /* XXX: Set too late ? */
438 MNT_ILOCK(mp);
439 mp->mnt_flag |= MNT_NFS4ACLS;
440 MNT_IUNLOCK(mp);
441 }
442 /*
443 * If this is a request from fsck to clean up the filesystem,
444 * then allow the specified pid to proceed.
445 */
446 if (fsckpid > 0) {
447 if (ump->um_fsckpid != 0) {
448 vfs_mount_error(mp,
449 "Active checker already running on %s",
450 fs->fs_fsmnt);
451 return (EINVAL);
452 }
453 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
454 ("soft updates enabled on read-only file system"));
455 g_topology_lock();
456 /*
457 * Request write access.
458 */
459 error = g_access(ump->um_cp, 0, 1, 0);
460 g_topology_unlock();
461 if (error) {
462 vfs_mount_error(mp,
463 "Checker activation failed on %s",
464 fs->fs_fsmnt);
465 return (error);
466 }
467 ump->um_fsckpid = fsckpid;
468 if (fs->fs_snapinum[0] != 0)
469 ffs_snapshot_mount(mp);
470 fs->fs_mtime = time_second;
471 fs->fs_fmod = 1;
472 fs->fs_clean = 0;
473 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
474 }
475
476 /*
477 * If this is a snapshot request, take the snapshot.
478 */
479 if (mp->mnt_flag & MNT_SNAPSHOT)
480 return (ffs_snapshot(mp, fspec));
481
482 /*
483 * Must not call namei() while owning busy ref.
484 */
485 vfs_unbusy(mp);
486 }
487
488 /*
489 * Not an update, or updating the name: look up the name
490 * and verify that it refers to a sensible disk device.
491 */
492 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
493 error = namei(&ndp);
494 if ((mp->mnt_flag & MNT_UPDATE) != 0) {
495 /*
496 * Unmount does not start if MNT_UPDATE is set. Mount
497 * update busies mp before setting MNT_UPDATE. We
498 * must be able to retain our busy ref succesfully,
499 * without sleep.
500 */
501 error1 = vfs_busy(mp, MBF_NOWAIT);
502 MPASS(error1 == 0);
503 }
504 if (error != 0)
505 return (error);
506 NDFREE(&ndp, NDF_ONLY_PNBUF);
507 devvp = ndp.ni_vp;
508 if (!vn_isdisk(devvp, &error)) {
509 vput(devvp);
510 return (error);
511 }
512
513 /*
514 * If mount by non-root, then verify that user has necessary
515 * permissions on the device.
516 */
517 accmode = VREAD;
518 if ((mp->mnt_flag & MNT_RDONLY) == 0)
519 accmode |= VWRITE;
520 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
521 if (error)
522 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
523 if (error) {
524 vput(devvp);
525 return (error);
526 }
527
528 if (mp->mnt_flag & MNT_UPDATE) {
529 /*
530 * Update only
531 *
532 * If it's not the same vnode, or at least the same device
533 * then it's not correct.
534 */
535
536 if (devvp->v_rdev != ump->um_devvp->v_rdev)
537 error = EINVAL; /* needs translation */
538 vput(devvp);
539 if (error)
540 return (error);
541 } else {
542 /*
543 * New mount
544 *
545 * We need the name for the mount point (also used for
546 * "last mounted on") copied in. If an error occurs,
547 * the mount point is discarded by the upper level code.
548 * Note that vfs_mount_alloc() populates f_mntonname for us.
549 */
550 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
551 vrele(devvp);
552 return (error);
553 }
554 if (fsckpid > 0) {
555 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
556 ("soft updates enabled on read-only file system"));
557 ump = VFSTOUFS(mp);
558 fs = ump->um_fs;
559 g_topology_lock();
560 /*
561 * Request write access.
562 */
563 error = g_access(ump->um_cp, 0, 1, 0);
564 g_topology_unlock();
565 if (error) {
566 printf("WARNING: %s: Checker activation "
567 "failed\n", fs->fs_fsmnt);
568 } else {
569 ump->um_fsckpid = fsckpid;
570 if (fs->fs_snapinum[0] != 0)
571 ffs_snapshot_mount(mp);
572 fs->fs_mtime = time_second;
573 fs->fs_clean = 0;
574 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
575 }
576 }
577 }
578 vfs_mountedfrom(mp, fspec);
579 return (0);
580 }
581
582 /*
583 * Compatibility with old mount system call.
584 */
585
586 static int
ffs_cmount(struct mntarg * ma,void * data,uint64_t flags)587 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
588 {
589 struct ufs_args args;
590 struct export_args exp;
591 int error;
592
593 if (data == NULL)
594 return (EINVAL);
595 error = copyin(data, &args, sizeof args);
596 if (error)
597 return (error);
598 vfs_oexport_conv(&args.export, &exp);
599
600 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
601 ma = mount_arg(ma, "export", &exp, sizeof(exp));
602 error = kernel_mount(ma, flags);
603
604 return (error);
605 }
606
607 /*
608 * Reload all incore data for a filesystem (used after running fsck on
609 * the root filesystem and finding things to fix). If the 'force' flag
610 * is 0, the filesystem must be mounted read-only.
611 *
612 * Things to do to update the mount:
613 * 1) invalidate all cached meta-data.
614 * 2) re-read superblock from disk.
615 * 3) re-read summary information from disk.
616 * 4) invalidate all inactive vnodes.
617 * 5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
618 * writers, if requested.
619 * 6) invalidate all cached file data.
620 * 7) re-read inode data for all active vnodes.
621 */
622 int
ffs_reload(struct mount * mp,struct thread * td,int flags)623 ffs_reload(struct mount *mp, struct thread *td, int flags)
624 {
625 struct vnode *vp, *mvp, *devvp;
626 struct inode *ip;
627 void *space;
628 struct buf *bp;
629 struct fs *fs, *newfs;
630 struct ufsmount *ump;
631 ufs2_daddr_t sblockloc;
632 int i, blks, error;
633 u_long size;
634 int32_t *lp;
635
636 ump = VFSTOUFS(mp);
637
638 MNT_ILOCK(mp);
639 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
640 MNT_IUNLOCK(mp);
641 return (EINVAL);
642 }
643 MNT_IUNLOCK(mp);
644
645 /*
646 * Step 1: invalidate all cached meta-data.
647 */
648 devvp = VFSTOUFS(mp)->um_devvp;
649 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
650 if (vinvalbuf(devvp, 0, 0, 0) != 0)
651 panic("ffs_reload: dirty1");
652 VOP_UNLOCK(devvp, 0);
653
654 /*
655 * Step 2: re-read superblock from disk.
656 */
657 fs = VFSTOUFS(mp)->um_fs;
658 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
659 NOCRED, &bp)) != 0)
660 return (error);
661 newfs = (struct fs *)bp->b_data;
662 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
663 newfs->fs_magic != FS_UFS2_MAGIC) ||
664 newfs->fs_bsize > MAXBSIZE ||
665 newfs->fs_bsize < sizeof(struct fs)) {
666 brelse(bp);
667 return (EIO); /* XXX needs translation */
668 }
669 /*
670 * Copy pointer fields back into superblock before copying in XXX
671 * new superblock. These should really be in the ufsmount. XXX
672 * Note that important parameters (eg fs_ncg) are unchanged.
673 */
674 newfs->fs_csp = fs->fs_csp;
675 newfs->fs_maxcluster = fs->fs_maxcluster;
676 newfs->fs_contigdirs = fs->fs_contigdirs;
677 newfs->fs_active = fs->fs_active;
678 newfs->fs_ronly = fs->fs_ronly;
679 sblockloc = fs->fs_sblockloc;
680 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
681 brelse(bp);
682 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
683 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
684 UFS_LOCK(ump);
685 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
686 printf("WARNING: %s: reload pending error: blocks %jd "
687 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
688 fs->fs_pendinginodes);
689 fs->fs_pendingblocks = 0;
690 fs->fs_pendinginodes = 0;
691 }
692 UFS_UNLOCK(ump);
693
694 /*
695 * Step 3: re-read summary information from disk.
696 */
697 size = fs->fs_cssize;
698 blks = howmany(size, fs->fs_fsize);
699 if (fs->fs_contigsumsize > 0)
700 size += fs->fs_ncg * sizeof(int32_t);
701 size += fs->fs_ncg * sizeof(u_int8_t);
702 free(fs->fs_csp, M_UFSMNT);
703 space = malloc(size, M_UFSMNT, M_WAITOK);
704 fs->fs_csp = space;
705 for (i = 0; i < blks; i += fs->fs_frag) {
706 size = fs->fs_bsize;
707 if (i + fs->fs_frag > blks)
708 size = (blks - i) * fs->fs_fsize;
709 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
710 NOCRED, &bp);
711 if (error)
712 return (error);
713 bcopy(bp->b_data, space, (u_int)size);
714 space = (char *)space + size;
715 brelse(bp);
716 }
717 /*
718 * We no longer know anything about clusters per cylinder group.
719 */
720 if (fs->fs_contigsumsize > 0) {
721 fs->fs_maxcluster = lp = space;
722 for (i = 0; i < fs->fs_ncg; i++)
723 *lp++ = fs->fs_contigsumsize;
724 space = lp;
725 }
726 size = fs->fs_ncg * sizeof(u_int8_t);
727 fs->fs_contigdirs = (u_int8_t *)space;
728 bzero(fs->fs_contigdirs, size);
729 if ((flags & FFSR_UNSUSPEND) != 0) {
730 MNT_ILOCK(mp);
731 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
732 wakeup(&mp->mnt_flag);
733 MNT_IUNLOCK(mp);
734 }
735
736 loop:
737 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
738 /*
739 * Skip syncer vnode.
740 */
741 if (vp->v_type == VNON) {
742 VI_UNLOCK(vp);
743 continue;
744 }
745 /*
746 * Step 4: invalidate all cached file data.
747 */
748 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
749 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
750 goto loop;
751 }
752 if (vinvalbuf(vp, 0, 0, 0))
753 panic("ffs_reload: dirty2");
754 /*
755 * Step 5: re-read inode data for all active vnodes.
756 */
757 ip = VTOI(vp);
758 error =
759 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
760 (int)fs->fs_bsize, NOCRED, &bp);
761 if (error) {
762 VOP_UNLOCK(vp, 0);
763 vrele(vp);
764 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
765 return (error);
766 }
767 ffs_load_inode(bp, ip, fs, ip->i_number);
768 ip->i_effnlink = ip->i_nlink;
769 brelse(bp);
770 VOP_UNLOCK(vp, 0);
771 vrele(vp);
772 }
773 return (0);
774 }
775
776 /*
777 * Common code for mount and mountroot
778 */
779 static int
ffs_mountfs(devvp,mp,td)780 ffs_mountfs(devvp, mp, td)
781 struct vnode *devvp;
782 struct mount *mp;
783 struct thread *td;
784 {
785 struct ufsmount *ump;
786 struct fs *fs;
787 struct cdev *dev;
788 int error, i, len, ronly;
789 struct ucred *cred;
790 struct g_consumer *cp;
791 struct mount *nmp;
792 int candelete;
793
794 fs = NULL;
795 ump = NULL;
796 cred = td ? td->td_ucred : NOCRED;
797 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
798
799 KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
800 dev = devvp->v_rdev;
801 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
802 (uintptr_t)mp) == 0) {
803 VOP_UNLOCK(devvp, 0);
804 return (EBUSY);
805 }
806 g_topology_lock();
807 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
808 g_topology_unlock();
809 if (error != 0) {
810 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
811 VOP_UNLOCK(devvp, 0);
812 return (error);
813 }
814 dev_ref(dev);
815 devvp->v_bufobj.bo_ops = &ffs_ops;
816 VOP_UNLOCK(devvp, 0);
817 if (dev->si_iosize_max != 0)
818 mp->mnt_iosize_max = dev->si_iosize_max;
819 if (mp->mnt_iosize_max > MAXPHYS)
820 mp->mnt_iosize_max = MAXPHYS;
821 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
822 error = EINVAL;
823 vfs_mount_error(mp,
824 "Invalid sectorsize %d for superblock size %d",
825 cp->provider->sectorsize, SBLOCKSIZE);
826 goto out;
827 }
828 /* fetch the superblock and summary information */
829 if ((error = ffs_sbget(devvp, &fs, -1, M_UFSMNT, ffs_use_bread)) != 0)
830 goto out;
831 fs->fs_fmod = 0;
832 fs->fs_flags &= ~FS_UNCLEAN;
833 if (fs->fs_clean == 0) {
834 fs->fs_flags |= FS_UNCLEAN;
835 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
836 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
837 (fs->fs_flags & FS_DOSOFTDEP))) {
838 printf("WARNING: %s was not properly dismounted\n",
839 fs->fs_fsmnt);
840 } else {
841 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
842 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
843 (fs->fs_flags & FS_SUJ) == 0 ? "" :
844 " Forced mount will invalidate journal contents");
845 error = EPERM;
846 goto out;
847 }
848 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
849 (mp->mnt_flag & MNT_FORCE)) {
850 printf("WARNING: %s: lost blocks %jd files %d\n",
851 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
852 fs->fs_pendinginodes);
853 fs->fs_pendingblocks = 0;
854 fs->fs_pendinginodes = 0;
855 }
856 }
857 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
858 printf("WARNING: %s: mount pending error: blocks %jd "
859 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
860 fs->fs_pendinginodes);
861 fs->fs_pendingblocks = 0;
862 fs->fs_pendinginodes = 0;
863 }
864 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
865 #ifdef UFS_GJOURNAL
866 /*
867 * Get journal provider name.
868 */
869 len = 1024;
870 mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
871 if (g_io_getattr("GJOURNAL::provider", cp, &len,
872 mp->mnt_gjprovider) == 0) {
873 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
874 M_UFSMNT, M_WAITOK);
875 MNT_ILOCK(mp);
876 mp->mnt_flag |= MNT_GJOURNAL;
877 MNT_IUNLOCK(mp);
878 } else {
879 printf("WARNING: %s: GJOURNAL flag on fs "
880 "but no gjournal provider below\n",
881 mp->mnt_stat.f_mntonname);
882 free(mp->mnt_gjprovider, M_UFSMNT);
883 mp->mnt_gjprovider = NULL;
884 }
885 #else
886 printf("WARNING: %s: GJOURNAL flag on fs but no "
887 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
888 #endif
889 } else {
890 mp->mnt_gjprovider = NULL;
891 }
892 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
893 ump->um_cp = cp;
894 ump->um_bo = &devvp->v_bufobj;
895 ump->um_fs = fs;
896 if (fs->fs_magic == FS_UFS1_MAGIC) {
897 ump->um_fstype = UFS1;
898 ump->um_balloc = ffs_balloc_ufs1;
899 } else {
900 ump->um_fstype = UFS2;
901 ump->um_balloc = ffs_balloc_ufs2;
902 }
903 ump->um_blkatoff = ffs_blkatoff;
904 ump->um_truncate = ffs_truncate;
905 ump->um_update = ffs_update;
906 ump->um_valloc = ffs_valloc;
907 ump->um_vfree = ffs_vfree;
908 ump->um_ifree = ffs_ifree;
909 ump->um_rdonly = ffs_rdonly;
910 ump->um_snapgone = ffs_snapgone;
911 if ((mp->mnt_flag & MNT_UNTRUSTED) != 0)
912 ump->um_check_blkno = ffs_check_blkno;
913 else
914 ump->um_check_blkno = NULL;
915 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
916 ffs_oldfscompat_read(fs, ump, fs->fs_sblockloc);
917 fs->fs_ronly = ronly;
918 fs->fs_active = NULL;
919 mp->mnt_data = ump;
920 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
921 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
922 nmp = NULL;
923 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
924 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
925 if (nmp)
926 vfs_rel(nmp);
927 vfs_getnewfsid(mp);
928 }
929 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
930 MNT_ILOCK(mp);
931 mp->mnt_flag |= MNT_LOCAL;
932 MNT_IUNLOCK(mp);
933 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
934 #ifdef MAC
935 MNT_ILOCK(mp);
936 mp->mnt_flag |= MNT_MULTILABEL;
937 MNT_IUNLOCK(mp);
938 #else
939 printf("WARNING: %s: multilabel flag on fs but "
940 "no MAC support\n", mp->mnt_stat.f_mntonname);
941 #endif
942 }
943 if ((fs->fs_flags & FS_ACLS) != 0) {
944 #ifdef UFS_ACL
945 MNT_ILOCK(mp);
946
947 if (mp->mnt_flag & MNT_NFS4ACLS)
948 printf("WARNING: %s: ACLs flag on fs conflicts with "
949 "\"nfsv4acls\" mount option; option ignored\n",
950 mp->mnt_stat.f_mntonname);
951 mp->mnt_flag &= ~MNT_NFS4ACLS;
952 mp->mnt_flag |= MNT_ACLS;
953
954 MNT_IUNLOCK(mp);
955 #else
956 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
957 mp->mnt_stat.f_mntonname);
958 #endif
959 }
960 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
961 #ifdef UFS_ACL
962 MNT_ILOCK(mp);
963
964 if (mp->mnt_flag & MNT_ACLS)
965 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
966 "with \"acls\" mount option; option ignored\n",
967 mp->mnt_stat.f_mntonname);
968 mp->mnt_flag &= ~MNT_ACLS;
969 mp->mnt_flag |= MNT_NFS4ACLS;
970
971 MNT_IUNLOCK(mp);
972 #else
973 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
974 "ACLs support\n", mp->mnt_stat.f_mntonname);
975 #endif
976 }
977 if ((fs->fs_flags & FS_TRIM) != 0) {
978 len = sizeof(int);
979 if (g_io_getattr("GEOM::candelete", cp, &len,
980 &candelete) == 0) {
981 if (candelete)
982 ump->um_flags |= UM_CANDELETE;
983 else
984 printf("WARNING: %s: TRIM flag on fs but disk "
985 "does not support TRIM\n",
986 mp->mnt_stat.f_mntonname);
987 } else {
988 printf("WARNING: %s: TRIM flag on fs but disk does "
989 "not confirm that it supports TRIM\n",
990 mp->mnt_stat.f_mntonname);
991 }
992 if (((ump->um_flags) & UM_CANDELETE) != 0) {
993 ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
994 taskqueue_thread_enqueue, &ump->um_trim_tq);
995 taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
996 "%s trim", mp->mnt_stat.f_mntonname);
997 ump->um_trimhash = hashinit(MAXTRIMIO, M_TRIM,
998 &ump->um_trimlisthashsize);
999 }
1000 }
1001
1002 ump->um_mountp = mp;
1003 ump->um_dev = dev;
1004 ump->um_devvp = devvp;
1005 ump->um_nindir = fs->fs_nindir;
1006 ump->um_bptrtodb = fs->fs_fsbtodb;
1007 ump->um_seqinc = fs->fs_frag;
1008 for (i = 0; i < MAXQUOTAS; i++)
1009 ump->um_quotas[i] = NULLVP;
1010 #ifdef UFS_EXTATTR
1011 ufs_extattr_uepm_init(&ump->um_extattr);
1012 #endif
1013 /*
1014 * Set FS local "last mounted on" information (NULL pad)
1015 */
1016 bzero(fs->fs_fsmnt, MAXMNTLEN);
1017 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1018 mp->mnt_stat.f_iosize = fs->fs_bsize;
1019
1020 if (mp->mnt_flag & MNT_ROOTFS) {
1021 /*
1022 * Root mount; update timestamp in mount structure.
1023 * this will be used by the common root mount code
1024 * to update the system clock.
1025 */
1026 mp->mnt_time = fs->fs_time;
1027 }
1028
1029 if (ronly == 0) {
1030 fs->fs_mtime = time_second;
1031 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1032 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1033 ffs_flushfiles(mp, FORCECLOSE, td);
1034 goto out;
1035 }
1036 if (fs->fs_snapinum[0] != 0)
1037 ffs_snapshot_mount(mp);
1038 fs->fs_fmod = 1;
1039 fs->fs_clean = 0;
1040 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1041 }
1042 /*
1043 * Initialize filesystem state information in mount struct.
1044 */
1045 MNT_ILOCK(mp);
1046 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1047 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1048 MNT_IUNLOCK(mp);
1049 #ifdef UFS_EXTATTR
1050 #ifdef UFS_EXTATTR_AUTOSTART
1051 /*
1052 *
1053 * Auto-starting does the following:
1054 * - check for /.attribute in the fs, and extattr_start if so
1055 * - for each file in .attribute, enable that file with
1056 * an attribute of the same name.
1057 * Not clear how to report errors -- probably eat them.
1058 * This would all happen while the filesystem was busy/not
1059 * available, so would effectively be "atomic".
1060 */
1061 (void) ufs_extattr_autostart(mp, td);
1062 #endif /* !UFS_EXTATTR_AUTOSTART */
1063 #endif /* !UFS_EXTATTR */
1064 return (0);
1065 out:
1066 if (fs != NULL) {
1067 free(fs->fs_csp, M_UFSMNT);
1068 free(fs, M_UFSMNT);
1069 }
1070 if (cp != NULL) {
1071 g_topology_lock();
1072 g_vfs_close(cp);
1073 g_topology_unlock();
1074 }
1075 if (ump) {
1076 mtx_destroy(UFS_MTX(ump));
1077 if (mp->mnt_gjprovider != NULL) {
1078 free(mp->mnt_gjprovider, M_UFSMNT);
1079 mp->mnt_gjprovider = NULL;
1080 }
1081 free(ump, M_UFSMNT);
1082 mp->mnt_data = NULL;
1083 }
1084 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1085 dev_rel(dev);
1086 return (error);
1087 }
1088
1089 /*
1090 * A read function for use by filesystem-layer routines.
1091 */
1092 static int
ffs_use_bread(void * devfd,off_t loc,void ** bufp,int size)1093 ffs_use_bread(void *devfd, off_t loc, void **bufp, int size)
1094 {
1095 struct buf *bp;
1096 int error;
1097
1098 KASSERT(*bufp == NULL, ("ffs_use_bread: non-NULL *bufp %p\n", *bufp));
1099 *bufp = malloc(size, M_UFSMNT, M_WAITOK);
1100 if ((error = bread((struct vnode *)devfd, btodb(loc), size, NOCRED,
1101 &bp)) != 0)
1102 return (error);
1103 bcopy(bp->b_data, *bufp, size);
1104 bp->b_flags |= B_INVAL | B_NOCACHE;
1105 brelse(bp);
1106 return (0);
1107 }
1108
1109 #include <sys/sysctl.h>
1110 static int bigcgs = 0;
1111 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1112
1113 /*
1114 * Sanity checks for loading old filesystem superblocks.
1115 * See ffs_oldfscompat_write below for unwound actions.
1116 *
1117 * XXX - Parts get retired eventually.
1118 * Unfortunately new bits get added.
1119 */
1120 static void
ffs_oldfscompat_read(fs,ump,sblockloc)1121 ffs_oldfscompat_read(fs, ump, sblockloc)
1122 struct fs *fs;
1123 struct ufsmount *ump;
1124 ufs2_daddr_t sblockloc;
1125 {
1126 off_t maxfilesize;
1127
1128 /*
1129 * If not yet done, update fs_flags location and value of fs_sblockloc.
1130 */
1131 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1132 fs->fs_flags = fs->fs_old_flags;
1133 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1134 fs->fs_sblockloc = sblockloc;
1135 }
1136 /*
1137 * If not yet done, update UFS1 superblock with new wider fields.
1138 */
1139 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1140 fs->fs_maxbsize = fs->fs_bsize;
1141 fs->fs_time = fs->fs_old_time;
1142 fs->fs_size = fs->fs_old_size;
1143 fs->fs_dsize = fs->fs_old_dsize;
1144 fs->fs_csaddr = fs->fs_old_csaddr;
1145 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1146 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1147 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1148 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1149 }
1150 if (fs->fs_magic == FS_UFS1_MAGIC &&
1151 fs->fs_old_inodefmt < FS_44INODEFMT) {
1152 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1153 fs->fs_qbmask = ~fs->fs_bmask;
1154 fs->fs_qfmask = ~fs->fs_fmask;
1155 }
1156 if (fs->fs_magic == FS_UFS1_MAGIC) {
1157 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1158 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1159 if (fs->fs_maxfilesize > maxfilesize)
1160 fs->fs_maxfilesize = maxfilesize;
1161 }
1162 /* Compatibility for old filesystems */
1163 if (fs->fs_avgfilesize <= 0)
1164 fs->fs_avgfilesize = AVFILESIZ;
1165 if (fs->fs_avgfpdir <= 0)
1166 fs->fs_avgfpdir = AFPDIR;
1167 if (bigcgs) {
1168 fs->fs_save_cgsize = fs->fs_cgsize;
1169 fs->fs_cgsize = fs->fs_bsize;
1170 }
1171 }
1172
1173 /*
1174 * Unwinding superblock updates for old filesystems.
1175 * See ffs_oldfscompat_read above for details.
1176 *
1177 * XXX - Parts get retired eventually.
1178 * Unfortunately new bits get added.
1179 */
1180 void
ffs_oldfscompat_write(fs,ump)1181 ffs_oldfscompat_write(fs, ump)
1182 struct fs *fs;
1183 struct ufsmount *ump;
1184 {
1185
1186 /*
1187 * Copy back UFS2 updated fields that UFS1 inspects.
1188 */
1189 if (fs->fs_magic == FS_UFS1_MAGIC) {
1190 fs->fs_old_time = fs->fs_time;
1191 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1192 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1193 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1194 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1195 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1196 }
1197 if (bigcgs) {
1198 fs->fs_cgsize = fs->fs_save_cgsize;
1199 fs->fs_save_cgsize = 0;
1200 }
1201 }
1202
1203 /*
1204 * unmount system call
1205 */
1206 static int
ffs_unmount(mp,mntflags)1207 ffs_unmount(mp, mntflags)
1208 struct mount *mp;
1209 int mntflags;
1210 {
1211 struct thread *td;
1212 struct ufsmount *ump = VFSTOUFS(mp);
1213 struct fs *fs;
1214 int error, flags, susp;
1215 #ifdef UFS_EXTATTR
1216 int e_restart;
1217 #endif
1218
1219 flags = 0;
1220 td = curthread;
1221 fs = ump->um_fs;
1222 if (mntflags & MNT_FORCE)
1223 flags |= FORCECLOSE;
1224 susp = fs->fs_ronly == 0;
1225 #ifdef UFS_EXTATTR
1226 if ((error = ufs_extattr_stop(mp, td))) {
1227 if (error != EOPNOTSUPP)
1228 printf("WARNING: unmount %s: ufs_extattr_stop "
1229 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1230 error);
1231 e_restart = 0;
1232 } else {
1233 ufs_extattr_uepm_destroy(&ump->um_extattr);
1234 e_restart = 1;
1235 }
1236 #endif
1237 if (susp) {
1238 error = vfs_write_suspend_umnt(mp);
1239 if (error != 0)
1240 goto fail1;
1241 }
1242 if (MOUNTEDSOFTDEP(mp))
1243 error = softdep_flushfiles(mp, flags, td);
1244 else
1245 error = ffs_flushfiles(mp, flags, td);
1246 if (error != 0 && error != ENXIO)
1247 goto fail;
1248
1249 UFS_LOCK(ump);
1250 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1251 printf("WARNING: unmount %s: pending error: blocks %jd "
1252 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1253 fs->fs_pendinginodes);
1254 fs->fs_pendingblocks = 0;
1255 fs->fs_pendinginodes = 0;
1256 }
1257 UFS_UNLOCK(ump);
1258 if (MOUNTEDSOFTDEP(mp))
1259 softdep_unmount(mp);
1260 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1261 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1262 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1263 if (error && error != ENXIO) {
1264 fs->fs_clean = 0;
1265 goto fail;
1266 }
1267 }
1268 if (susp)
1269 vfs_write_resume(mp, VR_START_WRITE);
1270 if (ump->um_trim_tq != NULL) {
1271 while (ump->um_trim_inflight != 0)
1272 pause("ufsutr", hz);
1273 taskqueue_drain_all(ump->um_trim_tq);
1274 taskqueue_free(ump->um_trim_tq);
1275 free (ump->um_trimhash, M_TRIM);
1276 }
1277 g_topology_lock();
1278 if (ump->um_fsckpid > 0) {
1279 /*
1280 * Return to normal read-only mode.
1281 */
1282 error = g_access(ump->um_cp, 0, -1, 0);
1283 ump->um_fsckpid = 0;
1284 }
1285 g_vfs_close(ump->um_cp);
1286 g_topology_unlock();
1287 atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1288 vrele(ump->um_devvp);
1289 dev_rel(ump->um_dev);
1290 mtx_destroy(UFS_MTX(ump));
1291 if (mp->mnt_gjprovider != NULL) {
1292 free(mp->mnt_gjprovider, M_UFSMNT);
1293 mp->mnt_gjprovider = NULL;
1294 }
1295 free(fs->fs_csp, M_UFSMNT);
1296 free(fs, M_UFSMNT);
1297 free(ump, M_UFSMNT);
1298 mp->mnt_data = NULL;
1299 MNT_ILOCK(mp);
1300 mp->mnt_flag &= ~MNT_LOCAL;
1301 MNT_IUNLOCK(mp);
1302 if (td->td_su == mp) {
1303 td->td_su = NULL;
1304 vfs_rel(mp);
1305 }
1306 return (error);
1307
1308 fail:
1309 if (susp)
1310 vfs_write_resume(mp, VR_START_WRITE);
1311 fail1:
1312 #ifdef UFS_EXTATTR
1313 if (e_restart) {
1314 ufs_extattr_uepm_init(&ump->um_extattr);
1315 #ifdef UFS_EXTATTR_AUTOSTART
1316 (void) ufs_extattr_autostart(mp, td);
1317 #endif
1318 }
1319 #endif
1320
1321 return (error);
1322 }
1323
1324 /*
1325 * Flush out all the files in a filesystem.
1326 */
1327 int
ffs_flushfiles(mp,flags,td)1328 ffs_flushfiles(mp, flags, td)
1329 struct mount *mp;
1330 int flags;
1331 struct thread *td;
1332 {
1333 struct ufsmount *ump;
1334 int qerror, error;
1335
1336 ump = VFSTOUFS(mp);
1337 qerror = 0;
1338 #ifdef QUOTA
1339 if (mp->mnt_flag & MNT_QUOTA) {
1340 int i;
1341 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1342 if (error)
1343 return (error);
1344 for (i = 0; i < MAXQUOTAS; i++) {
1345 error = quotaoff(td, mp, i);
1346 if (error != 0) {
1347 if ((flags & EARLYFLUSH) == 0)
1348 return (error);
1349 else
1350 qerror = error;
1351 }
1352 }
1353
1354 /*
1355 * Here we fall through to vflush again to ensure that
1356 * we have gotten rid of all the system vnodes, unless
1357 * quotas must not be closed.
1358 */
1359 }
1360 #endif
1361 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1362 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1363 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1364 return (error);
1365 ffs_snapshot_unmount(mp);
1366 flags |= FORCECLOSE;
1367 /*
1368 * Here we fall through to vflush again to ensure
1369 * that we have gotten rid of all the system vnodes.
1370 */
1371 }
1372
1373 /*
1374 * Do not close system files if quotas were not closed, to be
1375 * able to sync the remaining dquots. The freeblks softupdate
1376 * workitems might hold a reference on a dquot, preventing
1377 * quotaoff() from completing. Next round of
1378 * softdep_flushworklist() iteration should process the
1379 * blockers, allowing the next run of quotaoff() to finally
1380 * flush held dquots.
1381 *
1382 * Otherwise, flush all the files.
1383 */
1384 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1385 return (error);
1386
1387 /*
1388 * Flush filesystem metadata.
1389 */
1390 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1391 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1392 VOP_UNLOCK(ump->um_devvp, 0);
1393 return (error);
1394 }
1395
1396 /*
1397 * Get filesystem statistics.
1398 */
1399 static int
ffs_statfs(mp,sbp)1400 ffs_statfs(mp, sbp)
1401 struct mount *mp;
1402 struct statfs *sbp;
1403 {
1404 struct ufsmount *ump;
1405 struct fs *fs;
1406
1407 ump = VFSTOUFS(mp);
1408 fs = ump->um_fs;
1409 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1410 panic("ffs_statfs");
1411 sbp->f_version = STATFS_VERSION;
1412 sbp->f_bsize = fs->fs_fsize;
1413 sbp->f_iosize = fs->fs_bsize;
1414 sbp->f_blocks = fs->fs_dsize;
1415 UFS_LOCK(ump);
1416 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1417 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1418 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1419 dbtofsb(fs, fs->fs_pendingblocks);
1420 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1421 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1422 UFS_UNLOCK(ump);
1423 sbp->f_namemax = UFS_MAXNAMLEN;
1424 return (0);
1425 }
1426
1427 static bool
sync_doupdate(struct inode * ip)1428 sync_doupdate(struct inode *ip)
1429 {
1430
1431 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1432 IN_UPDATE)) != 0);
1433 }
1434
1435 /*
1436 * For a lazy sync, we only care about access times, quotas and the
1437 * superblock. Other filesystem changes are already converted to
1438 * cylinder group blocks or inode blocks updates and are written to
1439 * disk by syncer.
1440 */
1441 static int
ffs_sync_lazy(mp)1442 ffs_sync_lazy(mp)
1443 struct mount *mp;
1444 {
1445 struct vnode *mvp, *vp;
1446 struct inode *ip;
1447 struct thread *td;
1448 int allerror, error;
1449
1450 allerror = 0;
1451 td = curthread;
1452 if ((mp->mnt_flag & MNT_NOATIME) != 0) {
1453 #ifdef QUOTA
1454 qsync(mp);
1455 #endif
1456 goto sbupdate;
1457 }
1458 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1459 if (vp->v_type == VNON) {
1460 VI_UNLOCK(vp);
1461 continue;
1462 }
1463 ip = VTOI(vp);
1464
1465 /*
1466 * The IN_ACCESS flag is converted to IN_MODIFIED by
1467 * ufs_close() and ufs_getattr() by the calls to
1468 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1469 * Test also all the other timestamp flags too, to pick up
1470 * any other cases that could be missed.
1471 */
1472 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1473 VI_UNLOCK(vp);
1474 continue;
1475 }
1476 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1477 td)) != 0)
1478 continue;
1479 #ifdef QUOTA
1480 qsyncvp(vp);
1481 #endif
1482 if (sync_doupdate(ip))
1483 error = ffs_update(vp, 0);
1484 if (error != 0)
1485 allerror = error;
1486 vput(vp);
1487 }
1488 sbupdate:
1489 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1490 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1491 allerror = error;
1492 return (allerror);
1493 }
1494
1495 /*
1496 * Go through the disk queues to initiate sandbagged IO;
1497 * go through the inodes to write those that have been modified;
1498 * initiate the writing of the super block if it has been modified.
1499 *
1500 * Note: we are always called with the filesystem marked busy using
1501 * vfs_busy().
1502 */
1503 static int
ffs_sync(mp,waitfor)1504 ffs_sync(mp, waitfor)
1505 struct mount *mp;
1506 int waitfor;
1507 {
1508 struct vnode *mvp, *vp, *devvp;
1509 struct thread *td;
1510 struct inode *ip;
1511 struct ufsmount *ump = VFSTOUFS(mp);
1512 struct fs *fs;
1513 int error, count, lockreq, allerror = 0;
1514 int suspend;
1515 int suspended;
1516 int secondary_writes;
1517 int secondary_accwrites;
1518 int softdep_deps;
1519 int softdep_accdeps;
1520 struct bufobj *bo;
1521
1522 suspend = 0;
1523 suspended = 0;
1524 td = curthread;
1525 fs = ump->um_fs;
1526 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1527 panic("%s: ffs_sync: modification on read-only filesystem",
1528 fs->fs_fsmnt);
1529 if (waitfor == MNT_LAZY) {
1530 if (!rebooting)
1531 return (ffs_sync_lazy(mp));
1532 waitfor = MNT_NOWAIT;
1533 }
1534
1535 /*
1536 * Write back each (modified) inode.
1537 */
1538 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1539 if (waitfor == MNT_SUSPEND) {
1540 suspend = 1;
1541 waitfor = MNT_WAIT;
1542 }
1543 if (waitfor == MNT_WAIT)
1544 lockreq = LK_EXCLUSIVE;
1545 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1546 loop:
1547 /* Grab snapshot of secondary write counts */
1548 MNT_ILOCK(mp);
1549 secondary_writes = mp->mnt_secondary_writes;
1550 secondary_accwrites = mp->mnt_secondary_accwrites;
1551 MNT_IUNLOCK(mp);
1552
1553 /* Grab snapshot of softdep dependency counts */
1554 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1555
1556 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1557 /*
1558 * Depend on the vnode interlock to keep things stable enough
1559 * for a quick test. Since there might be hundreds of
1560 * thousands of vnodes, we cannot afford even a subroutine
1561 * call unless there's a good chance that we have work to do.
1562 */
1563 if (vp->v_type == VNON) {
1564 VI_UNLOCK(vp);
1565 continue;
1566 }
1567 ip = VTOI(vp);
1568 if ((ip->i_flag &
1569 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1570 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1571 VI_UNLOCK(vp);
1572 continue;
1573 }
1574 if ((error = vget(vp, lockreq, td)) != 0) {
1575 if (error == ENOENT || error == ENOLCK) {
1576 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1577 goto loop;
1578 }
1579 continue;
1580 }
1581 #ifdef QUOTA
1582 qsyncvp(vp);
1583 #endif
1584 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1585 allerror = error;
1586 vput(vp);
1587 }
1588 /*
1589 * Force stale filesystem control information to be flushed.
1590 */
1591 if (waitfor == MNT_WAIT || rebooting) {
1592 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1593 allerror = error;
1594 /* Flushed work items may create new vnodes to clean */
1595 if (allerror == 0 && count)
1596 goto loop;
1597 }
1598
1599 devvp = ump->um_devvp;
1600 bo = &devvp->v_bufobj;
1601 BO_LOCK(bo);
1602 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1603 BO_UNLOCK(bo);
1604 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1605 error = VOP_FSYNC(devvp, waitfor, td);
1606 VOP_UNLOCK(devvp, 0);
1607 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1608 error = ffs_sbupdate(ump, waitfor, 0);
1609 if (error != 0)
1610 allerror = error;
1611 if (allerror == 0 && waitfor == MNT_WAIT)
1612 goto loop;
1613 } else if (suspend != 0) {
1614 if (softdep_check_suspend(mp,
1615 devvp,
1616 softdep_deps,
1617 softdep_accdeps,
1618 secondary_writes,
1619 secondary_accwrites) != 0) {
1620 MNT_IUNLOCK(mp);
1621 goto loop; /* More work needed */
1622 }
1623 mtx_assert(MNT_MTX(mp), MA_OWNED);
1624 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1625 MNT_IUNLOCK(mp);
1626 suspended = 1;
1627 } else
1628 BO_UNLOCK(bo);
1629 /*
1630 * Write back modified superblock.
1631 */
1632 if (fs->fs_fmod != 0 &&
1633 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1634 allerror = error;
1635 return (allerror);
1636 }
1637
1638 int
ffs_vget(mp,ino,flags,vpp)1639 ffs_vget(mp, ino, flags, vpp)
1640 struct mount *mp;
1641 ino_t ino;
1642 int flags;
1643 struct vnode **vpp;
1644 {
1645 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1646 }
1647
1648 int
ffs_vgetf(mp,ino,flags,vpp,ffs_flags)1649 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1650 struct mount *mp;
1651 ino_t ino;
1652 int flags;
1653 struct vnode **vpp;
1654 int ffs_flags;
1655 {
1656 struct fs *fs;
1657 struct inode *ip;
1658 struct ufsmount *ump;
1659 struct buf *bp;
1660 struct vnode *vp;
1661 int error;
1662
1663 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1664 if (error || *vpp != NULL)
1665 return (error);
1666
1667 /*
1668 * We must promote to an exclusive lock for vnode creation. This
1669 * can happen if lookup is passed LOCKSHARED.
1670 */
1671 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1672 flags &= ~LK_TYPE_MASK;
1673 flags |= LK_EXCLUSIVE;
1674 }
1675
1676 /*
1677 * We do not lock vnode creation as it is believed to be too
1678 * expensive for such rare case as simultaneous creation of vnode
1679 * for same ino by different processes. We just allow them to race
1680 * and check later to decide who wins. Let the race begin!
1681 */
1682
1683 ump = VFSTOUFS(mp);
1684 fs = ump->um_fs;
1685 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1686
1687 /* Allocate a new vnode/inode. */
1688 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1689 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1690 if (error) {
1691 *vpp = NULL;
1692 uma_zfree(uma_inode, ip);
1693 return (error);
1694 }
1695 /*
1696 * FFS supports recursive locking.
1697 */
1698 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1699 VN_LOCK_AREC(vp);
1700 vp->v_data = ip;
1701 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1702 ip->i_vnode = vp;
1703 ip->i_ump = ump;
1704 ip->i_number = ino;
1705 ip->i_ea_refs = 0;
1706 ip->i_nextclustercg = -1;
1707 ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
1708 #ifdef QUOTA
1709 {
1710 int i;
1711 for (i = 0; i < MAXQUOTAS; i++)
1712 ip->i_dquot[i] = NODQUOT;
1713 }
1714 #endif
1715
1716 if (ffs_flags & FFSV_FORCEINSMQ)
1717 vp->v_vflag |= VV_FORCEINSMQ;
1718 error = insmntque(vp, mp);
1719 if (error != 0) {
1720 uma_zfree(uma_inode, ip);
1721 *vpp = NULL;
1722 return (error);
1723 }
1724 vp->v_vflag &= ~VV_FORCEINSMQ;
1725 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1726 if (error || *vpp != NULL)
1727 return (error);
1728
1729 /* Read in the disk contents for the inode, copy into the inode. */
1730 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1731 (int)fs->fs_bsize, NOCRED, &bp);
1732 if (error) {
1733 /*
1734 * The inode does not contain anything useful, so it would
1735 * be misleading to leave it on its hash chain. With mode
1736 * still zero, it will be unlinked and returned to the free
1737 * list by vput().
1738 */
1739 brelse(bp);
1740 vput(vp);
1741 *vpp = NULL;
1742 return (error);
1743 }
1744 if (I_IS_UFS1(ip))
1745 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1746 else
1747 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1748 ffs_load_inode(bp, ip, fs, ino);
1749 if (DOINGSOFTDEP(vp))
1750 softdep_load_inodeblock(ip);
1751 else
1752 ip->i_effnlink = ip->i_nlink;
1753 bqrelse(bp);
1754
1755 /*
1756 * Initialize the vnode from the inode, check for aliases.
1757 * Note that the underlying vnode may have changed.
1758 */
1759 error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
1760 &vp);
1761 if (error) {
1762 vput(vp);
1763 *vpp = NULL;
1764 return (error);
1765 }
1766
1767 /*
1768 * Finish inode initialization.
1769 */
1770 if (vp->v_type != VFIFO) {
1771 /* FFS supports shared locking for all files except fifos. */
1772 VN_LOCK_ASHARE(vp);
1773 }
1774
1775 /*
1776 * Set up a generation number for this inode if it does not
1777 * already have one. This should only happen on old filesystems.
1778 */
1779 if (ip->i_gen == 0) {
1780 while (ip->i_gen == 0)
1781 ip->i_gen = arc4random();
1782 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1783 ip->i_flag |= IN_MODIFIED;
1784 DIP_SET(ip, i_gen, ip->i_gen);
1785 }
1786 }
1787 #ifdef MAC
1788 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1789 /*
1790 * If this vnode is already allocated, and we're running
1791 * multi-label, attempt to perform a label association
1792 * from the extended attributes on the inode.
1793 */
1794 error = mac_vnode_associate_extattr(mp, vp);
1795 if (error) {
1796 /* ufs_inactive will release ip->i_devvp ref. */
1797 vput(vp);
1798 *vpp = NULL;
1799 return (error);
1800 }
1801 }
1802 #endif
1803
1804 *vpp = vp;
1805 return (0);
1806 }
1807
1808 /*
1809 * File handle to vnode
1810 *
1811 * Have to be really careful about stale file handles:
1812 * - check that the inode number is valid
1813 * - for UFS2 check that the inode number is initialized
1814 * - call ffs_vget() to get the locked inode
1815 * - check for an unallocated inode (i_mode == 0)
1816 * - check that the given client host has export rights and return
1817 * those rights via. exflagsp and credanonp
1818 */
1819 static int
ffs_fhtovp(mp,fhp,flags,vpp)1820 ffs_fhtovp(mp, fhp, flags, vpp)
1821 struct mount *mp;
1822 struct fid *fhp;
1823 int flags;
1824 struct vnode **vpp;
1825 {
1826 struct ufid *ufhp;
1827 struct ufsmount *ump;
1828 struct fs *fs;
1829 struct cg *cgp;
1830 struct buf *bp;
1831 ino_t ino;
1832 u_int cg;
1833 int error;
1834
1835 ufhp = (struct ufid *)fhp;
1836 ino = ufhp->ufid_ino;
1837 ump = VFSTOUFS(mp);
1838 fs = ump->um_fs;
1839 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
1840 return (ESTALE);
1841 /*
1842 * Need to check if inode is initialized because UFS2 does lazy
1843 * initialization and nfs_fhtovp can offer arbitrary inode numbers.
1844 */
1845 if (fs->fs_magic != FS_UFS2_MAGIC)
1846 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1847 cg = ino_to_cg(fs, ino);
1848 if ((error = ffs_getcg(fs, ump->um_devvp, cg, &bp, &cgp)) != 0)
1849 return (error);
1850 if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
1851 brelse(bp);
1852 return (ESTALE);
1853 }
1854 brelse(bp);
1855 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1856 }
1857
1858 /*
1859 * Initialize the filesystem.
1860 */
1861 static int
ffs_init(vfsp)1862 ffs_init(vfsp)
1863 struct vfsconf *vfsp;
1864 {
1865
1866 ffs_susp_initialize();
1867 softdep_initialize();
1868 return (ufs_init(vfsp));
1869 }
1870
1871 /*
1872 * Undo the work of ffs_init().
1873 */
1874 static int
ffs_uninit(vfsp)1875 ffs_uninit(vfsp)
1876 struct vfsconf *vfsp;
1877 {
1878 int ret;
1879
1880 ret = ufs_uninit(vfsp);
1881 softdep_uninitialize();
1882 ffs_susp_uninitialize();
1883 return (ret);
1884 }
1885
1886 /*
1887 * Structure used to pass information from ffs_sbupdate to its
1888 * helper routine ffs_use_bwrite.
1889 */
1890 struct devfd {
1891 struct ufsmount *ump;
1892 struct buf *sbbp;
1893 int waitfor;
1894 int suspended;
1895 int error;
1896 };
1897
1898 /*
1899 * Write a superblock and associated information back to disk.
1900 */
1901 int
ffs_sbupdate(ump,waitfor,suspended)1902 ffs_sbupdate(ump, waitfor, suspended)
1903 struct ufsmount *ump;
1904 int waitfor;
1905 int suspended;
1906 {
1907 struct fs *fs;
1908 struct buf *sbbp;
1909 struct devfd devfd;
1910
1911 fs = ump->um_fs;
1912 if (fs->fs_ronly == 1 &&
1913 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1914 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1915 panic("ffs_sbupdate: write read-only filesystem");
1916 /*
1917 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1918 */
1919 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1920 (int)fs->fs_sbsize, 0, 0, 0);
1921 /*
1922 * Initialize info needed for write function.
1923 */
1924 devfd.ump = ump;
1925 devfd.sbbp = sbbp;
1926 devfd.waitfor = waitfor;
1927 devfd.suspended = suspended;
1928 devfd.error = 0;
1929 return (ffs_sbput(&devfd, fs, fs->fs_sblockloc, ffs_use_bwrite));
1930 }
1931
1932 /*
1933 * Write function for use by filesystem-layer routines.
1934 */
1935 static int
ffs_use_bwrite(void * devfd,off_t loc,void * buf,int size)1936 ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size)
1937 {
1938 struct devfd *devfdp;
1939 struct ufsmount *ump;
1940 struct buf *bp;
1941 struct fs *fs;
1942 int error;
1943
1944 devfdp = devfd;
1945 ump = devfdp->ump;
1946 fs = ump->um_fs;
1947 /*
1948 * Writing the superblock summary information.
1949 */
1950 if (loc != fs->fs_sblockloc) {
1951 bp = getblk(ump->um_devvp, btodb(loc), size, 0, 0, 0);
1952 bcopy(buf, bp->b_data, (u_int)size);
1953 if (devfdp->suspended)
1954 bp->b_flags |= B_VALIDSUSPWRT;
1955 if (devfdp->waitfor != MNT_WAIT)
1956 bawrite(bp);
1957 else if ((error = bwrite(bp)) != 0)
1958 devfdp->error = error;
1959 return (0);
1960 }
1961 /*
1962 * Writing the superblock itself. We need to do special checks for it.
1963 */
1964 bp = devfdp->sbbp;
1965 if (devfdp->error != 0) {
1966 brelse(bp);
1967 return (devfdp->error);
1968 }
1969 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1970 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1971 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1972 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1973 fs->fs_sblockloc = SBLOCK_UFS1;
1974 }
1975 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1976 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1977 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1978 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1979 fs->fs_sblockloc = SBLOCK_UFS2;
1980 }
1981 if (MOUNTEDSOFTDEP(ump->um_mountp))
1982 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
1983 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1984 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
1985 if (devfdp->suspended)
1986 bp->b_flags |= B_VALIDSUSPWRT;
1987 if (devfdp->waitfor != MNT_WAIT)
1988 bawrite(bp);
1989 else if ((error = bwrite(bp)) != 0)
1990 devfdp->error = error;
1991 return (devfdp->error);
1992 }
1993
1994 static int
ffs_extattrctl(struct mount * mp,int cmd,struct vnode * filename_vp,int attrnamespace,const char * attrname)1995 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1996 int attrnamespace, const char *attrname)
1997 {
1998
1999 #ifdef UFS_EXTATTR
2000 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
2001 attrname));
2002 #else
2003 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
2004 attrname));
2005 #endif
2006 }
2007
2008 static void
ffs_ifree(struct ufsmount * ump,struct inode * ip)2009 ffs_ifree(struct ufsmount *ump, struct inode *ip)
2010 {
2011
2012 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
2013 uma_zfree(uma_ufs1, ip->i_din1);
2014 else if (ip->i_din2 != NULL)
2015 uma_zfree(uma_ufs2, ip->i_din2);
2016 uma_zfree(uma_inode, ip);
2017 }
2018
2019 static int dobkgrdwrite = 1;
2020 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2021 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2022
2023 /*
2024 * Complete a background write started from bwrite.
2025 */
2026 static void
ffs_backgroundwritedone(struct buf * bp)2027 ffs_backgroundwritedone(struct buf *bp)
2028 {
2029 struct bufobj *bufobj;
2030 struct buf *origbp;
2031
2032 /*
2033 * Find the original buffer that we are writing.
2034 */
2035 bufobj = bp->b_bufobj;
2036 BO_LOCK(bufobj);
2037 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2038 panic("backgroundwritedone: lost buffer");
2039
2040 /*
2041 * We should mark the cylinder group buffer origbp as
2042 * dirty, to not loose the failed write.
2043 */
2044 if ((bp->b_ioflags & BIO_ERROR) != 0)
2045 origbp->b_vflags |= BV_BKGRDERR;
2046 BO_UNLOCK(bufobj);
2047 /*
2048 * Process dependencies then return any unfinished ones.
2049 */
2050 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2051 buf_complete(bp);
2052 #ifdef SOFTUPDATES
2053 if (!LIST_EMPTY(&bp->b_dep))
2054 softdep_move_dependencies(bp, origbp);
2055 #endif
2056 /*
2057 * This buffer is marked B_NOCACHE so when it is released
2058 * by biodone it will be tossed.
2059 */
2060 bp->b_flags |= B_NOCACHE;
2061 bp->b_flags &= ~B_CACHE;
2062 pbrelvp(bp);
2063
2064 /*
2065 * Prevent brelse() from trying to keep and re-dirtying bp on
2066 * errors. It causes b_bufobj dereference in
2067 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2068 * pbrelvp() above.
2069 */
2070 if ((bp->b_ioflags & BIO_ERROR) != 0)
2071 bp->b_flags |= B_INVAL;
2072 bufdone(bp);
2073 BO_LOCK(bufobj);
2074 /*
2075 * Clear the BV_BKGRDINPROG flag in the original buffer
2076 * and awaken it if it is waiting for the write to complete.
2077 * If BV_BKGRDINPROG is not set in the original buffer it must
2078 * have been released and re-instantiated - which is not legal.
2079 */
2080 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2081 ("backgroundwritedone: lost buffer2"));
2082 origbp->b_vflags &= ~BV_BKGRDINPROG;
2083 if (origbp->b_vflags & BV_BKGRDWAIT) {
2084 origbp->b_vflags &= ~BV_BKGRDWAIT;
2085 wakeup(&origbp->b_xflags);
2086 }
2087 BO_UNLOCK(bufobj);
2088 }
2089
2090
2091 /*
2092 * Write, release buffer on completion. (Done by iodone
2093 * if async). Do not bother writing anything if the buffer
2094 * is invalid.
2095 *
2096 * Note that we set B_CACHE here, indicating that buffer is
2097 * fully valid and thus cacheable. This is true even of NFS
2098 * now so we set it generally. This could be set either here
2099 * or in biodone() since the I/O is synchronous. We put it
2100 * here.
2101 */
2102 static int
ffs_bufwrite(struct buf * bp)2103 ffs_bufwrite(struct buf *bp)
2104 {
2105 struct buf *newbp;
2106 struct cg *cgp;
2107
2108 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2109 if (bp->b_flags & B_INVAL) {
2110 brelse(bp);
2111 return (0);
2112 }
2113
2114 if (!BUF_ISLOCKED(bp))
2115 panic("bufwrite: buffer is not busy???");
2116 /*
2117 * If a background write is already in progress, delay
2118 * writing this block if it is asynchronous. Otherwise
2119 * wait for the background write to complete.
2120 */
2121 BO_LOCK(bp->b_bufobj);
2122 if (bp->b_vflags & BV_BKGRDINPROG) {
2123 if (bp->b_flags & B_ASYNC) {
2124 BO_UNLOCK(bp->b_bufobj);
2125 bdwrite(bp);
2126 return (0);
2127 }
2128 bp->b_vflags |= BV_BKGRDWAIT;
2129 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2130 "bwrbg", 0);
2131 if (bp->b_vflags & BV_BKGRDINPROG)
2132 panic("bufwrite: still writing");
2133 }
2134 bp->b_vflags &= ~BV_BKGRDERR;
2135 BO_UNLOCK(bp->b_bufobj);
2136
2137 /*
2138 * If this buffer is marked for background writing and we
2139 * do not have to wait for it, make a copy and write the
2140 * copy so as to leave this buffer ready for further use.
2141 *
2142 * This optimization eats a lot of memory. If we have a page
2143 * or buffer shortfall we can't do it.
2144 */
2145 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2146 (bp->b_flags & B_ASYNC) &&
2147 !vm_page_count_severe() &&
2148 !buf_dirty_count_severe()) {
2149 KASSERT(bp->b_iodone == NULL,
2150 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2151
2152 /* get a new block */
2153 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2154 if (newbp == NULL)
2155 goto normal_write;
2156
2157 KASSERT(buf_mapped(bp), ("Unmapped cg"));
2158 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2159 BO_LOCK(bp->b_bufobj);
2160 bp->b_vflags |= BV_BKGRDINPROG;
2161 BO_UNLOCK(bp->b_bufobj);
2162 newbp->b_xflags |=
2163 (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER;
2164 newbp->b_lblkno = bp->b_lblkno;
2165 newbp->b_blkno = bp->b_blkno;
2166 newbp->b_offset = bp->b_offset;
2167 newbp->b_iodone = ffs_backgroundwritedone;
2168 newbp->b_flags |= B_ASYNC;
2169 newbp->b_flags &= ~B_INVAL;
2170 pbgetvp(bp->b_vp, newbp);
2171
2172 #ifdef SOFTUPDATES
2173 /*
2174 * Move over the dependencies. If there are rollbacks,
2175 * leave the parent buffer dirtied as it will need to
2176 * be written again.
2177 */
2178 if (LIST_EMPTY(&bp->b_dep) ||
2179 softdep_move_dependencies(bp, newbp) == 0)
2180 bundirty(bp);
2181 #else
2182 bundirty(bp);
2183 #endif
2184
2185 /*
2186 * Initiate write on the copy, release the original. The
2187 * BKGRDINPROG flag prevents it from going away until
2188 * the background write completes. We have to recalculate
2189 * its check hash in case the buffer gets freed and then
2190 * reconstituted from the buffer cache during a later read.
2191 */
2192 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2193 cgp = (struct cg *)bp->b_data;
2194 cgp->cg_ckhash = 0;
2195 cgp->cg_ckhash =
2196 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2197 }
2198 bqrelse(bp);
2199 bp = newbp;
2200 } else
2201 /* Mark the buffer clean */
2202 bundirty(bp);
2203
2204
2205 /* Let the normal bufwrite do the rest for us */
2206 normal_write:
2207 /*
2208 * If we are writing a cylinder group, update its time.
2209 */
2210 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2211 cgp = (struct cg *)bp->b_data;
2212 cgp->cg_old_time = cgp->cg_time = time_second;
2213 }
2214 return (bufwrite(bp));
2215 }
2216
2217
2218 static void
ffs_geom_strategy(struct bufobj * bo,struct buf * bp)2219 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2220 {
2221 struct vnode *vp;
2222 struct buf *tbp;
2223 int error, nocopy;
2224
2225 vp = bo2vnode(bo);
2226 if (bp->b_iocmd == BIO_WRITE) {
2227 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2228 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2229 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2230 panic("ffs_geom_strategy: bad I/O");
2231 nocopy = bp->b_flags & B_NOCOPY;
2232 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2233 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2234 vp->v_rdev->si_snapdata != NULL) {
2235 if ((bp->b_flags & B_CLUSTER) != 0) {
2236 runningbufwakeup(bp);
2237 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2238 b_cluster.cluster_entry) {
2239 error = ffs_copyonwrite(vp, tbp);
2240 if (error != 0 &&
2241 error != EOPNOTSUPP) {
2242 bp->b_error = error;
2243 bp->b_ioflags |= BIO_ERROR;
2244 bp->b_flags &= ~B_BARRIER;
2245 bufdone(bp);
2246 return;
2247 }
2248 }
2249 bp->b_runningbufspace = bp->b_bufsize;
2250 atomic_add_long(&runningbufspace,
2251 bp->b_runningbufspace);
2252 } else {
2253 error = ffs_copyonwrite(vp, bp);
2254 if (error != 0 && error != EOPNOTSUPP) {
2255 bp->b_error = error;
2256 bp->b_ioflags |= BIO_ERROR;
2257 bp->b_flags &= ~B_BARRIER;
2258 bufdone(bp);
2259 return;
2260 }
2261 }
2262 }
2263 #ifdef SOFTUPDATES
2264 if ((bp->b_flags & B_CLUSTER) != 0) {
2265 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2266 b_cluster.cluster_entry) {
2267 if (!LIST_EMPTY(&tbp->b_dep))
2268 buf_start(tbp);
2269 }
2270 } else {
2271 if (!LIST_EMPTY(&bp->b_dep))
2272 buf_start(bp);
2273 }
2274
2275 #endif
2276 /*
2277 * Check for metadata that needs check-hashes and update them.
2278 */
2279 switch (bp->b_xflags & BX_FSPRIV) {
2280 case BX_CYLGRP:
2281 ((struct cg *)bp->b_data)->cg_ckhash = 0;
2282 ((struct cg *)bp->b_data)->cg_ckhash =
2283 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2284 break;
2285
2286 case BX_SUPERBLOCK:
2287 case BX_INODE:
2288 case BX_INDIR:
2289 case BX_DIR:
2290 printf("Check-hash write is unimplemented!!!\n");
2291 break;
2292
2293 case 0:
2294 break;
2295
2296 default:
2297 printf("multiple buffer types 0x%b\n",
2298 (u_int)(bp->b_xflags & BX_FSPRIV),
2299 PRINT_UFS_BUF_XFLAGS);
2300 break;
2301 }
2302 }
2303 g_vfs_strategy(bo, bp);
2304 }
2305
2306 int
ffs_own_mount(const struct mount * mp)2307 ffs_own_mount(const struct mount *mp)
2308 {
2309
2310 if (mp->mnt_op == &ufs_vfsops)
2311 return (1);
2312 return (0);
2313 }
2314
2315 #ifdef DDB
2316 #ifdef SOFTUPDATES
2317
2318 /* defined in ffs_softdep.c */
2319 extern void db_print_ffs(struct ufsmount *ump);
2320
DB_SHOW_COMMAND(ffs,db_show_ffs)2321 DB_SHOW_COMMAND(ffs, db_show_ffs)
2322 {
2323 struct mount *mp;
2324 struct ufsmount *ump;
2325
2326 if (have_addr) {
2327 ump = VFSTOUFS((struct mount *)addr);
2328 db_print_ffs(ump);
2329 return;
2330 }
2331
2332 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2333 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2334 db_print_ffs(VFSTOUFS(mp));
2335 }
2336 }
2337
2338 #endif /* SOFTUPDATES */
2339 #endif /* DDB */
2340