xref: /linux/fs/xfs/xfs_mount.c (revision e467705a9fb37f51595aa6deaca085ccb4005454)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_sb.h"
14 #include "xfs_mount.h"
15 #include "xfs_inode.h"
16 #include "xfs_dir2.h"
17 #include "xfs_ialloc.h"
18 #include "xfs_alloc.h"
19 #include "xfs_rtalloc.h"
20 #include "xfs_bmap.h"
21 #include "xfs_trans.h"
22 #include "xfs_trans_priv.h"
23 #include "xfs_log.h"
24 #include "xfs_log_priv.h"
25 #include "xfs_error.h"
26 #include "xfs_quota.h"
27 #include "xfs_fsops.h"
28 #include "xfs_icache.h"
29 #include "xfs_sysfs.h"
30 #include "xfs_rmap_btree.h"
31 #include "xfs_refcount_btree.h"
32 #include "xfs_reflink.h"
33 #include "xfs_extent_busy.h"
34 #include "xfs_health.h"
35 #include "xfs_trace.h"
36 #include "xfs_ag.h"
37 #include "xfs_rtbitmap.h"
38 #include "scrub/stats.h"
39 
40 static DEFINE_MUTEX(xfs_uuid_table_mutex);
41 static int xfs_uuid_table_size;
42 static uuid_t *xfs_uuid_table;
43 
44 void
45 xfs_uuid_table_free(void)
46 {
47 	if (xfs_uuid_table_size == 0)
48 		return;
49 	kfree(xfs_uuid_table);
50 	xfs_uuid_table = NULL;
51 	xfs_uuid_table_size = 0;
52 }
53 
54 /*
55  * See if the UUID is unique among mounted XFS filesystems.
56  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
57  */
58 STATIC int
59 xfs_uuid_mount(
60 	struct xfs_mount	*mp)
61 {
62 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
63 	int			hole, i;
64 
65 	/* Publish UUID in struct super_block */
66 	super_set_uuid(mp->m_super, uuid->b, sizeof(*uuid));
67 
68 	if (xfs_has_nouuid(mp))
69 		return 0;
70 
71 	if (uuid_is_null(uuid)) {
72 		xfs_warn(mp, "Filesystem has null UUID - can't mount");
73 		return -EINVAL;
74 	}
75 
76 	mutex_lock(&xfs_uuid_table_mutex);
77 	for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
78 		if (uuid_is_null(&xfs_uuid_table[i])) {
79 			hole = i;
80 			continue;
81 		}
82 		if (uuid_equal(uuid, &xfs_uuid_table[i]))
83 			goto out_duplicate;
84 	}
85 
86 	if (hole < 0) {
87 		xfs_uuid_table = krealloc(xfs_uuid_table,
88 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
89 			GFP_KERNEL | __GFP_NOFAIL);
90 		hole = xfs_uuid_table_size++;
91 	}
92 	xfs_uuid_table[hole] = *uuid;
93 	mutex_unlock(&xfs_uuid_table_mutex);
94 
95 	return 0;
96 
97  out_duplicate:
98 	mutex_unlock(&xfs_uuid_table_mutex);
99 	xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
100 	return -EINVAL;
101 }
102 
103 STATIC void
104 xfs_uuid_unmount(
105 	struct xfs_mount	*mp)
106 {
107 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
108 	int			i;
109 
110 	if (xfs_has_nouuid(mp))
111 		return;
112 
113 	mutex_lock(&xfs_uuid_table_mutex);
114 	for (i = 0; i < xfs_uuid_table_size; i++) {
115 		if (uuid_is_null(&xfs_uuid_table[i]))
116 			continue;
117 		if (!uuid_equal(uuid, &xfs_uuid_table[i]))
118 			continue;
119 		memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
120 		break;
121 	}
122 	ASSERT(i < xfs_uuid_table_size);
123 	mutex_unlock(&xfs_uuid_table_mutex);
124 }
125 
126 /*
127  * Check size of device based on the (data/realtime) block count.
128  * Note: this check is used by the growfs code as well as mount.
129  */
130 int
131 xfs_sb_validate_fsb_count(
132 	xfs_sb_t	*sbp,
133 	uint64_t	nblocks)
134 {
135 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
136 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
137 
138 	/* Limited by ULONG_MAX of page cache index */
139 	if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
140 		return -EFBIG;
141 	return 0;
142 }
143 
144 /*
145  * xfs_readsb
146  *
147  * Does the initial read of the superblock.
148  */
149 int
150 xfs_readsb(
151 	struct xfs_mount *mp,
152 	int		flags)
153 {
154 	unsigned int	sector_size;
155 	struct xfs_buf	*bp;
156 	struct xfs_sb	*sbp = &mp->m_sb;
157 	int		error;
158 	int		loud = !(flags & XFS_MFSI_QUIET);
159 	const struct xfs_buf_ops *buf_ops;
160 
161 	ASSERT(mp->m_sb_bp == NULL);
162 	ASSERT(mp->m_ddev_targp != NULL);
163 
164 	/*
165 	 * For the initial read, we must guess at the sector
166 	 * size based on the block device.  It's enough to
167 	 * get the sb_sectsize out of the superblock and
168 	 * then reread with the proper length.
169 	 * We don't verify it yet, because it may not be complete.
170 	 */
171 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
172 	buf_ops = NULL;
173 
174 	/*
175 	 * Allocate a (locked) buffer to hold the superblock. This will be kept
176 	 * around at all times to optimize access to the superblock. Therefore,
177 	 * set XBF_NO_IOACCT to make sure it doesn't hold the buftarg count
178 	 * elevated.
179 	 */
180 reread:
181 	error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
182 				      BTOBB(sector_size), XBF_NO_IOACCT, &bp,
183 				      buf_ops);
184 	if (error) {
185 		if (loud)
186 			xfs_warn(mp, "SB validate failed with error %d.", error);
187 		/* bad CRC means corrupted metadata */
188 		if (error == -EFSBADCRC)
189 			error = -EFSCORRUPTED;
190 		return error;
191 	}
192 
193 	/*
194 	 * Initialize the mount structure from the superblock.
195 	 */
196 	xfs_sb_from_disk(sbp, bp->b_addr);
197 
198 	/*
199 	 * If we haven't validated the superblock, do so now before we try
200 	 * to check the sector size and reread the superblock appropriately.
201 	 */
202 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
203 		if (loud)
204 			xfs_warn(mp, "Invalid superblock magic number");
205 		error = -EINVAL;
206 		goto release_buf;
207 	}
208 
209 	/*
210 	 * We must be able to do sector-sized and sector-aligned IO.
211 	 */
212 	if (sector_size > sbp->sb_sectsize) {
213 		if (loud)
214 			xfs_warn(mp, "device supports %u byte sectors (not %u)",
215 				sector_size, sbp->sb_sectsize);
216 		error = -ENOSYS;
217 		goto release_buf;
218 	}
219 
220 	if (buf_ops == NULL) {
221 		/*
222 		 * Re-read the superblock so the buffer is correctly sized,
223 		 * and properly verified.
224 		 */
225 		xfs_buf_relse(bp);
226 		sector_size = sbp->sb_sectsize;
227 		buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops;
228 		goto reread;
229 	}
230 
231 	mp->m_features |= xfs_sb_version_to_features(sbp);
232 	xfs_reinit_percpu_counters(mp);
233 
234 	/*
235 	 * If logged xattrs are enabled after log recovery finishes, then set
236 	 * the opstate so that log recovery will work properly.
237 	 */
238 	if (xfs_sb_version_haslogxattrs(&mp->m_sb))
239 		xfs_set_using_logged_xattrs(mp);
240 
241 	/* no need to be quiet anymore, so reset the buf ops */
242 	bp->b_ops = &xfs_sb_buf_ops;
243 
244 	mp->m_sb_bp = bp;
245 	xfs_buf_unlock(bp);
246 	return 0;
247 
248 release_buf:
249 	xfs_buf_relse(bp);
250 	return error;
251 }
252 
253 /*
254  * If the sunit/swidth change would move the precomputed root inode value, we
255  * must reject the ondisk change because repair will stumble over that.
256  * However, we allow the mount to proceed because we never rejected this
257  * combination before.  Returns true to update the sb, false otherwise.
258  */
259 static inline int
260 xfs_check_new_dalign(
261 	struct xfs_mount	*mp,
262 	int			new_dalign,
263 	bool			*update_sb)
264 {
265 	struct xfs_sb		*sbp = &mp->m_sb;
266 	xfs_ino_t		calc_ino;
267 
268 	calc_ino = xfs_ialloc_calc_rootino(mp, new_dalign);
269 	trace_xfs_check_new_dalign(mp, new_dalign, calc_ino);
270 
271 	if (sbp->sb_rootino == calc_ino) {
272 		*update_sb = true;
273 		return 0;
274 	}
275 
276 	xfs_warn(mp,
277 "Cannot change stripe alignment; would require moving root inode.");
278 
279 	/*
280 	 * XXX: Next time we add a new incompat feature, this should start
281 	 * returning -EINVAL to fail the mount.  Until then, spit out a warning
282 	 * that we're ignoring the administrator's instructions.
283 	 */
284 	xfs_warn(mp, "Skipping superblock stripe alignment update.");
285 	*update_sb = false;
286 	return 0;
287 }
288 
289 /*
290  * If we were provided with new sunit/swidth values as mount options, make sure
291  * that they pass basic alignment and superblock feature checks, and convert
292  * them into the same units (FSB) that everything else expects.  This step
293  * /must/ be done before computing the inode geometry.
294  */
295 STATIC int
296 xfs_validate_new_dalign(
297 	struct xfs_mount	*mp)
298 {
299 	if (mp->m_dalign == 0)
300 		return 0;
301 
302 	/*
303 	 * If stripe unit and stripe width are not multiples
304 	 * of the fs blocksize turn off alignment.
305 	 */
306 	if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
307 	    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
308 		xfs_warn(mp,
309 	"alignment check failed: sunit/swidth vs. blocksize(%d)",
310 			mp->m_sb.sb_blocksize);
311 		return -EINVAL;
312 	}
313 
314 	/*
315 	 * Convert the stripe unit and width to FSBs.
316 	 */
317 	mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
318 	if (mp->m_dalign && (mp->m_sb.sb_agblocks % mp->m_dalign)) {
319 		xfs_warn(mp,
320 	"alignment check failed: sunit/swidth vs. agsize(%d)",
321 			mp->m_sb.sb_agblocks);
322 		return -EINVAL;
323 	}
324 
325 	if (!mp->m_dalign) {
326 		xfs_warn(mp,
327 	"alignment check failed: sunit(%d) less than bsize(%d)",
328 			mp->m_dalign, mp->m_sb.sb_blocksize);
329 		return -EINVAL;
330 	}
331 
332 	mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
333 
334 	if (!xfs_has_dalign(mp)) {
335 		xfs_warn(mp,
336 "cannot change alignment: superblock does not support data alignment");
337 		return -EINVAL;
338 	}
339 
340 	return 0;
341 }
342 
343 /* Update alignment values based on mount options and sb values. */
344 STATIC int
345 xfs_update_alignment(
346 	struct xfs_mount	*mp)
347 {
348 	struct xfs_sb		*sbp = &mp->m_sb;
349 
350 	if (mp->m_dalign) {
351 		bool		update_sb;
352 		int		error;
353 
354 		if (sbp->sb_unit == mp->m_dalign &&
355 		    sbp->sb_width == mp->m_swidth)
356 			return 0;
357 
358 		error = xfs_check_new_dalign(mp, mp->m_dalign, &update_sb);
359 		if (error || !update_sb)
360 			return error;
361 
362 		sbp->sb_unit = mp->m_dalign;
363 		sbp->sb_width = mp->m_swidth;
364 		mp->m_update_sb = true;
365 	} else if (!xfs_has_noalign(mp) && xfs_has_dalign(mp)) {
366 		mp->m_dalign = sbp->sb_unit;
367 		mp->m_swidth = sbp->sb_width;
368 	}
369 
370 	return 0;
371 }
372 
373 /*
374  * precalculate the low space thresholds for dynamic speculative preallocation.
375  */
376 void
377 xfs_set_low_space_thresholds(
378 	struct xfs_mount	*mp)
379 {
380 	uint64_t		dblocks = mp->m_sb.sb_dblocks;
381 	uint64_t		rtexts = mp->m_sb.sb_rextents;
382 	int			i;
383 
384 	do_div(dblocks, 100);
385 	do_div(rtexts, 100);
386 
387 	for (i = 0; i < XFS_LOWSP_MAX; i++) {
388 		mp->m_low_space[i] = dblocks * (i + 1);
389 		mp->m_low_rtexts[i] = rtexts * (i + 1);
390 	}
391 }
392 
393 /*
394  * Check that the data (and log if separate) is an ok size.
395  */
396 STATIC int
397 xfs_check_sizes(
398 	struct xfs_mount *mp)
399 {
400 	struct xfs_buf	*bp;
401 	xfs_daddr_t	d;
402 	int		error;
403 
404 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
405 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
406 		xfs_warn(mp, "filesystem size mismatch detected");
407 		return -EFBIG;
408 	}
409 	error = xfs_buf_read_uncached(mp->m_ddev_targp,
410 					d - XFS_FSS_TO_BB(mp, 1),
411 					XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
412 	if (error) {
413 		xfs_warn(mp, "last sector read failed");
414 		return error;
415 	}
416 	xfs_buf_relse(bp);
417 
418 	if (mp->m_logdev_targp == mp->m_ddev_targp)
419 		return 0;
420 
421 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
422 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
423 		xfs_warn(mp, "log size mismatch detected");
424 		return -EFBIG;
425 	}
426 	error = xfs_buf_read_uncached(mp->m_logdev_targp,
427 					d - XFS_FSB_TO_BB(mp, 1),
428 					XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
429 	if (error) {
430 		xfs_warn(mp, "log device read failed");
431 		return error;
432 	}
433 	xfs_buf_relse(bp);
434 	return 0;
435 }
436 
437 /*
438  * Clear the quotaflags in memory and in the superblock.
439  */
440 int
441 xfs_mount_reset_sbqflags(
442 	struct xfs_mount	*mp)
443 {
444 	mp->m_qflags = 0;
445 
446 	/* It is OK to look at sb_qflags in the mount path without m_sb_lock. */
447 	if (mp->m_sb.sb_qflags == 0)
448 		return 0;
449 	spin_lock(&mp->m_sb_lock);
450 	mp->m_sb.sb_qflags = 0;
451 	spin_unlock(&mp->m_sb_lock);
452 
453 	if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
454 		return 0;
455 
456 	return xfs_sync_sb(mp, false);
457 }
458 
459 uint64_t
460 xfs_default_resblks(xfs_mount_t *mp)
461 {
462 	uint64_t resblks;
463 
464 	/*
465 	 * We default to 5% or 8192 fsbs of space reserved, whichever is
466 	 * smaller.  This is intended to cover concurrent allocation
467 	 * transactions when we initially hit enospc. These each require a 4
468 	 * block reservation. Hence by default we cover roughly 2000 concurrent
469 	 * allocation reservations.
470 	 */
471 	resblks = mp->m_sb.sb_dblocks;
472 	do_div(resblks, 20);
473 	resblks = min_t(uint64_t, resblks, 8192);
474 	return resblks;
475 }
476 
477 /* Ensure the summary counts are correct. */
478 STATIC int
479 xfs_check_summary_counts(
480 	struct xfs_mount	*mp)
481 {
482 	int			error = 0;
483 
484 	/*
485 	 * The AG0 superblock verifier rejects in-progress filesystems,
486 	 * so we should never see the flag set this far into mounting.
487 	 */
488 	if (mp->m_sb.sb_inprogress) {
489 		xfs_err(mp, "sb_inprogress set after log recovery??");
490 		WARN_ON(1);
491 		return -EFSCORRUPTED;
492 	}
493 
494 	/*
495 	 * Now the log is mounted, we know if it was an unclean shutdown or
496 	 * not. If it was, with the first phase of recovery has completed, we
497 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
498 	 * but they are recovered transactionally in the second recovery phase
499 	 * later.
500 	 *
501 	 * If the log was clean when we mounted, we can check the summary
502 	 * counters.  If any of them are obviously incorrect, we can recompute
503 	 * them from the AGF headers in the next step.
504 	 */
505 	if (xfs_is_clean(mp) &&
506 	    (mp->m_sb.sb_fdblocks > mp->m_sb.sb_dblocks ||
507 	     !xfs_verify_icount(mp, mp->m_sb.sb_icount) ||
508 	     mp->m_sb.sb_ifree > mp->m_sb.sb_icount))
509 		xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
510 
511 	/*
512 	 * We can safely re-initialise incore superblock counters from the
513 	 * per-ag data. These may not be correct if the filesystem was not
514 	 * cleanly unmounted, so we waited for recovery to finish before doing
515 	 * this.
516 	 *
517 	 * If the filesystem was cleanly unmounted or the previous check did
518 	 * not flag anything weird, then we can trust the values in the
519 	 * superblock to be correct and we don't need to do anything here.
520 	 * Otherwise, recalculate the summary counters.
521 	 */
522 	if ((xfs_has_lazysbcount(mp) && !xfs_is_clean(mp)) ||
523 	    xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS)) {
524 		error = xfs_initialize_perag_data(mp, mp->m_sb.sb_agcount);
525 		if (error)
526 			return error;
527 	}
528 
529 	/*
530 	 * Older kernels misused sb_frextents to reflect both incore
531 	 * reservations made by running transactions and the actual count of
532 	 * free rt extents in the ondisk metadata.  Transactions committed
533 	 * during runtime can therefore contain a superblock update that
534 	 * undercounts the number of free rt extents tracked in the rt bitmap.
535 	 * A clean unmount record will have the correct frextents value since
536 	 * there can be no other transactions running at that point.
537 	 *
538 	 * If we're mounting the rt volume after recovering the log, recompute
539 	 * frextents from the rtbitmap file to fix the inconsistency.
540 	 */
541 	if (xfs_has_realtime(mp) && !xfs_is_clean(mp)) {
542 		error = xfs_rtalloc_reinit_frextents(mp);
543 		if (error)
544 			return error;
545 	}
546 
547 	return 0;
548 }
549 
550 static void
551 xfs_unmount_check(
552 	struct xfs_mount	*mp)
553 {
554 	if (xfs_is_shutdown(mp))
555 		return;
556 
557 	if (percpu_counter_sum(&mp->m_ifree) >
558 			percpu_counter_sum(&mp->m_icount)) {
559 		xfs_alert(mp, "ifree/icount mismatch at unmount");
560 		xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
561 	}
562 }
563 
564 /*
565  * Flush and reclaim dirty inodes in preparation for unmount. Inodes and
566  * internal inode structures can be sitting in the CIL and AIL at this point,
567  * so we need to unpin them, write them back and/or reclaim them before unmount
568  * can proceed.  In other words, callers are required to have inactivated all
569  * inodes.
570  *
571  * An inode cluster that has been freed can have its buffer still pinned in
572  * memory because the transaction is still sitting in a iclog. The stale inodes
573  * on that buffer will be pinned to the buffer until the transaction hits the
574  * disk and the callbacks run. Pushing the AIL will skip the stale inodes and
575  * may never see the pinned buffer, so nothing will push out the iclog and
576  * unpin the buffer.
577  *
578  * Hence we need to force the log to unpin everything first. However, log
579  * forces don't wait for the discards they issue to complete, so we have to
580  * explicitly wait for them to complete here as well.
581  *
582  * Then we can tell the world we are unmounting so that error handling knows
583  * that the filesystem is going away and we should error out anything that we
584  * have been retrying in the background.  This will prevent never-ending
585  * retries in AIL pushing from hanging the unmount.
586  *
587  * Finally, we can push the AIL to clean all the remaining dirty objects, then
588  * reclaim the remaining inodes that are still in memory at this point in time.
589  */
590 static void
591 xfs_unmount_flush_inodes(
592 	struct xfs_mount	*mp)
593 {
594 	xfs_log_force(mp, XFS_LOG_SYNC);
595 	xfs_extent_busy_wait_all(mp);
596 	flush_workqueue(xfs_discard_wq);
597 
598 	set_bit(XFS_OPSTATE_UNMOUNTING, &mp->m_opstate);
599 
600 	xfs_ail_push_all_sync(mp->m_ail);
601 	xfs_inodegc_stop(mp);
602 	cancel_delayed_work_sync(&mp->m_reclaim_work);
603 	xfs_reclaim_inodes(mp);
604 	xfs_health_unmount(mp);
605 }
606 
607 static void
608 xfs_mount_setup_inode_geom(
609 	struct xfs_mount	*mp)
610 {
611 	struct xfs_ino_geometry *igeo = M_IGEO(mp);
612 
613 	igeo->attr_fork_offset = xfs_bmap_compute_attr_offset(mp);
614 	ASSERT(igeo->attr_fork_offset < XFS_LITINO(mp));
615 
616 	xfs_ialloc_setup_geometry(mp);
617 }
618 
619 /* Compute maximum possible height for per-AG btree types for this fs. */
620 static inline void
621 xfs_agbtree_compute_maxlevels(
622 	struct xfs_mount	*mp)
623 {
624 	unsigned int		levels;
625 
626 	levels = max(mp->m_alloc_maxlevels, M_IGEO(mp)->inobt_maxlevels);
627 	levels = max(levels, mp->m_rmap_maxlevels);
628 	mp->m_agbtree_maxlevels = max(levels, mp->m_refc_maxlevels);
629 }
630 
631 /*
632  * This function does the following on an initial mount of a file system:
633  *	- reads the superblock from disk and init the mount struct
634  *	- if we're a 32-bit kernel, do a size check on the superblock
635  *		so we don't mount terabyte filesystems
636  *	- init mount struct realtime fields
637  *	- allocate inode hash table for fs
638  *	- init directory manager
639  *	- perform recovery and init the log manager
640  */
641 int
642 xfs_mountfs(
643 	struct xfs_mount	*mp)
644 {
645 	struct xfs_sb		*sbp = &(mp->m_sb);
646 	struct xfs_inode	*rip;
647 	struct xfs_ino_geometry	*igeo = M_IGEO(mp);
648 	uint			quotamount = 0;
649 	uint			quotaflags = 0;
650 	int			error = 0;
651 
652 	xfs_sb_mount_common(mp, sbp);
653 
654 	/*
655 	 * Check for a mismatched features2 values.  Older kernels read & wrote
656 	 * into the wrong sb offset for sb_features2 on some platforms due to
657 	 * xfs_sb_t not being 64bit size aligned when sb_features2 was added,
658 	 * which made older superblock reading/writing routines swap it as a
659 	 * 64-bit value.
660 	 *
661 	 * For backwards compatibility, we make both slots equal.
662 	 *
663 	 * If we detect a mismatched field, we OR the set bits into the existing
664 	 * features2 field in case it has already been modified; we don't want
665 	 * to lose any features.  We then update the bad location with the ORed
666 	 * value so that older kernels will see any features2 flags. The
667 	 * superblock writeback code ensures the new sb_features2 is copied to
668 	 * sb_bad_features2 before it is logged or written to disk.
669 	 */
670 	if (xfs_sb_has_mismatched_features2(sbp)) {
671 		xfs_warn(mp, "correcting sb_features alignment problem");
672 		sbp->sb_features2 |= sbp->sb_bad_features2;
673 		mp->m_update_sb = true;
674 	}
675 
676 
677 	/* always use v2 inodes by default now */
678 	if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
679 		mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
680 		mp->m_features |= XFS_FEAT_NLINK;
681 		mp->m_update_sb = true;
682 	}
683 
684 	/*
685 	 * If we were given new sunit/swidth options, do some basic validation
686 	 * checks and convert the incore dalign and swidth values to the
687 	 * same units (FSB) that everything else uses.  This /must/ happen
688 	 * before computing the inode geometry.
689 	 */
690 	error = xfs_validate_new_dalign(mp);
691 	if (error)
692 		goto out;
693 
694 	xfs_alloc_compute_maxlevels(mp);
695 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
696 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
697 	xfs_mount_setup_inode_geom(mp);
698 	xfs_rmapbt_compute_maxlevels(mp);
699 	xfs_refcountbt_compute_maxlevels(mp);
700 
701 	xfs_agbtree_compute_maxlevels(mp);
702 
703 	/*
704 	 * Check if sb_agblocks is aligned at stripe boundary.  If sb_agblocks
705 	 * is NOT aligned turn off m_dalign since allocator alignment is within
706 	 * an ag, therefore ag has to be aligned at stripe boundary.  Note that
707 	 * we must compute the free space and rmap btree geometry before doing
708 	 * this.
709 	 */
710 	error = xfs_update_alignment(mp);
711 	if (error)
712 		goto out;
713 
714 	/* enable fail_at_unmount as default */
715 	mp->m_fail_unmount = true;
716 
717 	super_set_sysfs_name_id(mp->m_super);
718 
719 	error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype,
720 			       NULL, mp->m_super->s_id);
721 	if (error)
722 		goto out;
723 
724 	error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype,
725 			       &mp->m_kobj, "stats");
726 	if (error)
727 		goto out_remove_sysfs;
728 
729 	xchk_stats_register(mp->m_scrub_stats, mp->m_debugfs);
730 
731 	error = xfs_error_sysfs_init(mp);
732 	if (error)
733 		goto out_remove_scrub_stats;
734 
735 	error = xfs_errortag_init(mp);
736 	if (error)
737 		goto out_remove_error_sysfs;
738 
739 	error = xfs_uuid_mount(mp);
740 	if (error)
741 		goto out_remove_errortag;
742 
743 	/*
744 	 * Update the preferred write size based on the information from the
745 	 * on-disk superblock.
746 	 */
747 	mp->m_allocsize_log =
748 		max_t(uint32_t, sbp->sb_blocklog, mp->m_allocsize_log);
749 	mp->m_allocsize_blocks = 1U << (mp->m_allocsize_log - sbp->sb_blocklog);
750 
751 	/* set the low space thresholds for dynamic preallocation */
752 	xfs_set_low_space_thresholds(mp);
753 
754 	/*
755 	 * If enabled, sparse inode chunk alignment is expected to match the
756 	 * cluster size. Full inode chunk alignment must match the chunk size,
757 	 * but that is checked on sb read verification...
758 	 */
759 	if (xfs_has_sparseinodes(mp) &&
760 	    mp->m_sb.sb_spino_align !=
761 			XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw)) {
762 		xfs_warn(mp,
763 	"Sparse inode block alignment (%u) must match cluster size (%llu).",
764 			 mp->m_sb.sb_spino_align,
765 			 XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw));
766 		error = -EINVAL;
767 		goto out_remove_uuid;
768 	}
769 
770 	/*
771 	 * Check that the data (and log if separate) is an ok size.
772 	 */
773 	error = xfs_check_sizes(mp);
774 	if (error)
775 		goto out_remove_uuid;
776 
777 	/*
778 	 * Initialize realtime fields in the mount structure
779 	 */
780 	error = xfs_rtmount_init(mp);
781 	if (error) {
782 		xfs_warn(mp, "RT mount failed");
783 		goto out_remove_uuid;
784 	}
785 
786 	/*
787 	 *  Copies the low order bits of the timestamp and the randomly
788 	 *  set "sequence" number out of a UUID.
789 	 */
790 	mp->m_fixedfsid[0] =
791 		(get_unaligned_be16(&sbp->sb_uuid.b[8]) << 16) |
792 		 get_unaligned_be16(&sbp->sb_uuid.b[4]);
793 	mp->m_fixedfsid[1] = get_unaligned_be32(&sbp->sb_uuid.b[0]);
794 
795 	error = xfs_da_mount(mp);
796 	if (error) {
797 		xfs_warn(mp, "Failed dir/attr init: %d", error);
798 		goto out_remove_uuid;
799 	}
800 
801 	/*
802 	 * Initialize the precomputed transaction reservations values.
803 	 */
804 	xfs_trans_init(mp);
805 
806 	/*
807 	 * Allocate and initialize the per-ag data.
808 	 */
809 	error = xfs_initialize_perag(mp, sbp->sb_agcount, mp->m_sb.sb_dblocks,
810 			&mp->m_maxagi);
811 	if (error) {
812 		xfs_warn(mp, "Failed per-ag init: %d", error);
813 		goto out_free_dir;
814 	}
815 
816 	if (XFS_IS_CORRUPT(mp, !sbp->sb_logblocks)) {
817 		xfs_warn(mp, "no log defined");
818 		error = -EFSCORRUPTED;
819 		goto out_free_perag;
820 	}
821 
822 	error = xfs_inodegc_register_shrinker(mp);
823 	if (error)
824 		goto out_fail_wait;
825 
826 	/*
827 	 * Log's mount-time initialization. The first part of recovery can place
828 	 * some items on the AIL, to be handled when recovery is finished or
829 	 * cancelled.
830 	 */
831 	error = xfs_log_mount(mp, mp->m_logdev_targp,
832 			      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
833 			      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
834 	if (error) {
835 		xfs_warn(mp, "log mount failed");
836 		goto out_inodegc_shrinker;
837 	}
838 
839 	/*
840 	 * If logged xattrs are still enabled after log recovery finishes, then
841 	 * they'll be available until unmount.  Otherwise, turn them off.
842 	 */
843 	if (xfs_sb_version_haslogxattrs(&mp->m_sb))
844 		xfs_set_using_logged_xattrs(mp);
845 	else
846 		xfs_clear_using_logged_xattrs(mp);
847 
848 	/* Enable background inode inactivation workers. */
849 	xfs_inodegc_start(mp);
850 	xfs_blockgc_start(mp);
851 
852 	/*
853 	 * Now that we've recovered any pending superblock feature bit
854 	 * additions, we can finish setting up the attr2 behaviour for the
855 	 * mount. The noattr2 option overrides the superblock flag, so only
856 	 * check the superblock feature flag if the mount option is not set.
857 	 */
858 	if (xfs_has_noattr2(mp)) {
859 		mp->m_features &= ~XFS_FEAT_ATTR2;
860 	} else if (!xfs_has_attr2(mp) &&
861 		   (mp->m_sb.sb_features2 & XFS_SB_VERSION2_ATTR2BIT)) {
862 		mp->m_features |= XFS_FEAT_ATTR2;
863 	}
864 
865 	/*
866 	 * Get and sanity-check the root inode.
867 	 * Save the pointer to it in the mount structure.
868 	 */
869 	error = xfs_iget(mp, NULL, sbp->sb_rootino, XFS_IGET_UNTRUSTED,
870 			 XFS_ILOCK_EXCL, &rip);
871 	if (error) {
872 		xfs_warn(mp,
873 			"Failed to read root inode 0x%llx, error %d",
874 			sbp->sb_rootino, -error);
875 		goto out_log_dealloc;
876 	}
877 
878 	ASSERT(rip != NULL);
879 
880 	if (XFS_IS_CORRUPT(mp, !S_ISDIR(VFS_I(rip)->i_mode))) {
881 		xfs_warn(mp, "corrupted root inode %llu: not a directory",
882 			(unsigned long long)rip->i_ino);
883 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
884 		error = -EFSCORRUPTED;
885 		goto out_rele_rip;
886 	}
887 	mp->m_rootip = rip;	/* save it */
888 
889 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
890 
891 	/*
892 	 * Initialize realtime inode pointers in the mount structure
893 	 */
894 	error = xfs_rtmount_inodes(mp);
895 	if (error) {
896 		/*
897 		 * Free up the root inode.
898 		 */
899 		xfs_warn(mp, "failed to read RT inodes");
900 		goto out_rele_rip;
901 	}
902 
903 	/* Make sure the summary counts are ok. */
904 	error = xfs_check_summary_counts(mp);
905 	if (error)
906 		goto out_rtunmount;
907 
908 	/*
909 	 * If this is a read-only mount defer the superblock updates until
910 	 * the next remount into writeable mode.  Otherwise we would never
911 	 * perform the update e.g. for the root filesystem.
912 	 */
913 	if (mp->m_update_sb && !xfs_is_readonly(mp)) {
914 		error = xfs_sync_sb(mp, false);
915 		if (error) {
916 			xfs_warn(mp, "failed to write sb changes");
917 			goto out_rtunmount;
918 		}
919 	}
920 
921 	/*
922 	 * Initialise the XFS quota management subsystem for this mount
923 	 */
924 	if (XFS_IS_QUOTA_ON(mp)) {
925 		error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
926 		if (error)
927 			goto out_rtunmount;
928 	} else {
929 		/*
930 		 * If a file system had quotas running earlier, but decided to
931 		 * mount without -o uquota/pquota/gquota options, revoke the
932 		 * quotachecked license.
933 		 */
934 		if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
935 			xfs_notice(mp, "resetting quota flags");
936 			error = xfs_mount_reset_sbqflags(mp);
937 			if (error)
938 				goto out_rtunmount;
939 		}
940 	}
941 
942 	/*
943 	 * Finish recovering the file system.  This part needed to be delayed
944 	 * until after the root and real-time bitmap inodes were consistently
945 	 * read in.  Temporarily create per-AG space reservations for metadata
946 	 * btree shape changes because space freeing transactions (for inode
947 	 * inactivation) require the per-AG reservation in lieu of reserving
948 	 * blocks.
949 	 */
950 	error = xfs_fs_reserve_ag_blocks(mp);
951 	if (error && error == -ENOSPC)
952 		xfs_warn(mp,
953 	"ENOSPC reserving per-AG metadata pool, log recovery may fail.");
954 	error = xfs_log_mount_finish(mp);
955 	xfs_fs_unreserve_ag_blocks(mp);
956 	if (error) {
957 		xfs_warn(mp, "log mount finish failed");
958 		goto out_rtunmount;
959 	}
960 
961 	/*
962 	 * Now the log is fully replayed, we can transition to full read-only
963 	 * mode for read-only mounts. This will sync all the metadata and clean
964 	 * the log so that the recovery we just performed does not have to be
965 	 * replayed again on the next mount.
966 	 *
967 	 * We use the same quiesce mechanism as the rw->ro remount, as they are
968 	 * semantically identical operations.
969 	 */
970 	if (xfs_is_readonly(mp) && !xfs_has_norecovery(mp))
971 		xfs_log_clean(mp);
972 
973 	/*
974 	 * Complete the quota initialisation, post-log-replay component.
975 	 */
976 	if (quotamount) {
977 		ASSERT(mp->m_qflags == 0);
978 		mp->m_qflags = quotaflags;
979 
980 		xfs_qm_mount_quotas(mp);
981 	}
982 
983 	/*
984 	 * Now we are mounted, reserve a small amount of unused space for
985 	 * privileged transactions. This is needed so that transaction
986 	 * space required for critical operations can dip into this pool
987 	 * when at ENOSPC. This is needed for operations like create with
988 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
989 	 * are not allowed to use this reserved space.
990 	 *
991 	 * This may drive us straight to ENOSPC on mount, but that implies
992 	 * we were already there on the last unmount. Warn if this occurs.
993 	 */
994 	if (!xfs_is_readonly(mp)) {
995 		error = xfs_reserve_blocks(mp, xfs_default_resblks(mp));
996 		if (error)
997 			xfs_warn(mp,
998 	"Unable to allocate reserve blocks. Continuing without reserve pool.");
999 
1000 		/* Reserve AG blocks for future btree expansion. */
1001 		error = xfs_fs_reserve_ag_blocks(mp);
1002 		if (error && error != -ENOSPC)
1003 			goto out_agresv;
1004 	}
1005 
1006 	return 0;
1007 
1008  out_agresv:
1009 	xfs_fs_unreserve_ag_blocks(mp);
1010 	xfs_qm_unmount_quotas(mp);
1011  out_rtunmount:
1012 	xfs_rtunmount_inodes(mp);
1013  out_rele_rip:
1014 	xfs_irele(rip);
1015 	/* Clean out dquots that might be in memory after quotacheck. */
1016 	xfs_qm_unmount(mp);
1017 
1018 	/*
1019 	 * Inactivate all inodes that might still be in memory after a log
1020 	 * intent recovery failure so that reclaim can free them.  Metadata
1021 	 * inodes and the root directory shouldn't need inactivation, but the
1022 	 * mount failed for some reason, so pull down all the state and flee.
1023 	 */
1024 	xfs_inodegc_flush(mp);
1025 
1026 	/*
1027 	 * Flush all inode reclamation work and flush the log.
1028 	 * We have to do this /after/ rtunmount and qm_unmount because those
1029 	 * two will have scheduled delayed reclaim for the rt/quota inodes.
1030 	 *
1031 	 * This is slightly different from the unmountfs call sequence
1032 	 * because we could be tearing down a partially set up mount.  In
1033 	 * particular, if log_mount_finish fails we bail out without calling
1034 	 * qm_unmount_quotas and therefore rely on qm_unmount to release the
1035 	 * quota inodes.
1036 	 */
1037 	xfs_unmount_flush_inodes(mp);
1038  out_log_dealloc:
1039 	xfs_log_mount_cancel(mp);
1040  out_inodegc_shrinker:
1041 	shrinker_free(mp->m_inodegc_shrinker);
1042  out_fail_wait:
1043 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
1044 		xfs_buftarg_drain(mp->m_logdev_targp);
1045 	xfs_buftarg_drain(mp->m_ddev_targp);
1046  out_free_perag:
1047 	xfs_free_perag(mp);
1048  out_free_dir:
1049 	xfs_da_unmount(mp);
1050  out_remove_uuid:
1051 	xfs_uuid_unmount(mp);
1052  out_remove_errortag:
1053 	xfs_errortag_del(mp);
1054  out_remove_error_sysfs:
1055 	xfs_error_sysfs_del(mp);
1056  out_remove_scrub_stats:
1057 	xchk_stats_unregister(mp->m_scrub_stats);
1058 	xfs_sysfs_del(&mp->m_stats.xs_kobj);
1059  out_remove_sysfs:
1060 	xfs_sysfs_del(&mp->m_kobj);
1061  out:
1062 	return error;
1063 }
1064 
1065 /*
1066  * This flushes out the inodes,dquots and the superblock, unmounts the
1067  * log and makes sure that incore structures are freed.
1068  */
1069 void
1070 xfs_unmountfs(
1071 	struct xfs_mount	*mp)
1072 {
1073 	int			error;
1074 
1075 	/*
1076 	 * Perform all on-disk metadata updates required to inactivate inodes
1077 	 * that the VFS evicted earlier in the unmount process.  Freeing inodes
1078 	 * and discarding CoW fork preallocations can cause shape changes to
1079 	 * the free inode and refcount btrees, respectively, so we must finish
1080 	 * this before we discard the metadata space reservations.  Metadata
1081 	 * inodes and the root directory do not require inactivation.
1082 	 */
1083 	xfs_inodegc_flush(mp);
1084 
1085 	xfs_blockgc_stop(mp);
1086 	xfs_fs_unreserve_ag_blocks(mp);
1087 	xfs_qm_unmount_quotas(mp);
1088 	xfs_rtunmount_inodes(mp);
1089 	xfs_irele(mp->m_rootip);
1090 
1091 	xfs_unmount_flush_inodes(mp);
1092 
1093 	xfs_qm_unmount(mp);
1094 
1095 	/*
1096 	 * Unreserve any blocks we have so that when we unmount we don't account
1097 	 * the reserved free space as used. This is really only necessary for
1098 	 * lazy superblock counting because it trusts the incore superblock
1099 	 * counters to be absolutely correct on clean unmount.
1100 	 *
1101 	 * We don't bother correcting this elsewhere for lazy superblock
1102 	 * counting because on mount of an unclean filesystem we reconstruct the
1103 	 * correct counter value and this is irrelevant.
1104 	 *
1105 	 * For non-lazy counter filesystems, this doesn't matter at all because
1106 	 * we only every apply deltas to the superblock and hence the incore
1107 	 * value does not matter....
1108 	 */
1109 	error = xfs_reserve_blocks(mp, 0);
1110 	if (error)
1111 		xfs_warn(mp, "Unable to free reserved block pool. "
1112 				"Freespace may not be correct on next mount.");
1113 	xfs_unmount_check(mp);
1114 
1115 	/*
1116 	 * Indicate that it's ok to clear log incompat bits before cleaning
1117 	 * the log and writing the unmount record.
1118 	 */
1119 	xfs_set_done_with_log_incompat(mp);
1120 	xfs_log_unmount(mp);
1121 	xfs_da_unmount(mp);
1122 	xfs_uuid_unmount(mp);
1123 
1124 #if defined(DEBUG)
1125 	xfs_errortag_clearall(mp);
1126 #endif
1127 	shrinker_free(mp->m_inodegc_shrinker);
1128 	xfs_free_perag(mp);
1129 
1130 	xfs_errortag_del(mp);
1131 	xfs_error_sysfs_del(mp);
1132 	xchk_stats_unregister(mp->m_scrub_stats);
1133 	xfs_sysfs_del(&mp->m_stats.xs_kobj);
1134 	xfs_sysfs_del(&mp->m_kobj);
1135 }
1136 
1137 /*
1138  * Determine whether modifications can proceed. The caller specifies the minimum
1139  * freeze level for which modifications should not be allowed. This allows
1140  * certain operations to proceed while the freeze sequence is in progress, if
1141  * necessary.
1142  */
1143 bool
1144 xfs_fs_writable(
1145 	struct xfs_mount	*mp,
1146 	int			level)
1147 {
1148 	ASSERT(level > SB_UNFROZEN);
1149 	if ((mp->m_super->s_writers.frozen >= level) ||
1150 	    xfs_is_shutdown(mp) || xfs_is_readonly(mp))
1151 		return false;
1152 
1153 	return true;
1154 }
1155 
1156 void
1157 xfs_add_freecounter(
1158 	struct xfs_mount	*mp,
1159 	struct percpu_counter	*counter,
1160 	uint64_t		delta)
1161 {
1162 	bool			has_resv_pool = (counter == &mp->m_fdblocks);
1163 	uint64_t		res_used;
1164 
1165 	/*
1166 	 * If the reserve pool is depleted, put blocks back into it first.
1167 	 * Most of the time the pool is full.
1168 	 */
1169 	if (!has_resv_pool || mp->m_resblks == mp->m_resblks_avail) {
1170 		percpu_counter_add(counter, delta);
1171 		return;
1172 	}
1173 
1174 	spin_lock(&mp->m_sb_lock);
1175 	res_used = mp->m_resblks - mp->m_resblks_avail;
1176 	if (res_used > delta) {
1177 		mp->m_resblks_avail += delta;
1178 	} else {
1179 		delta -= res_used;
1180 		mp->m_resblks_avail = mp->m_resblks;
1181 		percpu_counter_add(counter, delta);
1182 	}
1183 	spin_unlock(&mp->m_sb_lock);
1184 }
1185 
1186 int
1187 xfs_dec_freecounter(
1188 	struct xfs_mount	*mp,
1189 	struct percpu_counter	*counter,
1190 	uint64_t		delta,
1191 	bool			rsvd)
1192 {
1193 	int64_t			lcounter;
1194 	uint64_t		set_aside = 0;
1195 	s32			batch;
1196 	bool			has_resv_pool;
1197 
1198 	ASSERT(counter == &mp->m_fdblocks || counter == &mp->m_frextents);
1199 	has_resv_pool = (counter == &mp->m_fdblocks);
1200 	if (rsvd)
1201 		ASSERT(has_resv_pool);
1202 
1203 	/*
1204 	 * Taking blocks away, need to be more accurate the closer we
1205 	 * are to zero.
1206 	 *
1207 	 * If the counter has a value of less than 2 * max batch size,
1208 	 * then make everything serialise as we are real close to
1209 	 * ENOSPC.
1210 	 */
1211 	if (__percpu_counter_compare(counter, 2 * XFS_FDBLOCKS_BATCH,
1212 				     XFS_FDBLOCKS_BATCH) < 0)
1213 		batch = 1;
1214 	else
1215 		batch = XFS_FDBLOCKS_BATCH;
1216 
1217 	/*
1218 	 * Set aside allocbt blocks because these blocks are tracked as free
1219 	 * space but not available for allocation. Technically this means that a
1220 	 * single reservation cannot consume all remaining free space, but the
1221 	 * ratio of allocbt blocks to usable free blocks should be rather small.
1222 	 * The tradeoff without this is that filesystems that maintain high
1223 	 * perag block reservations can over reserve physical block availability
1224 	 * and fail physical allocation, which leads to much more serious
1225 	 * problems (i.e. transaction abort, pagecache discards, etc.) than
1226 	 * slightly premature -ENOSPC.
1227 	 */
1228 	if (has_resv_pool)
1229 		set_aside = xfs_fdblocks_unavailable(mp);
1230 	percpu_counter_add_batch(counter, -((int64_t)delta), batch);
1231 	if (__percpu_counter_compare(counter, set_aside,
1232 				     XFS_FDBLOCKS_BATCH) >= 0) {
1233 		/* we had space! */
1234 		return 0;
1235 	}
1236 
1237 	/*
1238 	 * lock up the sb for dipping into reserves before releasing the space
1239 	 * that took us to ENOSPC.
1240 	 */
1241 	spin_lock(&mp->m_sb_lock);
1242 	percpu_counter_add(counter, delta);
1243 	if (!has_resv_pool || !rsvd)
1244 		goto fdblocks_enospc;
1245 
1246 	lcounter = (long long)mp->m_resblks_avail - delta;
1247 	if (lcounter >= 0) {
1248 		mp->m_resblks_avail = lcounter;
1249 		spin_unlock(&mp->m_sb_lock);
1250 		return 0;
1251 	}
1252 	xfs_warn_once(mp,
1253 "Reserve blocks depleted! Consider increasing reserve pool size.");
1254 
1255 fdblocks_enospc:
1256 	spin_unlock(&mp->m_sb_lock);
1257 	return -ENOSPC;
1258 }
1259 
1260 /*
1261  * Used to free the superblock along various error paths.
1262  */
1263 void
1264 xfs_freesb(
1265 	struct xfs_mount	*mp)
1266 {
1267 	struct xfs_buf		*bp = mp->m_sb_bp;
1268 
1269 	xfs_buf_lock(bp);
1270 	mp->m_sb_bp = NULL;
1271 	xfs_buf_relse(bp);
1272 }
1273 
1274 /*
1275  * If the underlying (data/log/rt) device is readonly, there are some
1276  * operations that cannot proceed.
1277  */
1278 int
1279 xfs_dev_is_read_only(
1280 	struct xfs_mount	*mp,
1281 	char			*message)
1282 {
1283 	if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1284 	    xfs_readonly_buftarg(mp->m_logdev_targp) ||
1285 	    (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
1286 		xfs_notice(mp, "%s required on read-only device.", message);
1287 		xfs_notice(mp, "write access unavailable, cannot proceed.");
1288 		return -EROFS;
1289 	}
1290 	return 0;
1291 }
1292 
1293 /* Force the summary counters to be recalculated at next mount. */
1294 void
1295 xfs_force_summary_recalc(
1296 	struct xfs_mount	*mp)
1297 {
1298 	if (!xfs_has_lazysbcount(mp))
1299 		return;
1300 
1301 	xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
1302 }
1303 
1304 /*
1305  * Enable a log incompat feature flag in the primary superblock.  The caller
1306  * cannot have any other transactions in progress.
1307  */
1308 int
1309 xfs_add_incompat_log_feature(
1310 	struct xfs_mount	*mp,
1311 	uint32_t		feature)
1312 {
1313 	struct xfs_dsb		*dsb;
1314 	int			error;
1315 
1316 	ASSERT(hweight32(feature) == 1);
1317 	ASSERT(!(feature & XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
1318 
1319 	/*
1320 	 * Force the log to disk and kick the background AIL thread to reduce
1321 	 * the chances that the bwrite will stall waiting for the AIL to unpin
1322 	 * the primary superblock buffer.  This isn't a data integrity
1323 	 * operation, so we don't need a synchronous push.
1324 	 */
1325 	error = xfs_log_force(mp, XFS_LOG_SYNC);
1326 	if (error)
1327 		return error;
1328 	xfs_ail_push_all(mp->m_ail);
1329 
1330 	/*
1331 	 * Lock the primary superblock buffer to serialize all callers that
1332 	 * are trying to set feature bits.
1333 	 */
1334 	xfs_buf_lock(mp->m_sb_bp);
1335 	xfs_buf_hold(mp->m_sb_bp);
1336 
1337 	if (xfs_is_shutdown(mp)) {
1338 		error = -EIO;
1339 		goto rele;
1340 	}
1341 
1342 	if (xfs_sb_has_incompat_log_feature(&mp->m_sb, feature))
1343 		goto rele;
1344 
1345 	/*
1346 	 * Write the primary superblock to disk immediately, because we need
1347 	 * the log_incompat bit to be set in the primary super now to protect
1348 	 * the log items that we're going to commit later.
1349 	 */
1350 	dsb = mp->m_sb_bp->b_addr;
1351 	xfs_sb_to_disk(dsb, &mp->m_sb);
1352 	dsb->sb_features_log_incompat |= cpu_to_be32(feature);
1353 	error = xfs_bwrite(mp->m_sb_bp);
1354 	if (error)
1355 		goto shutdown;
1356 
1357 	/*
1358 	 * Add the feature bits to the incore superblock before we unlock the
1359 	 * buffer.
1360 	 */
1361 	xfs_sb_add_incompat_log_features(&mp->m_sb, feature);
1362 	xfs_buf_relse(mp->m_sb_bp);
1363 
1364 	/* Log the superblock to disk. */
1365 	return xfs_sync_sb(mp, false);
1366 shutdown:
1367 	xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1368 rele:
1369 	xfs_buf_relse(mp->m_sb_bp);
1370 	return error;
1371 }
1372 
1373 /*
1374  * Clear all the log incompat flags from the superblock.
1375  *
1376  * The caller cannot be in a transaction, must ensure that the log does not
1377  * contain any log items protected by any log incompat bit, and must ensure
1378  * that there are no other threads that depend on the state of the log incompat
1379  * feature flags in the primary super.
1380  *
1381  * Returns true if the superblock is dirty.
1382  */
1383 bool
1384 xfs_clear_incompat_log_features(
1385 	struct xfs_mount	*mp)
1386 {
1387 	bool			ret = false;
1388 
1389 	if (!xfs_has_crc(mp) ||
1390 	    !xfs_sb_has_incompat_log_feature(&mp->m_sb,
1391 				XFS_SB_FEAT_INCOMPAT_LOG_ALL) ||
1392 	    xfs_is_shutdown(mp) ||
1393 	    !xfs_is_done_with_log_incompat(mp))
1394 		return false;
1395 
1396 	/*
1397 	 * Update the incore superblock.  We synchronize on the primary super
1398 	 * buffer lock to be consistent with the add function, though at least
1399 	 * in theory this shouldn't be necessary.
1400 	 */
1401 	xfs_buf_lock(mp->m_sb_bp);
1402 	xfs_buf_hold(mp->m_sb_bp);
1403 
1404 	if (xfs_sb_has_incompat_log_feature(&mp->m_sb,
1405 				XFS_SB_FEAT_INCOMPAT_LOG_ALL)) {
1406 		xfs_sb_remove_incompat_log_features(&mp->m_sb);
1407 		ret = true;
1408 	}
1409 
1410 	xfs_buf_relse(mp->m_sb_bp);
1411 	return ret;
1412 }
1413 
1414 /*
1415  * Update the in-core delayed block counter.
1416  *
1417  * We prefer to update the counter without having to take a spinlock for every
1418  * counter update (i.e. batching).  Each change to delayed allocation
1419  * reservations can change can easily exceed the default percpu counter
1420  * batching, so we use a larger batch factor here.
1421  *
1422  * Note that we don't currently have any callers requiring fast summation
1423  * (e.g. percpu_counter_read) so we can use a big batch value here.
1424  */
1425 #define XFS_DELALLOC_BATCH	(4096)
1426 void
1427 xfs_mod_delalloc(
1428 	struct xfs_inode	*ip,
1429 	int64_t			data_delta,
1430 	int64_t			ind_delta)
1431 {
1432 	struct xfs_mount	*mp = ip->i_mount;
1433 
1434 	if (XFS_IS_REALTIME_INODE(ip)) {
1435 		percpu_counter_add_batch(&mp->m_delalloc_rtextents,
1436 				xfs_rtb_to_rtx(mp, data_delta),
1437 				XFS_DELALLOC_BATCH);
1438 		if (!ind_delta)
1439 			return;
1440 		data_delta = 0;
1441 	}
1442 	percpu_counter_add_batch(&mp->m_delalloc_blks, data_delta + ind_delta,
1443 			XFS_DELALLOC_BATCH);
1444 }
1445