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