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