blob: 5f3ba88985d5e3ce666629be4543fdc7f352895c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Dave Chinner9a2cc412014-12-04 09:43:17 +110029#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110031#include "xfs_dir2.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_alloc.h"
34#include "xfs_rtalloc.h"
35#include "xfs_bmap.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110036#include "xfs_trans.h"
37#include "xfs_trans_priv.h"
38#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_quota.h"
41#include "xfs_fsops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Dave Chinner6d8b79c2012-10-08 21:56:09 +110043#include "xfs_icache.h"
Brian Fostera31b1d32014-07-15 08:07:01 +100044#include "xfs_sysfs.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Christoph Hellwig27174202009-03-30 10:21:31 +020047static DEFINE_MUTEX(xfs_uuid_table_mutex);
48static int xfs_uuid_table_size;
49static uuid_t *xfs_uuid_table;
50
Darrick J. Wongaf3b6382015-11-03 13:06:34 +110051void
52xfs_uuid_table_free(void)
53{
54 if (xfs_uuid_table_size == 0)
55 return;
56 kmem_free(xfs_uuid_table);
57 xfs_uuid_table = NULL;
58 xfs_uuid_table_size = 0;
59}
60
Christoph Hellwig27174202009-03-30 10:21:31 +020061/*
62 * See if the UUID is unique among mounted XFS filesystems.
63 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
64 */
65STATIC int
66xfs_uuid_mount(
67 struct xfs_mount *mp)
68{
69 uuid_t *uuid = &mp->m_sb.sb_uuid;
70 int hole, i;
71
72 if (mp->m_flags & XFS_MOUNT_NOUUID)
73 return 0;
74
75 if (uuid_is_nil(uuid)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +110076 xfs_warn(mp, "Filesystem has nil UUID - can't mount");
Dave Chinner24513372014-06-25 14:58:08 +100077 return -EINVAL;
Christoph Hellwig27174202009-03-30 10:21:31 +020078 }
79
80 mutex_lock(&xfs_uuid_table_mutex);
81 for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
82 if (uuid_is_nil(&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 = kmem_realloc(xfs_uuid_table,
92 (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
Christoph Hellwig27174202009-03-30 10:21:31 +020093 KM_SLEEP);
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);
Mitsuo Hayasaka021000e2012-01-13 05:58:39 +0000103 xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
Dave Chinner24513372014-06-25 14:58:08 +1000104 return -EINVAL;
Christoph Hellwig27174202009-03-30 10:21:31 +0200105}
106
107STATIC void
108xfs_uuid_unmount(
109 struct xfs_mount *mp)
110{
111 uuid_t *uuid = &mp->m_sb.sb_uuid;
112 int i;
113
114 if (mp->m_flags & XFS_MOUNT_NOUUID)
115 return;
116
117 mutex_lock(&xfs_uuid_table_mutex);
118 for (i = 0; i < xfs_uuid_table_size; i++) {
119 if (uuid_is_nil(&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
Dave Chinnere1765792010-09-22 10:47:20 +1000131STATIC void
132__xfs_free_perag(
133 struct rcu_head *head)
134{
135 struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
136
137 ASSERT(atomic_read(&pag->pag_ref) == 0);
138 kmem_free(pag);
139}
140
Dave Chinner0fa800f2010-01-11 11:47:46 +0000141/*
Dave Chinnere1765792010-09-22 10:47:20 +1000142 * Free up the per-ag resources associated with the mount structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
Christoph Hellwigc962fb72008-05-20 15:10:52 +1000144STATIC void
Christoph Hellwigff4f0382008-08-13 16:50:47 +1000145xfs_free_perag(
Christoph Hellwig745f6912007-08-30 17:20:39 +1000146 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000148 xfs_agnumber_t agno;
149 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000151 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
152 spin_lock(&mp->m_perag_lock);
153 pag = radix_tree_delete(&mp->m_perag_tree, agno);
154 spin_unlock(&mp->m_perag_lock);
Dave Chinnere1765792010-09-22 10:47:20 +1000155 ASSERT(pag);
Dave Chinnerf83282a2010-11-08 08:55:04 +0000156 ASSERT(atomic_read(&pag->pag_ref) == 0);
Dave Chinnere1765792010-09-22 10:47:20 +1000157 call_rcu(&pag->rcu_head, __xfs_free_perag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Nathan Scott4cc929e2007-05-14 18:24:02 +1000161/*
162 * Check size of device based on the (data/realtime) block count.
163 * Note: this check is used by the growfs code as well as mount.
164 */
165int
166xfs_sb_validate_fsb_count(
167 xfs_sb_t *sbp,
168 __uint64_t nblocks)
169{
170 ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
171 ASSERT(sbp->sb_blocklog >= BBSHIFT);
172
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000173 /* Limited by ULONG_MAX of page cache index */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300174 if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000175 return -EFBIG;
Nathan Scott4cc929e2007-05-14 18:24:02 +1000176 return 0;
177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000179int
Nathan Scottc11e2c32005-11-02 15:11:45 +1100180xfs_initialize_perag(
Nathan Scottc11e2c32005-11-02 15:11:45 +1100181 xfs_mount_t *mp,
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000182 xfs_agnumber_t agcount,
183 xfs_agnumber_t *maxagi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300185 xfs_agnumber_t index;
Dave Chinner8b26c582010-01-11 11:47:48 +0000186 xfs_agnumber_t first_initialised = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 xfs_perag_t *pag;
Dave Chinner8b26c582010-01-11 11:47:48 +0000188 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000190 /*
191 * Walk the current per-ag tree so we don't try to initialise AGs
192 * that already exist (growfs case). Allocate and insert all the
193 * AGs we don't find ready for initialisation.
194 */
195 for (index = 0; index < agcount; index++) {
196 pag = xfs_perag_get(mp, index);
197 if (pag) {
198 xfs_perag_put(pag);
199 continue;
200 }
Dave Chinner8b26c582010-01-11 11:47:48 +0000201 if (!first_initialised)
202 first_initialised = index;
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000203
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000204 pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
205 if (!pag)
Dave Chinner8b26c582010-01-11 11:47:48 +0000206 goto out_unwind;
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000207 pag->pag_agno = index;
208 pag->pag_mount = mp;
Dave Chinner1a427ab2010-12-16 17:08:41 +1100209 spin_lock_init(&pag->pag_ici_lock);
Dave Chinner69b491c2010-09-27 11:09:51 +1000210 mutex_init(&pag->pag_ici_reclaim_lock);
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000211 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
Dave Chinner74f75a02010-09-24 19:59:04 +1000212 spin_lock_init(&pag->pag_buf_lock);
213 pag->pag_buf_tree = RB_ROOT;
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000214
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000215 if (radix_tree_preload(GFP_NOFS))
Dave Chinner8b26c582010-01-11 11:47:48 +0000216 goto out_unwind;
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000217
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000218 spin_lock(&mp->m_perag_lock);
219 if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
220 BUG();
221 spin_unlock(&mp->m_perag_lock);
Dave Chinner8b26c582010-01-11 11:47:48 +0000222 radix_tree_preload_end();
223 error = -EEXIST;
224 goto out_unwind;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000225 }
226 spin_unlock(&mp->m_perag_lock);
227 radix_tree_preload_end();
228 }
229
Eric Sandeen12c3f052016-03-02 09:58:09 +1100230 index = xfs_set_inode_alloc(mp, agcount);
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000231
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000232 if (maxagi)
233 *maxagi = index;
234 return 0;
Dave Chinner8b26c582010-01-11 11:47:48 +0000235
236out_unwind:
237 kmem_free(pag);
238 for (; index > first_initialised; index--) {
239 pag = radix_tree_delete(&mp->m_perag_tree, index);
240 kmem_free(pag);
241 }
242 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
246 * xfs_readsb
247 *
248 * Does the initial read of the superblock.
249 */
250int
Dave Chinnerff550682013-08-12 20:49:41 +1000251xfs_readsb(
252 struct xfs_mount *mp,
253 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 unsigned int sector_size;
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100256 struct xfs_buf *bp;
257 struct xfs_sb *sbp = &mp->m_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int error;
Dave Chinneraf34e092011-03-07 10:04:35 +1100259 int loud = !(flags & XFS_MFSI_QUIET);
Eric Sandeendaba5422014-02-19 15:39:16 +1100260 const struct xfs_buf_ops *buf_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 ASSERT(mp->m_sb_bp == NULL);
263 ASSERT(mp->m_ddev_targp != NULL);
264
265 /*
Eric Sandeendaba5422014-02-19 15:39:16 +1100266 * For the initial read, we must guess at the sector
267 * size based on the block device. It's enough to
268 * get the sb_sectsize out of the superblock and
269 * then reread with the proper length.
270 * We don't verify it yet, because it may not be complete.
271 */
272 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
273 buf_ops = NULL;
274
275 /*
Brian Fosterc891c302016-07-20 11:13:43 +1000276 * Allocate a (locked) buffer to hold the superblock. This will be kept
277 * around at all times to optimize access to the superblock. Therefore,
278 * set XBF_NO_IOACCT to make sure it doesn't hold the buftarg count
279 * elevated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Dave Chinner26af6552010-09-22 10:47:20 +1000281reread:
Dave Chinnerba372672014-10-02 09:05:32 +1000282 error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
Brian Fosterc891c302016-07-20 11:13:43 +1000283 BTOBB(sector_size), XBF_NO_IOACCT, &bp,
284 buf_ops);
Dave Chinnerba372672014-10-02 09:05:32 +1000285 if (error) {
Dave Chinnereab4e632012-11-12 22:54:02 +1100286 if (loud)
Dave Chinnere721f502013-04-03 16:11:32 +1100287 xfs_warn(mp, "SB validate failed with error %d.", error);
Dave Chinnerac75a1f2014-03-07 16:19:14 +1100288 /* bad CRC means corrupted metadata */
Dave Chinner24513372014-06-25 14:58:08 +1000289 if (error == -EFSBADCRC)
290 error = -EFSCORRUPTED;
Dave Chinnerba372672014-10-02 09:05:32 +1000291 return error;
Dave Chinnereab4e632012-11-12 22:54:02 +1100292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 /*
295 * Initialize the mount structure from the superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
Dave Chinner556b8882014-06-06 16:00:43 +1000297 xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
Dave Chinner556b8882014-06-06 16:00:43 +1000298
299 /*
300 * If we haven't validated the superblock, do so now before we try
301 * to check the sector size and reread the superblock appropriately.
302 */
303 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
304 if (loud)
305 xfs_warn(mp, "Invalid superblock magic number");
Dave Chinner24513372014-06-25 14:58:08 +1000306 error = -EINVAL;
Dave Chinner556b8882014-06-06 16:00:43 +1000307 goto release_buf;
308 }
Dave Chinnerff550682013-08-12 20:49:41 +1000309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /*
311 * We must be able to do sector-sized and sector-aligned IO.
312 */
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100313 if (sector_size > sbp->sb_sectsize) {
Dave Chinneraf34e092011-03-07 10:04:35 +1100314 if (loud)
315 xfs_warn(mp, "device supports %u byte sectors (not %u)",
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100316 sector_size, sbp->sb_sectsize);
Dave Chinner24513372014-06-25 14:58:08 +1000317 error = -ENOSYS;
Dave Chinner26af6552010-09-22 10:47:20 +1000318 goto release_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Eric Sandeendaba5422014-02-19 15:39:16 +1100321 if (buf_ops == NULL) {
Dave Chinner556b8882014-06-06 16:00:43 +1000322 /*
323 * Re-read the superblock so the buffer is correctly sized,
324 * and properly verified.
325 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 xfs_buf_relse(bp);
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100327 sector_size = sbp->sb_sectsize;
Eric Sandeendaba5422014-02-19 15:39:16 +1100328 buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops;
Dave Chinner26af6552010-09-22 10:47:20 +1000329 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
Dave Chinner5681ca42015-02-23 21:22:31 +1100332 xfs_reinit_percpu_counters(mp);
David Chinner8d280b92006-03-14 13:13:09 +1100333
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100334 /* no need to be quiet anymore, so reset the buf ops */
335 bp->b_ops = &xfs_sb_buf_ops;
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 mp->m_sb_bp = bp;
Dave Chinner26af6552010-09-22 10:47:20 +1000338 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 0;
340
Dave Chinner26af6552010-09-22 10:47:20 +1000341release_buf:
342 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return error;
344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000347 * Update alignment values based on mount options and sb values
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
Eric Sandeen0771fb42007-10-12 11:03:40 +1000349STATIC int
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100350xfs_update_alignment(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 xfs_sb_t *sbp = &(mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Christoph Hellwig42490232008-08-13 16:49:32 +1000354 if (mp->m_dalign) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /*
356 * If stripe unit and stripe width are not multiples
357 * of the fs blocksize turn off alignment.
358 */
359 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
360 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
Jie Liu39a45d82013-05-02 19:27:47 +0800361 xfs_warn(mp,
362 "alignment check failed: sunit/swidth vs. blocksize(%d)",
363 sbp->sb_blocksize);
Dave Chinner24513372014-06-25 14:58:08 +1000364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 } else {
366 /*
367 * Convert the stripe unit and width to FSBs.
368 */
369 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
370 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
Dave Chinner53487782011-03-07 10:05:35 +1100371 xfs_warn(mp,
Jie Liu39a45d82013-05-02 19:27:47 +0800372 "alignment check failed: sunit/swidth vs. agsize(%d)",
373 sbp->sb_agblocks);
Dave Chinner24513372014-06-25 14:58:08 +1000374 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 } else if (mp->m_dalign) {
376 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
377 } else {
Jie Liu39a45d82013-05-02 19:27:47 +0800378 xfs_warn(mp,
379 "alignment check failed: sunit(%d) less than bsize(%d)",
380 mp->m_dalign, sbp->sb_blocksize);
Dave Chinner24513372014-06-25 14:58:08 +1000381 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383 }
384
385 /*
386 * Update superblock with new values
387 * and log changes
388 */
Eric Sandeen62118702008-03-06 13:44:28 +1100389 if (xfs_sb_version_hasdalign(sbp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (sbp->sb_unit != mp->m_dalign) {
391 sbp->sb_unit = mp->m_dalign;
Dave Chinner61e63ec2015-01-22 09:10:31 +1100392 mp->m_update_sb = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 if (sbp->sb_width != mp->m_swidth) {
395 sbp->sb_width = mp->m_swidth;
Dave Chinner61e63ec2015-01-22 09:10:31 +1100396 mp->m_update_sb = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Jie Liu34d7f602013-05-02 19:27:53 +0800398 } else {
399 xfs_warn(mp,
400 "cannot change alignment: superblock does not support data alignment");
Dave Chinner24513372014-06-25 14:58:08 +1000401 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
Eric Sandeen62118702008-03-06 13:44:28 +1100404 xfs_sb_version_hasdalign(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 mp->m_dalign = sbp->sb_unit;
406 mp->m_swidth = sbp->sb_width;
407 }
408
Eric Sandeen0771fb42007-10-12 11:03:40 +1000409 return 0;
410}
411
412/*
413 * Set the maximum inode count for this filesystem
414 */
415STATIC void
416xfs_set_maxicount(xfs_mount_t *mp)
417{
418 xfs_sb_t *sbp = &(mp->m_sb);
419 __uint64_t icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (sbp->sb_imax_pct) {
Eric Sandeen0771fb42007-10-12 11:03:40 +1000422 /*
423 * Make sure the maximum inode count is a multiple
424 * of the units we allocate inodes in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
427 do_div(icount, 100);
428 do_div(icount, mp->m_ialloc_blks);
429 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
430 sbp->sb_inopblog;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000431 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 mp->m_maxicount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Eric Sandeen0771fb42007-10-12 11:03:40 +1000434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Eric Sandeen0771fb42007-10-12 11:03:40 +1000436/*
437 * Set the default minimum read and write sizes unless
438 * already specified in a mount option.
439 * We use smaller I/O sizes when the file system
440 * is being used for NFS service (wsync mount option).
441 */
442STATIC void
443xfs_set_rw_sizes(xfs_mount_t *mp)
444{
445 xfs_sb_t *sbp = &(mp->m_sb);
446 int readio_log, writeio_log;
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
449 if (mp->m_flags & XFS_MOUNT_WSYNC) {
450 readio_log = XFS_WSYNC_READIO_LOG;
451 writeio_log = XFS_WSYNC_WRITEIO_LOG;
452 } else {
453 readio_log = XFS_READIO_LOG_LARGE;
454 writeio_log = XFS_WRITEIO_LOG_LARGE;
455 }
456 } else {
457 readio_log = mp->m_readio_log;
458 writeio_log = mp->m_writeio_log;
459 }
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (sbp->sb_blocklog > readio_log) {
462 mp->m_readio_log = sbp->sb_blocklog;
463 } else {
464 mp->m_readio_log = readio_log;
465 }
466 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
467 if (sbp->sb_blocklog > writeio_log) {
468 mp->m_writeio_log = sbp->sb_blocklog;
469 } else {
470 mp->m_writeio_log = writeio_log;
471 }
472 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000473}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Eric Sandeen0771fb42007-10-12 11:03:40 +1000475/*
Dave Chinner055388a2011-01-04 11:35:03 +1100476 * precalculate the low space thresholds for dynamic speculative preallocation.
477 */
478void
479xfs_set_low_space_thresholds(
480 struct xfs_mount *mp)
481{
482 int i;
483
484 for (i = 0; i < XFS_LOWSP_MAX; i++) {
485 __uint64_t space = mp->m_sb.sb_dblocks;
486
487 do_div(space, 100);
488 mp->m_low_space[i] = space * (i + 1);
489 }
490}
491
492
493/*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000494 * Set whether we're using inode alignment.
495 */
496STATIC void
497xfs_set_inoalignment(xfs_mount_t *mp)
498{
Eric Sandeen62118702008-03-06 13:44:28 +1100499 if (xfs_sb_version_hasalign(&mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 mp->m_sb.sb_inoalignmt >=
501 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
502 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
503 else
504 mp->m_inoalign_mask = 0;
505 /*
506 * If we are using stripe alignment, check whether
507 * the stripe unit is a multiple of the inode alignment
508 */
509 if (mp->m_dalign && mp->m_inoalign_mask &&
510 !(mp->m_dalign & mp->m_inoalign_mask))
511 mp->m_sinoalign = mp->m_dalign;
512 else
513 mp->m_sinoalign = 0;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000514}
515
516/*
Zhi Yong Wu0471f622013-08-07 10:10:58 +0000517 * Check that the data (and log if separate) is an ok size.
Eric Sandeen0771fb42007-10-12 11:03:40 +1000518 */
519STATIC int
Dave Chinnerba372672014-10-02 09:05:32 +1000520xfs_check_sizes(
521 struct xfs_mount *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +1000522{
Dave Chinnerba372672014-10-02 09:05:32 +1000523 struct xfs_buf *bp;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000524 xfs_daddr_t d;
Dave Chinnerba372672014-10-02 09:05:32 +1000525 int error;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
528 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100529 xfs_warn(mp, "filesystem size mismatch detected");
Dave Chinner24513372014-06-25 14:58:08 +1000530 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Dave Chinnerba372672014-10-02 09:05:32 +1000532 error = xfs_buf_read_uncached(mp->m_ddev_targp,
Dave Chinner1922c942010-09-22 10:47:20 +1000533 d - XFS_FSS_TO_BB(mp, 1),
Dave Chinnerba372672014-10-02 09:05:32 +1000534 XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
535 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100536 xfs_warn(mp, "last sector read failed");
Dave Chinnerba372672014-10-02 09:05:32 +1000537 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Dave Chinner1922c942010-09-22 10:47:20 +1000539 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Dave Chinnerba372672014-10-02 09:05:32 +1000541 if (mp->m_logdev_targp == mp->m_ddev_targp)
542 return 0;
543
544 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
545 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
546 xfs_warn(mp, "log size mismatch detected");
547 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Dave Chinnerba372672014-10-02 09:05:32 +1000549 error = xfs_buf_read_uncached(mp->m_logdev_targp,
550 d - XFS_FSB_TO_BB(mp, 1),
551 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
552 if (error) {
553 xfs_warn(mp, "log device read failed");
554 return error;
555 }
556 xfs_buf_relse(bp);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000557 return 0;
558}
559
560/*
Christoph Hellwig7d095252009-06-08 15:33:32 +0200561 * Clear the quotaflags in memory and in the superblock.
562 */
563int
564xfs_mount_reset_sbqflags(
565 struct xfs_mount *mp)
566{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200567 mp->m_qflags = 0;
568
Dave Chinner61e63ec2015-01-22 09:10:31 +1100569 /* It is OK to look at sb_qflags in the mount path without m_sb_lock. */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200570 if (mp->m_sb.sb_qflags == 0)
571 return 0;
572 spin_lock(&mp->m_sb_lock);
573 mp->m_sb.sb_qflags = 0;
574 spin_unlock(&mp->m_sb_lock);
575
Dave Chinner61e63ec2015-01-22 09:10:31 +1100576 if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
Christoph Hellwig7d095252009-06-08 15:33:32 +0200577 return 0;
578
Dave Chinner61e63ec2015-01-22 09:10:31 +1100579 return xfs_sync_sb(mp, false);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200580}
581
Eric Sandeend5db0f92010-02-05 22:59:53 +0000582__uint64_t
583xfs_default_resblks(xfs_mount_t *mp)
584{
585 __uint64_t resblks;
586
587 /*
Dave Chinner8babd8a2010-03-04 01:46:25 +0000588 * We default to 5% or 8192 fsbs of space reserved, whichever is
589 * smaller. This is intended to cover concurrent allocation
590 * transactions when we initially hit enospc. These each require a 4
591 * block reservation. Hence by default we cover roughly 2000 concurrent
592 * allocation reservations.
Eric Sandeend5db0f92010-02-05 22:59:53 +0000593 */
594 resblks = mp->m_sb.sb_dblocks;
595 do_div(resblks, 20);
Dave Chinner8babd8a2010-03-04 01:46:25 +0000596 resblks = min_t(__uint64_t, resblks, 8192);
Eric Sandeend5db0f92010-02-05 22:59:53 +0000597 return resblks;
598}
599
Christoph Hellwig7d095252009-06-08 15:33:32 +0200600/*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000601 * This function does the following on an initial mount of a file system:
602 * - reads the superblock from disk and init the mount struct
603 * - if we're a 32-bit kernel, do a size check on the superblock
604 * so we don't mount terabyte filesystems
605 * - init mount struct realtime fields
606 * - allocate inode hash table for fs
607 * - init directory manager
608 * - perform recovery and init the log manager
609 */
610int
611xfs_mountfs(
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000612 struct xfs_mount *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +1000613{
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000614 struct xfs_sb *sbp = &(mp->m_sb);
615 struct xfs_inode *rip;
616 __uint64_t resblks;
617 uint quotamount = 0;
618 uint quotaflags = 0;
619 int error = 0;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000620
Dave Chinnerff550682013-08-12 20:49:41 +1000621 xfs_sb_mount_common(mp, sbp);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000622
623 /*
Dave Chinner074e4272015-01-22 09:10:33 +1100624 * Check for a mismatched features2 values. Older kernels read & wrote
625 * into the wrong sb offset for sb_features2 on some platforms due to
626 * xfs_sb_t not being 64bit size aligned when sb_features2 was added,
627 * which made older superblock reading/writing routines swap it as a
628 * 64-bit value.
David Chinneree1c0902008-03-06 13:45:50 +1100629 *
Eric Sandeene6957ea2008-04-10 12:19:34 +1000630 * For backwards compatibility, we make both slots equal.
631 *
Dave Chinner074e4272015-01-22 09:10:33 +1100632 * If we detect a mismatched field, we OR the set bits into the existing
633 * features2 field in case it has already been modified; we don't want
634 * to lose any features. We then update the bad location with the ORed
635 * value so that older kernels will see any features2 flags. The
636 * superblock writeback code ensures the new sb_features2 is copied to
637 * sb_bad_features2 before it is logged or written to disk.
David Chinneree1c0902008-03-06 13:45:50 +1100638 */
Eric Sandeene6957ea2008-04-10 12:19:34 +1000639 if (xfs_sb_has_mismatched_features2(sbp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100640 xfs_warn(mp, "correcting sb_features alignment problem");
David Chinneree1c0902008-03-06 13:45:50 +1100641 sbp->sb_features2 |= sbp->sb_bad_features2;
Dave Chinner61e63ec2015-01-22 09:10:31 +1100642 mp->m_update_sb = true;
Eric Sandeene6957ea2008-04-10 12:19:34 +1000643
644 /*
645 * Re-check for ATTR2 in case it was found in bad_features2
646 * slot.
647 */
Tim Shimmin7c12f292008-04-30 18:15:28 +1000648 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
649 !(mp->m_flags & XFS_MOUNT_NOATTR2))
Eric Sandeene6957ea2008-04-10 12:19:34 +1000650 mp->m_flags |= XFS_MOUNT_ATTR2;
Tim Shimmin7c12f292008-04-30 18:15:28 +1000651 }
Eric Sandeene6957ea2008-04-10 12:19:34 +1000652
Tim Shimmin7c12f292008-04-30 18:15:28 +1000653 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
654 (mp->m_flags & XFS_MOUNT_NOATTR2)) {
655 xfs_sb_version_removeattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +1100656 mp->m_update_sb = true;
Tim Shimmin7c12f292008-04-30 18:15:28 +1000657
658 /* update sb_versionnum for the clearing of the morebits */
659 if (!sbp->sb_features2)
Dave Chinner61e63ec2015-01-22 09:10:31 +1100660 mp->m_update_sb = true;
David Chinneree1c0902008-03-06 13:45:50 +1100661 }
662
Dave Chinner263997a2014-05-20 07:46:40 +1000663 /* always use v2 inodes by default now */
664 if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
665 mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
Dave Chinner61e63ec2015-01-22 09:10:31 +1100666 mp->m_update_sb = true;
Dave Chinner263997a2014-05-20 07:46:40 +1000667 }
668
David Chinneree1c0902008-03-06 13:45:50 +1100669 /*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000670 * Check if sb_agblocks is aligned at stripe boundary
671 * If sb_agblocks is NOT aligned turn off m_dalign since
672 * allocator alignment is within an ag, therefore ag has
673 * to be aligned at stripe boundary.
674 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100675 error = xfs_update_alignment(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000676 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100677 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000678
679 xfs_alloc_compute_maxlevels(mp);
680 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
681 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
682 xfs_ialloc_compute_maxlevels(mp);
683
684 xfs_set_maxicount(mp);
685
Carlos Maiolinoe6b3bb72016-05-18 11:11:27 +1000686 /* enable fail_at_unmount as default */
687 mp->m_fail_unmount = 1;
688
Brian Fostera31b1d32014-07-15 08:07:01 +1000689 error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype, NULL, mp->m_fsname);
Christoph Hellwig27174202009-03-30 10:21:31 +0200690 if (error)
691 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000692
Bill O'Donnell225e4632015-10-12 18:21:19 +1100693 error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype,
694 &mp->m_kobj, "stats");
Brian Fostera31b1d32014-07-15 08:07:01 +1000695 if (error)
696 goto out_remove_sysfs;
697
Carlos Maiolino192852b2016-05-18 10:58:51 +1000698 error = xfs_error_sysfs_init(mp);
Bill O'Donnell225e4632015-10-12 18:21:19 +1100699 if (error)
700 goto out_del_stats;
701
Carlos Maiolino192852b2016-05-18 10:58:51 +1000702
703 error = xfs_uuid_mount(mp);
704 if (error)
705 goto out_remove_error_sysfs;
706
Eric Sandeen0771fb42007-10-12 11:03:40 +1000707 /*
708 * Set the minimum read and write sizes
709 */
710 xfs_set_rw_sizes(mp);
711
Dave Chinner055388a2011-01-04 11:35:03 +1100712 /* set the low space thresholds for dynamic preallocation */
713 xfs_set_low_space_thresholds(mp);
714
Eric Sandeen0771fb42007-10-12 11:03:40 +1000715 /*
716 * Set the inode cluster size.
717 * This may still be overridden by the file system
718 * block size if it is larger than the chosen cluster size.
Dave Chinner8f805872013-11-01 15:27:20 +1100719 *
720 * For v5 filesystems, scale the cluster size with the inode size to
721 * keep a constant ratio of inode per cluster buffer, but only if mkfs
722 * has set the inode alignment value appropriately for larger cluster
723 * sizes.
Eric Sandeen0771fb42007-10-12 11:03:40 +1000724 */
725 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
Dave Chinner8f805872013-11-01 15:27:20 +1100726 if (xfs_sb_version_hascrc(&mp->m_sb)) {
727 int new_size = mp->m_inode_cluster_size;
728
729 new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
730 if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
731 mp->m_inode_cluster_size = new_size;
Dave Chinner8f805872013-11-01 15:27:20 +1100732 }
Eric Sandeen0771fb42007-10-12 11:03:40 +1000733
734 /*
Brian Fostere5376fc2015-05-29 08:57:27 +1000735 * If enabled, sparse inode chunk alignment is expected to match the
736 * cluster size. Full inode chunk alignment must match the chunk size,
737 * but that is checked on sb read verification...
738 */
739 if (xfs_sb_version_hassparseinodes(&mp->m_sb) &&
740 mp->m_sb.sb_spino_align !=
741 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) {
742 xfs_warn(mp,
743 "Sparse inode block alignment (%u) must match cluster size (%llu).",
744 mp->m_sb.sb_spino_align,
745 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size));
746 error = -EINVAL;
747 goto out_remove_uuid;
748 }
749
750 /*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000751 * Set inode alignment fields
752 */
753 xfs_set_inoalignment(mp);
754
755 /*
Zhi Yong Wuc2bfbc92013-08-12 03:15:03 +0000756 * Check that the data (and log if separate) is an ok size.
Eric Sandeen0771fb42007-10-12 11:03:40 +1000757 */
Christoph Hellwig42490232008-08-13 16:49:32 +1000758 error = xfs_check_sizes(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000759 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100760 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /*
763 * Initialize realtime fields in the mount structure
764 */
Eric Sandeen0771fb42007-10-12 11:03:40 +1000765 error = xfs_rtmount_init(mp);
766 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100767 xfs_warn(mp, "RT mount failed");
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100768 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
771 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 * Copies the low order bits of the timestamp and the randomly
773 * set "sequence" number out of a UUID.
774 */
775 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 mp->m_dmevmask = 0; /* not persistent; set after each mount */
778
Dave Chinner0650b552014-06-06 15:01:58 +1000779 error = xfs_da_mount(mp);
780 if (error) {
781 xfs_warn(mp, "Failed dir/attr init: %d", error);
782 goto out_remove_uuid;
783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /*
786 * Initialize the precomputed transaction reservations values.
787 */
788 xfs_trans_init(mp);
789
790 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * Allocate and initialize the per-ag data.
792 */
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000793 spin_lock_init(&mp->m_perag_lock);
Dave Chinner9b98b6f2010-05-27 01:58:13 +0000794 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000795 error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
796 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100797 xfs_warn(mp, "Failed per-ag init: %d", error);
Dave Chinner0650b552014-06-06 15:01:58 +1000798 goto out_free_dir;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100801 if (!sbp->sb_logblocks) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100802 xfs_warn(mp, "no log defined");
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100803 XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000804 error = -EFSCORRUPTED;
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100805 goto out_free_perag;
806 }
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /*
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000809 * Log's mount-time initialization. The first part of recovery can place
810 * some items on the AIL, to be handled when recovery is finished or
811 * cancelled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100813 error = xfs_log_mount(mp, mp->m_logdev_targp,
814 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
815 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
816 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100817 xfs_warn(mp, "log mount failed");
Dave Chinnerd4f35122012-04-23 15:59:06 +1000818 goto out_fail_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
821 /*
David Chinner92821e22007-05-24 15:26:31 +1000822 * Now the log is mounted, we know if it was an unclean shutdown or
823 * not. If it was, with the first phase of recovery has completed, we
824 * have consistent AG blocks on disk. We have not recovered EFIs yet,
825 * but they are recovered transactionally in the second recovery phase
826 * later.
827 *
828 * Hence we can safely re-initialise incore superblock counters from
829 * the per-ag data. These may not be correct if the filesystem was not
830 * cleanly unmounted, so we need to wait for recovery to finish before
831 * doing this.
832 *
833 * If the filesystem was cleanly unmounted, then we can trust the
834 * values in the superblock to be correct and we don't need to do
835 * anything here.
836 *
837 * If we are currently making the filesystem, the initialisation will
838 * fail as the perag data is in an undefined state.
839 */
David Chinner92821e22007-05-24 15:26:31 +1000840 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
841 !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
842 !mp->m_sb.sb_inprogress) {
843 error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100844 if (error)
kbuild test robot6eee8972014-08-04 13:49:40 +1000845 goto out_log_dealloc;
David Chinner92821e22007-05-24 15:26:31 +1000846 }
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100847
David Chinner92821e22007-05-24 15:26:31 +1000848 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * Get and sanity-check the root inode.
850 * Save the pointer to it in the mount structure.
851 */
Dave Chinner7b6259e2010-06-24 11:35:17 +1000852 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100854 xfs_warn(mp, "failed to read root inode");
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100855 goto out_log_dealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
858 ASSERT(rip != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100860 if (unlikely(!S_ISDIR(VFS_I(rip)->i_mode))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100861 xfs_warn(mp, "corrupted root inode %llu: not a directory",
Nathan Scottb6574522006-06-09 15:29:40 +1000862 (unsigned long long)rip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 xfs_iunlock(rip, XFS_ILOCK_EXCL);
864 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
865 mp);
Dave Chinner24513372014-06-25 14:58:08 +1000866 error = -EFSCORRUPTED;
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100867 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 mp->m_rootip = rip; /* save it */
870
871 xfs_iunlock(rip, XFS_ILOCK_EXCL);
872
873 /*
874 * Initialize realtime inode pointers in the mount structure
875 */
Eric Sandeen0771fb42007-10-12 11:03:40 +1000876 error = xfs_rtmount_inodes(mp);
877 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /*
879 * Free up the root inode.
880 */
Dave Chinner0b932cc2011-03-07 10:08:35 +1100881 xfs_warn(mp, "failed to read RT inodes");
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100882 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
885 /*
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100886 * If this is a read-only mount defer the superblock updates until
887 * the next remount into writeable mode. Otherwise we would never
888 * perform the update e.g. for the root filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
Dave Chinner61e63ec2015-01-22 09:10:31 +1100890 if (mp->m_update_sb && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
891 error = xfs_sync_sb(mp, false);
David Chinnere5720ee2008-04-10 12:21:18 +1000892 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100893 xfs_warn(mp, "failed to write sb changes");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +0100894 goto out_rtunmount;
David Chinnere5720ee2008-04-10 12:21:18 +1000895 }
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 /*
899 * Initialise the XFS quota management subsystem for this mount
900 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200901 if (XFS_IS_QUOTA_RUNNING(mp)) {
902 error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
903 if (error)
904 goto out_rtunmount;
905 } else {
906 ASSERT(!XFS_IS_QUOTA_ON(mp));
907
908 /*
909 * If a file system had quotas running earlier, but decided to
910 * mount without -o uquota/pquota/gquota options, revoke the
911 * quotachecked license.
912 */
913 if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100914 xfs_notice(mp, "resetting quota flags");
Christoph Hellwig7d095252009-06-08 15:33:32 +0200915 error = xfs_mount_reset_sbqflags(mp);
916 if (error)
Brian Fostera70a4fa2014-07-15 07:41:25 +1000917 goto out_rtunmount;
Christoph Hellwig7d095252009-06-08 15:33:32 +0200918 }
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 /*
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000922 * Finish recovering the file system. This part needed to be delayed
923 * until after the root and real-time bitmap inodes were consistently
924 * read in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
Christoph Hellwig42490232008-08-13 16:49:32 +1000926 error = xfs_log_mount_finish(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100928 xfs_warn(mp, "log mount finish failed");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +0100929 goto out_rtunmount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
932 /*
933 * Complete the quota initialisation, post-log-replay component.
934 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200935 if (quotamount) {
936 ASSERT(mp->m_qflags == 0);
937 mp->m_qflags = quotaflags;
938
939 xfs_qm_mount_quotas(mp);
940 }
941
David Chinner84e1e992007-06-18 16:50:27 +1000942 /*
943 * Now we are mounted, reserve a small amount of unused space for
944 * privileged transactions. This is needed so that transaction
945 * space required for critical operations can dip into this pool
946 * when at ENOSPC. This is needed for operations like create with
947 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
948 * are not allowed to use this reserved space.
Dave Chinner8babd8a2010-03-04 01:46:25 +0000949 *
950 * This may drive us straight to ENOSPC on mount, but that implies
951 * we were already there on the last unmount. Warn if this occurs.
David Chinner84e1e992007-06-18 16:50:27 +1000952 */
Eric Sandeend5db0f92010-02-05 22:59:53 +0000953 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
954 resblks = xfs_default_resblks(mp);
955 error = xfs_reserve_blocks(mp, &resblks, NULL);
956 if (error)
Dave Chinner0b932cc2011-03-07 10:08:35 +1100957 xfs_warn(mp,
958 "Unable to allocate reserve blocks. Continuing without reserve pool.");
Eric Sandeend5db0f92010-02-05 22:59:53 +0000959 }
David Chinner84e1e992007-06-18 16:50:27 +1000960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
962
Christoph Hellwigb93b6e42009-02-04 09:33:58 +0100963 out_rtunmount:
964 xfs_rtunmount_inodes(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100965 out_rele_rip:
Christoph Hellwig43355092008-03-27 18:01:08 +1100966 IRELE(rip);
Brian Foster0ae120f2015-08-19 10:00:28 +1000967 cancel_delayed_work_sync(&mp->m_reclaim_work);
968 xfs_reclaim_inodes(mp, SYNC_WAIT);
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100969 out_log_dealloc:
Carlos Maiolinoe6b3bb72016-05-18 11:11:27 +1000970 mp->m_flags |= XFS_MOUNT_UNMOUNTING;
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000971 xfs_log_mount_cancel(mp);
Dave Chinnerd4f35122012-04-23 15:59:06 +1000972 out_fail_wait:
973 if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
974 xfs_wait_buftarg(mp->m_logdev_targp);
975 xfs_wait_buftarg(mp->m_ddev_targp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100976 out_free_perag:
Christoph Hellwigff4f0382008-08-13 16:50:47 +1000977 xfs_free_perag(mp);
Dave Chinner0650b552014-06-06 15:01:58 +1000978 out_free_dir:
979 xfs_da_unmount(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100980 out_remove_uuid:
Christoph Hellwig27174202009-03-30 10:21:31 +0200981 xfs_uuid_unmount(mp);
Carlos Maiolino192852b2016-05-18 10:58:51 +1000982 out_remove_error_sysfs:
983 xfs_error_sysfs_del(mp);
Bill O'Donnell225e4632015-10-12 18:21:19 +1100984 out_del_stats:
985 xfs_sysfs_del(&mp->m_stats.xs_kobj);
Brian Fostera31b1d32014-07-15 08:07:01 +1000986 out_remove_sysfs:
987 xfs_sysfs_del(&mp->m_kobj);
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100988 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return error;
990}
991
992/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 * This flushes out the inodes,dquots and the superblock, unmounts the
994 * log and makes sure that incore structures are freed.
995 */
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +1000996void
997xfs_unmountfs(
998 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +10001000 __uint64_t resblks;
1001 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Brian Foster579b62f2012-11-06 09:50:47 -05001003 cancel_delayed_work_sync(&mp->m_eofblocks_work);
1004
Christoph Hellwig7d095252009-06-08 15:33:32 +02001005 xfs_qm_unmount_quotas(mp);
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001006 xfs_rtunmount_inodes(mp);
Christoph Hellwig77508ec2008-08-13 16:49:04 +10001007 IRELE(mp->m_rootip);
1008
David Chinner641c56f2007-06-18 16:50:17 +10001009 /*
1010 * We can potentially deadlock here if we have an inode cluster
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001011 * that has been freed has its buffer still pinned in memory because
David Chinner641c56f2007-06-18 16:50:17 +10001012 * the transaction is still sitting in a iclog. The stale inodes
1013 * on that buffer will have their flush locks held until the
1014 * transaction hits the disk and the callbacks run. the inode
1015 * flush takes the flush lock unconditionally and with nothing to
1016 * push out the iclog we will never get that unlocked. hence we
1017 * need to force the log first.
1018 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001019 xfs_log_force(mp, XFS_LOG_SYNC);
Dave Chinnerc8543632010-02-06 12:39:36 +11001020
1021 /*
Carlos Maiolinoe6b3bb72016-05-18 11:11:27 +10001022 * We now need to tell the world we are unmounting. This will allow
1023 * us to detect that the filesystem is going away and we should error
1024 * out anything that we have been retrying in the background. This will
1025 * prevent neverending retries in AIL pushing from hanging the unmount.
1026 */
1027 mp->m_flags |= XFS_MOUNT_UNMOUNTING;
1028
1029 /*
Christoph Hellwig211e4d42012-04-23 15:58:34 +10001030 * Flush all pending changes from the AIL.
Dave Chinnerc8543632010-02-06 12:39:36 +11001031 */
Christoph Hellwig211e4d42012-04-23 15:58:34 +10001032 xfs_ail_push_all_sync(mp->m_ail);
1033
1034 /*
1035 * And reclaim all inodes. At this point there should be no dirty
Dave Chinner7e185302012-10-08 21:56:00 +11001036 * inodes and none should be pinned or locked, but use synchronous
1037 * reclaim just to be sure. We can stop background inode reclaim
1038 * here as well if it is still running.
Christoph Hellwig211e4d42012-04-23 15:58:34 +10001039 */
Dave Chinner7e185302012-10-08 21:56:00 +11001040 cancel_delayed_work_sync(&mp->m_reclaim_work);
Dave Chinnerc8543632010-02-06 12:39:36 +11001041 xfs_reclaim_inodes(mp, SYNC_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Christoph Hellwig7d095252009-06-08 15:33:32 +02001043 xfs_qm_unmount(mp);
Lachlan McIlroya357a122008-10-30 16:53:25 +11001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
David Chinner84e1e992007-06-18 16:50:27 +10001046 * Unreserve any blocks we have so that when we unmount we don't account
1047 * the reserved free space as used. This is really only necessary for
1048 * lazy superblock counting because it trusts the incore superblock
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001049 * counters to be absolutely correct on clean unmount.
David Chinner84e1e992007-06-18 16:50:27 +10001050 *
1051 * We don't bother correcting this elsewhere for lazy superblock
1052 * counting because on mount of an unclean filesystem we reconstruct the
1053 * correct counter value and this is irrelevant.
1054 *
1055 * For non-lazy counter filesystems, this doesn't matter at all because
1056 * we only every apply deltas to the superblock and hence the incore
1057 * value does not matter....
1058 */
1059 resblks = 0;
David Chinner714082b2008-04-10 12:20:03 +10001060 error = xfs_reserve_blocks(mp, &resblks, NULL);
1061 if (error)
Dave Chinner0b932cc2011-03-07 10:08:35 +11001062 xfs_warn(mp, "Unable to free reserved block pool. "
David Chinner714082b2008-04-10 12:20:03 +10001063 "Freespace may not be correct on next mount.");
1064
Chandra Seetharamanadab0f62011-06-29 22:10:14 +00001065 error = xfs_log_sbcount(mp);
David Chinnere5720ee2008-04-10 12:21:18 +10001066 if (error)
Dave Chinner0b932cc2011-03-07 10:08:35 +11001067 xfs_warn(mp, "Unable to update superblock counters. "
David Chinnere5720ee2008-04-10 12:21:18 +10001068 "Freespace may not be correct on next mount.");
Christoph Hellwig87c7bec2011-09-14 14:08:26 +00001069
Bill O'Donnell225e4632015-10-12 18:21:19 +11001070
Christoph Hellwig21b699c2009-03-16 08:19:29 +01001071 xfs_log_unmount(mp);
Dave Chinner0650b552014-06-06 15:01:58 +10001072 xfs_da_unmount(mp);
Christoph Hellwig27174202009-03-30 10:21:31 +02001073 xfs_uuid_unmount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Christoph Hellwig1550d0b2008-08-13 16:17:37 +10001075#if defined(DEBUG)
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +10001076 xfs_errortag_clearall(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077#endif
Christoph Hellwigff4f0382008-08-13 16:50:47 +10001078 xfs_free_perag(mp);
Brian Fostera31b1d32014-07-15 08:07:01 +10001079
Carlos Maiolino192852b2016-05-18 10:58:51 +10001080 xfs_error_sysfs_del(mp);
Bill O'Donnell225e4632015-10-12 18:21:19 +11001081 xfs_sysfs_del(&mp->m_stats.xs_kobj);
Brian Fostera31b1d32014-07-15 08:07:01 +10001082 xfs_sysfs_del(&mp->m_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Brian Foster91ee5752014-11-28 14:02:59 +11001085/*
1086 * Determine whether modifications can proceed. The caller specifies the minimum
1087 * freeze level for which modifications should not be allowed. This allows
1088 * certain operations to proceed while the freeze sequence is in progress, if
1089 * necessary.
1090 */
1091bool
1092xfs_fs_writable(
1093 struct xfs_mount *mp,
1094 int level)
David Chinner92821e22007-05-24 15:26:31 +10001095{
Brian Foster91ee5752014-11-28 14:02:59 +11001096 ASSERT(level > SB_UNFROZEN);
1097 if ((mp->m_super->s_writers.frozen >= level) ||
1098 XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_RDONLY))
1099 return false;
1100
1101 return true;
David Chinner92821e22007-05-24 15:26:31 +10001102}
1103
1104/*
Alex Elderb2ce3972011-07-11 09:51:44 -05001105 * xfs_log_sbcount
1106 *
Chandra Seetharamanadab0f62011-06-29 22:10:14 +00001107 * Sync the superblock counters to disk.
Alex Elderb2ce3972011-07-11 09:51:44 -05001108 *
Brian Foster91ee5752014-11-28 14:02:59 +11001109 * Note this code can be called during the process of freezing, so we use the
1110 * transaction allocator that does not block when the transaction subsystem is
1111 * in its frozen state.
David Chinner92821e22007-05-24 15:26:31 +10001112 */
1113int
Chandra Seetharamanadab0f62011-06-29 22:10:14 +00001114xfs_log_sbcount(xfs_mount_t *mp)
David Chinner92821e22007-05-24 15:26:31 +10001115{
Brian Foster91ee5752014-11-28 14:02:59 +11001116 /* allow this to proceed during the freeze sequence... */
1117 if (!xfs_fs_writable(mp, SB_FREEZE_COMPLETE))
David Chinner92821e22007-05-24 15:26:31 +10001118 return 0;
1119
David Chinner92821e22007-05-24 15:26:31 +10001120 /*
1121 * we don't need to do this if we are updating the superblock
1122 * counters on every modification.
1123 */
1124 if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
1125 return 0;
1126
Dave Chinner61e63ec2015-01-22 09:10:31 +11001127 return xfs_sync_sb(mp, true);
David Chinner92821e22007-05-24 15:26:31 +10001128}
1129
Dave Chinner8c1903d2015-05-29 07:39:34 +10001130/*
1131 * Deltas for the inode count are +/-64, hence we use a large batch size
1132 * of 128 so we don't need to take the counter lock on every update.
1133 */
1134#define XFS_ICOUNT_BATCH 128
Dave Chinner501ab322015-02-23 21:19:28 +11001135int
1136xfs_mod_icount(
1137 struct xfs_mount *mp,
1138 int64_t delta)
1139{
Dave Chinner8c1903d2015-05-29 07:39:34 +10001140 __percpu_counter_add(&mp->m_icount, delta, XFS_ICOUNT_BATCH);
1141 if (__percpu_counter_compare(&mp->m_icount, 0, XFS_ICOUNT_BATCH) < 0) {
Dave Chinner501ab322015-02-23 21:19:28 +11001142 ASSERT(0);
1143 percpu_counter_add(&mp->m_icount, -delta);
1144 return -EINVAL;
1145 }
1146 return 0;
1147}
1148
Dave Chinnere88b64e2015-02-23 21:19:53 +11001149int
1150xfs_mod_ifree(
1151 struct xfs_mount *mp,
1152 int64_t delta)
1153{
1154 percpu_counter_add(&mp->m_ifree, delta);
1155 if (percpu_counter_compare(&mp->m_ifree, 0) < 0) {
1156 ASSERT(0);
1157 percpu_counter_add(&mp->m_ifree, -delta);
1158 return -EINVAL;
1159 }
1160 return 0;
1161}
Dave Chinner0d485ad2015-02-23 21:22:03 +11001162
Dave Chinner8c1903d2015-05-29 07:39:34 +10001163/*
1164 * Deltas for the block count can vary from 1 to very large, but lock contention
1165 * only occurs on frequent small block count updates such as in the delayed
1166 * allocation path for buffered writes (page a time updates). Hence we set
1167 * a large batch count (1024) to minimise global counter updates except when
1168 * we get near to ENOSPC and we have to be very accurate with our updates.
1169 */
1170#define XFS_FDBLOCKS_BATCH 1024
Dave Chinner0d485ad2015-02-23 21:22:03 +11001171int
1172xfs_mod_fdblocks(
1173 struct xfs_mount *mp,
1174 int64_t delta,
1175 bool rsvd)
1176{
1177 int64_t lcounter;
1178 long long res_used;
1179 s32 batch;
1180
1181 if (delta > 0) {
1182 /*
1183 * If the reserve pool is depleted, put blocks back into it
1184 * first. Most of the time the pool is full.
1185 */
1186 if (likely(mp->m_resblks == mp->m_resblks_avail)) {
1187 percpu_counter_add(&mp->m_fdblocks, delta);
1188 return 0;
1189 }
1190
1191 spin_lock(&mp->m_sb_lock);
1192 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1193
1194 if (res_used > delta) {
1195 mp->m_resblks_avail += delta;
1196 } else {
1197 delta -= res_used;
1198 mp->m_resblks_avail = mp->m_resblks;
1199 percpu_counter_add(&mp->m_fdblocks, delta);
1200 }
1201 spin_unlock(&mp->m_sb_lock);
1202 return 0;
1203 }
1204
1205 /*
1206 * Taking blocks away, need to be more accurate the closer we
1207 * are to zero.
1208 *
Dave Chinner0d485ad2015-02-23 21:22:03 +11001209 * If the counter has a value of less than 2 * max batch size,
1210 * then make everything serialise as we are real close to
1211 * ENOSPC.
1212 */
Dave Chinner8c1903d2015-05-29 07:39:34 +10001213 if (__percpu_counter_compare(&mp->m_fdblocks, 2 * XFS_FDBLOCKS_BATCH,
1214 XFS_FDBLOCKS_BATCH) < 0)
Dave Chinner0d485ad2015-02-23 21:22:03 +11001215 batch = 1;
1216 else
Dave Chinner8c1903d2015-05-29 07:39:34 +10001217 batch = XFS_FDBLOCKS_BATCH;
Dave Chinner0d485ad2015-02-23 21:22:03 +11001218
1219 __percpu_counter_add(&mp->m_fdblocks, delta, batch);
Dave Chinner8c1903d2015-05-29 07:39:34 +10001220 if (__percpu_counter_compare(&mp->m_fdblocks, XFS_ALLOC_SET_ASIDE(mp),
1221 XFS_FDBLOCKS_BATCH) >= 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11001222 /* we had space! */
1223 return 0;
1224 }
1225
1226 /*
1227 * lock up the sb for dipping into reserves before releasing the space
1228 * that took us to ENOSPC.
1229 */
1230 spin_lock(&mp->m_sb_lock);
1231 percpu_counter_add(&mp->m_fdblocks, -delta);
1232 if (!rsvd)
1233 goto fdblocks_enospc;
1234
1235 lcounter = (long long)mp->m_resblks_avail + delta;
1236 if (lcounter >= 0) {
1237 mp->m_resblks_avail = lcounter;
1238 spin_unlock(&mp->m_sb_lock);
1239 return 0;
1240 }
1241 printk_once(KERN_WARNING
1242 "Filesystem \"%s\": reserve blocks depleted! "
1243 "Consider increasing reserve pool size.",
1244 mp->m_fsname);
1245fdblocks_enospc:
1246 spin_unlock(&mp->m_sb_lock);
1247 return -ENOSPC;
1248}
1249
Dave Chinnerbab98bb2015-02-23 21:22:54 +11001250int
1251xfs_mod_frextents(
1252 struct xfs_mount *mp,
1253 int64_t delta)
1254{
1255 int64_t lcounter;
1256 int ret = 0;
1257
1258 spin_lock(&mp->m_sb_lock);
1259 lcounter = mp->m_sb.sb_frextents + delta;
1260 if (lcounter < 0)
1261 ret = -ENOSPC;
1262 else
1263 mp->m_sb.sb_frextents = lcounter;
1264 spin_unlock(&mp->m_sb_lock);
1265 return ret;
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 * xfs_getsb() is called to obtain the buffer for the superblock.
1270 * The buffer is returned locked and read in from disk.
1271 * The buffer should be released with a call to xfs_brelse().
1272 *
1273 * If the flags parameter is BUF_TRYLOCK, then we'll only return
1274 * the superblock buffer if it can be locked without sleeping.
1275 * If it can't then we'll return NULL.
1276 */
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001277struct xfs_buf *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278xfs_getsb(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001279 struct xfs_mount *mp,
1280 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001282 struct xfs_buf *bp = mp->m_sb_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001284 if (!xfs_buf_trylock(bp)) {
1285 if (flags & XBF_TRYLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return NULL;
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001287 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001289
Chandra Seetharaman72790aa2011-07-22 23:40:04 +00001290 xfs_buf_hold(bp);
Dave Chinnerb0388bf2016-02-10 15:01:11 +11001291 ASSERT(bp->b_flags & XBF_DONE);
Jesper Juhl014c2542006-01-15 02:37:08 +01001292 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295/*
1296 * Used to free the superblock along various error paths.
1297 */
1298void
1299xfs_freesb(
Dave Chinner26af6552010-09-22 10:47:20 +10001300 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Dave Chinner26af6552010-09-22 10:47:20 +10001302 struct xfs_buf *bp = mp->m_sb_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Dave Chinner26af6552010-09-22 10:47:20 +10001304 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 mp->m_sb_bp = NULL;
Dave Chinner26af6552010-09-22 10:47:20 +10001306 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
1309/*
Christoph Hellwigdda35b82010-02-15 09:44:46 +00001310 * If the underlying (data/log/rt) device is readonly, there are some
1311 * operations that cannot proceed.
1312 */
1313int
1314xfs_dev_is_read_only(
1315 struct xfs_mount *mp,
1316 char *message)
1317{
1318 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1319 xfs_readonly_buftarg(mp->m_logdev_targp) ||
1320 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001321 xfs_notice(mp, "%s required on read-only device.", message);
1322 xfs_notice(mp, "write access unavailable, cannot proceed.");
Dave Chinner24513372014-06-25 14:58:08 +10001323 return -EROFS;
Christoph Hellwigdda35b82010-02-15 09:44:46 +00001324 }
1325 return 0;
1326}