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