Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * 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. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * 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 | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
70a9883c | 20 | #include "xfs_shared.h" |
239880ef DC |
21 | #include "xfs_format.h" |
22 | #include "xfs_log_format.h" | |
23 | #include "xfs_trans_resv.h" | |
a844f451 | 24 | #include "xfs_bit.h" |
1da177e4 | 25 | #include "xfs_sb.h" |
1da177e4 | 26 | #include "xfs_mount.h" |
3ab78df2 | 27 | #include "xfs_defer.h" |
57062787 | 28 | #include "xfs_da_format.h" |
9a2cc41c | 29 | #include "xfs_da_btree.h" |
1da177e4 | 30 | #include "xfs_inode.h" |
a4fbe6ab | 31 | #include "xfs_dir2.h" |
a844f451 | 32 | #include "xfs_ialloc.h" |
1da177e4 LT |
33 | #include "xfs_alloc.h" |
34 | #include "xfs_rtalloc.h" | |
35 | #include "xfs_bmap.h" | |
a4fbe6ab DC |
36 | #include "xfs_trans.h" |
37 | #include "xfs_trans_priv.h" | |
38 | #include "xfs_log.h" | |
1da177e4 | 39 | #include "xfs_error.h" |
1da177e4 LT |
40 | #include "xfs_quota.h" |
41 | #include "xfs_fsops.h" | |
0b1b213f | 42 | #include "xfs_trace.h" |
6d8b79cf | 43 | #include "xfs_icache.h" |
a31b1d3d | 44 | #include "xfs_sysfs.h" |
0b1b213f | 45 | |
1da177e4 | 46 | |
27174203 CH |
47 | static DEFINE_MUTEX(xfs_uuid_table_mutex); |
48 | static int xfs_uuid_table_size; | |
49 | static uuid_t *xfs_uuid_table; | |
50 | ||
af3b6382 DW |
51 | void |
52 | xfs_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 | ||
27174203 CH |
61 | /* |
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 | */ | |
65 | STATIC int | |
66 | xfs_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)) { | |
0b932ccc | 76 | xfs_warn(mp, "Filesystem has nil UUID - can't mount"); |
2451337d | 77 | return -EINVAL; |
27174203 CH |
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_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), | |
27174203 CH |
93 | 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); | |
021000e5 | 103 | xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid); |
2451337d | 104 | return -EINVAL; |
27174203 CH |
105 | } |
106 | ||
107 | STATIC void | |
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 (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 | ||
e176579e DC |
131 | STATIC 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 | ||
1da177e4 | 141 | /* |
e176579e | 142 | * Free up the per-ag resources associated with the mount structure. |
1da177e4 | 143 | */ |
c962fb79 | 144 | STATIC void |
ff4f038c | 145 | xfs_free_perag( |
745f6919 | 146 | xfs_mount_t *mp) |
1da177e4 | 147 | { |
1c1c6ebc DC |
148 | xfs_agnumber_t agno; |
149 | struct xfs_perag *pag; | |
150 | ||
151 | 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); | |
e176579e | 155 | ASSERT(pag); |
f83282a8 | 156 | ASSERT(atomic_read(&pag->pag_ref) == 0); |
e176579e | 157 | call_rcu(&pag->rcu_head, __xfs_free_perag); |
1da177e4 | 158 | } |
1da177e4 LT |
159 | } |
160 | ||
4cc929ee NS |
161 | /* |
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 | */ | |
165 | int | |
166 | xfs_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 | ||
d5cf09ba | 173 | /* Limited by ULONG_MAX of page cache index */ |
09cbfeaf | 174 | if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) > ULONG_MAX) |
2451337d | 175 | return -EFBIG; |
4cc929ee NS |
176 | return 0; |
177 | } | |
1da177e4 | 178 | |
1c1c6ebc | 179 | int |
c11e2c36 | 180 | xfs_initialize_perag( |
c11e2c36 | 181 | xfs_mount_t *mp, |
1c1c6ebc DC |
182 | xfs_agnumber_t agcount, |
183 | xfs_agnumber_t *maxagi) | |
1da177e4 | 184 | { |
2d2194f6 | 185 | xfs_agnumber_t index; |
8b26c582 | 186 | xfs_agnumber_t first_initialised = 0; |
1da177e4 | 187 | xfs_perag_t *pag; |
8b26c582 | 188 | int error = -ENOMEM; |
1da177e4 | 189 | |
1c1c6ebc DC |
190 | /* |
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 | } | |
8b26c582 DC |
201 | if (!first_initialised) |
202 | first_initialised = index; | |
fb3b504a | 203 | |
1c1c6ebc DC |
204 | pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL); |
205 | if (!pag) | |
8b26c582 | 206 | goto out_unwind; |
fb3b504a CH |
207 | pag->pag_agno = index; |
208 | pag->pag_mount = mp; | |
1a427ab0 | 209 | spin_lock_init(&pag->pag_ici_lock); |
69b491c2 | 210 | mutex_init(&pag->pag_ici_reclaim_lock); |
fb3b504a | 211 | INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC); |
74f75a0c DC |
212 | spin_lock_init(&pag->pag_buf_lock); |
213 | pag->pag_buf_tree = RB_ROOT; | |
fb3b504a | 214 | |
1c1c6ebc | 215 | if (radix_tree_preload(GFP_NOFS)) |
8b26c582 | 216 | goto out_unwind; |
fb3b504a | 217 | |
1c1c6ebc DC |
218 | 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); | |
8b26c582 DC |
222 | radix_tree_preload_end(); |
223 | error = -EEXIST; | |
224 | goto out_unwind; | |
1c1c6ebc DC |
225 | } |
226 | spin_unlock(&mp->m_perag_lock); | |
227 | radix_tree_preload_end(); | |
228 | } | |
229 | ||
12c3f05c | 230 | index = xfs_set_inode_alloc(mp, agcount); |
fb3b504a | 231 | |
1c1c6ebc DC |
232 | if (maxagi) |
233 | *maxagi = index; | |
234 | return 0; | |
8b26c582 DC |
235 | |
236 | out_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; | |
1da177e4 LT |
243 | } |
244 | ||
1da177e4 LT |
245 | /* |
246 | * xfs_readsb | |
247 | * | |
248 | * Does the initial read of the superblock. | |
249 | */ | |
250 | int | |
ff55068c DC |
251 | xfs_readsb( |
252 | struct xfs_mount *mp, | |
253 | int flags) | |
1da177e4 LT |
254 | { |
255 | unsigned int sector_size; | |
04a1e6c5 DC |
256 | struct xfs_buf *bp; |
257 | struct xfs_sb *sbp = &mp->m_sb; | |
1da177e4 | 258 | int error; |
af34e09d | 259 | int loud = !(flags & XFS_MFSI_QUIET); |
daba5427 | 260 | const struct xfs_buf_ops *buf_ops; |
1da177e4 LT |
261 | |
262 | ASSERT(mp->m_sb_bp == NULL); | |
263 | ASSERT(mp->m_ddev_targp != NULL); | |
264 | ||
daba5427 ES |
265 | /* |
266 | * 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 | ||
1da177e4 | 275 | /* |
c891c30a BF |
276 | * 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. | |
1da177e4 | 280 | */ |
26af6552 | 281 | reread: |
ba372674 | 282 | error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR, |
c891c30a BF |
283 | BTOBB(sector_size), XBF_NO_IOACCT, &bp, |
284 | buf_ops); | |
ba372674 | 285 | if (error) { |
eab4e633 | 286 | if (loud) |
e721f504 | 287 | xfs_warn(mp, "SB validate failed with error %d.", error); |
ac75a1f7 | 288 | /* bad CRC means corrupted metadata */ |
2451337d DC |
289 | if (error == -EFSBADCRC) |
290 | error = -EFSCORRUPTED; | |
ba372674 | 291 | return error; |
eab4e633 | 292 | } |
1da177e4 LT |
293 | |
294 | /* | |
295 | * Initialize the mount structure from the superblock. | |
1da177e4 | 296 | */ |
556b8883 | 297 | xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp)); |
556b8883 DC |
298 | |
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"); | |
2451337d | 306 | error = -EINVAL; |
556b8883 DC |
307 | goto release_buf; |
308 | } | |
ff55068c | 309 | |
1da177e4 LT |
310 | /* |
311 | * We must be able to do sector-sized and sector-aligned IO. | |
312 | */ | |
04a1e6c5 | 313 | if (sector_size > sbp->sb_sectsize) { |
af34e09d DC |
314 | if (loud) |
315 | xfs_warn(mp, "device supports %u byte sectors (not %u)", | |
04a1e6c5 | 316 | sector_size, sbp->sb_sectsize); |
2451337d | 317 | error = -ENOSYS; |
26af6552 | 318 | goto release_buf; |
1da177e4 LT |
319 | } |
320 | ||
daba5427 | 321 | if (buf_ops == NULL) { |
556b8883 DC |
322 | /* |
323 | * Re-read the superblock so the buffer is correctly sized, | |
324 | * and properly verified. | |
325 | */ | |
1da177e4 | 326 | xfs_buf_relse(bp); |
04a1e6c5 | 327 | sector_size = sbp->sb_sectsize; |
daba5427 | 328 | buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops; |
26af6552 | 329 | goto reread; |
1da177e4 LT |
330 | } |
331 | ||
5681ca40 | 332 | xfs_reinit_percpu_counters(mp); |
8d280b98 | 333 | |
04a1e6c5 DC |
334 | /* no need to be quiet anymore, so reset the buf ops */ |
335 | bp->b_ops = &xfs_sb_buf_ops; | |
336 | ||
1da177e4 | 337 | mp->m_sb_bp = bp; |
26af6552 | 338 | xfs_buf_unlock(bp); |
1da177e4 LT |
339 | return 0; |
340 | ||
26af6552 DC |
341 | release_buf: |
342 | xfs_buf_relse(bp); | |
1da177e4 LT |
343 | return error; |
344 | } | |
345 | ||
1da177e4 | 346 | /* |
0771fb45 | 347 | * Update alignment values based on mount options and sb values |
1da177e4 | 348 | */ |
0771fb45 | 349 | STATIC int |
7884bc86 | 350 | xfs_update_alignment(xfs_mount_t *mp) |
1da177e4 | 351 | { |
1da177e4 | 352 | xfs_sb_t *sbp = &(mp->m_sb); |
1da177e4 | 353 | |
4249023a | 354 | if (mp->m_dalign) { |
1da177e4 LT |
355 | /* |
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)) { | |
39a45d84 JL |
361 | xfs_warn(mp, |
362 | "alignment check failed: sunit/swidth vs. blocksize(%d)", | |
363 | sbp->sb_blocksize); | |
2451337d | 364 | return -EINVAL; |
1da177e4 LT |
365 | } 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)) { | |
53487786 | 371 | xfs_warn(mp, |
39a45d84 JL |
372 | "alignment check failed: sunit/swidth vs. agsize(%d)", |
373 | sbp->sb_agblocks); | |
2451337d | 374 | return -EINVAL; |
1da177e4 LT |
375 | } else if (mp->m_dalign) { |
376 | mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth); | |
377 | } else { | |
39a45d84 JL |
378 | xfs_warn(mp, |
379 | "alignment check failed: sunit(%d) less than bsize(%d)", | |
380 | mp->m_dalign, sbp->sb_blocksize); | |
2451337d | 381 | return -EINVAL; |
1da177e4 LT |
382 | } |
383 | } | |
384 | ||
385 | /* | |
386 | * Update superblock with new values | |
387 | * and log changes | |
388 | */ | |
62118709 | 389 | if (xfs_sb_version_hasdalign(sbp)) { |
1da177e4 LT |
390 | if (sbp->sb_unit != mp->m_dalign) { |
391 | sbp->sb_unit = mp->m_dalign; | |
61e63ecb | 392 | mp->m_update_sb = true; |
1da177e4 LT |
393 | } |
394 | if (sbp->sb_width != mp->m_swidth) { | |
395 | sbp->sb_width = mp->m_swidth; | |
61e63ecb | 396 | mp->m_update_sb = true; |
1da177e4 | 397 | } |
34d7f603 JL |
398 | } else { |
399 | xfs_warn(mp, | |
400 | "cannot change alignment: superblock does not support data alignment"); | |
2451337d | 401 | return -EINVAL; |
1da177e4 LT |
402 | } |
403 | } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN && | |
62118709 | 404 | xfs_sb_version_hasdalign(&mp->m_sb)) { |
1da177e4 LT |
405 | mp->m_dalign = sbp->sb_unit; |
406 | mp->m_swidth = sbp->sb_width; | |
407 | } | |
408 | ||
0771fb45 ES |
409 | return 0; |
410 | } | |
1da177e4 | 411 | |
0771fb45 ES |
412 | /* |
413 | * Set the maximum inode count for this filesystem | |
414 | */ | |
415 | STATIC void | |
416 | xfs_set_maxicount(xfs_mount_t *mp) | |
417 | { | |
418 | xfs_sb_t *sbp = &(mp->m_sb); | |
419 | __uint64_t icount; | |
1da177e4 | 420 | |
0771fb45 ES |
421 | if (sbp->sb_imax_pct) { |
422 | /* | |
423 | * Make sure the maximum inode count is a multiple | |
424 | * of the units we allocate inodes in. | |
1da177e4 | 425 | */ |
1da177e4 LT |
426 | 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; | |
0771fb45 | 431 | } else { |
1da177e4 | 432 | mp->m_maxicount = 0; |
1da177e4 | 433 | } |
0771fb45 ES |
434 | } |
435 | ||
436 | /* | |
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 | */ | |
442 | STATIC void | |
443 | xfs_set_rw_sizes(xfs_mount_t *mp) | |
444 | { | |
445 | xfs_sb_t *sbp = &(mp->m_sb); | |
446 | int readio_log, writeio_log; | |
1da177e4 | 447 | |
1da177e4 LT |
448 | 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 | ||
1da177e4 LT |
461 | 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); | |
0771fb45 | 473 | } |
1da177e4 | 474 | |
055388a3 DC |
475 | /* |
476 | * precalculate the low space thresholds for dynamic speculative preallocation. | |
477 | */ | |
478 | void | |
479 | xfs_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 | ||
0771fb45 ES |
493 | /* |
494 | * Set whether we're using inode alignment. | |
495 | */ | |
496 | STATIC void | |
497 | xfs_set_inoalignment(xfs_mount_t *mp) | |
498 | { | |
62118709 | 499 | if (xfs_sb_version_hasalign(&mp->m_sb) && |
1da177e4 LT |
500 | 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; | |
0771fb45 ES |
514 | } |
515 | ||
516 | /* | |
0471f62e | 517 | * Check that the data (and log if separate) is an ok size. |
0771fb45 ES |
518 | */ |
519 | STATIC int | |
ba372674 DC |
520 | xfs_check_sizes( |
521 | struct xfs_mount *mp) | |
0771fb45 | 522 | { |
ba372674 | 523 | struct xfs_buf *bp; |
0771fb45 | 524 | xfs_daddr_t d; |
ba372674 | 525 | int error; |
0771fb45 | 526 | |
1da177e4 LT |
527 | 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) { | |
0b932ccc | 529 | xfs_warn(mp, "filesystem size mismatch detected"); |
2451337d | 530 | return -EFBIG; |
1da177e4 | 531 | } |
ba372674 | 532 | error = xfs_buf_read_uncached(mp->m_ddev_targp, |
1922c949 | 533 | d - XFS_FSS_TO_BB(mp, 1), |
ba372674 DC |
534 | XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL); |
535 | if (error) { | |
0b932ccc | 536 | xfs_warn(mp, "last sector read failed"); |
ba372674 | 537 | return error; |
1da177e4 | 538 | } |
1922c949 | 539 | xfs_buf_relse(bp); |
1da177e4 | 540 | |
ba372674 DC |
541 | 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; | |
548 | } | |
549 | error = xfs_buf_read_uncached(mp->m_logdev_targp, | |
1922c949 | 550 | d - XFS_FSB_TO_BB(mp, 1), |
ba372674 DC |
551 | XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL); |
552 | if (error) { | |
553 | xfs_warn(mp, "log device read failed"); | |
554 | return error; | |
0771fb45 | 555 | } |
ba372674 | 556 | xfs_buf_relse(bp); |
0771fb45 ES |
557 | return 0; |
558 | } | |
559 | ||
7d095257 CH |
560 | /* |
561 | * Clear the quotaflags in memory and in the superblock. | |
562 | */ | |
563 | int | |
564 | xfs_mount_reset_sbqflags( | |
565 | struct xfs_mount *mp) | |
566 | { | |
7d095257 CH |
567 | mp->m_qflags = 0; |
568 | ||
61e63ecb | 569 | /* It is OK to look at sb_qflags in the mount path without m_sb_lock. */ |
7d095257 CH |
570 | 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 | ||
61e63ecb | 576 | if (!xfs_fs_writable(mp, SB_FREEZE_WRITE)) |
7d095257 CH |
577 | return 0; |
578 | ||
61e63ecb | 579 | return xfs_sync_sb(mp, false); |
7d095257 CH |
580 | } |
581 | ||
d5db0f97 ES |
582 | __uint64_t |
583 | xfs_default_resblks(xfs_mount_t *mp) | |
584 | { | |
585 | __uint64_t resblks; | |
586 | ||
587 | /* | |
8babd8a2 DC |
588 | * 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. | |
d5db0f97 ES |
593 | */ |
594 | resblks = mp->m_sb.sb_dblocks; | |
595 | do_div(resblks, 20); | |
8babd8a2 | 596 | resblks = min_t(__uint64_t, resblks, 8192); |
d5db0f97 ES |
597 | return resblks; |
598 | } | |
599 | ||
0771fb45 | 600 | /* |
0771fb45 ES |
601 | * 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 | */ | |
610 | int | |
611 | xfs_mountfs( | |
f0b2efad | 612 | struct xfs_mount *mp) |
0771fb45 | 613 | { |
f0b2efad BF |
614 | 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; | |
0771fb45 | 620 | |
ff55068c | 621 | xfs_sb_mount_common(mp, sbp); |
0771fb45 | 622 | |
ee1c0908 | 623 | /* |
074e427b DC |
624 | * 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. | |
ee1c0908 | 629 | * |
e6957ea4 ES |
630 | * For backwards compatibility, we make both slots equal. |
631 | * | |
074e427b DC |
632 | * 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. | |
ee1c0908 | 638 | */ |
e6957ea4 | 639 | if (xfs_sb_has_mismatched_features2(sbp)) { |
0b932ccc | 640 | xfs_warn(mp, "correcting sb_features alignment problem"); |
ee1c0908 | 641 | sbp->sb_features2 |= sbp->sb_bad_features2; |
61e63ecb | 642 | mp->m_update_sb = true; |
e6957ea4 ES |
643 | |
644 | /* | |
645 | * Re-check for ATTR2 in case it was found in bad_features2 | |
646 | * slot. | |
647 | */ | |
7c12f296 TS |
648 | if (xfs_sb_version_hasattr2(&mp->m_sb) && |
649 | !(mp->m_flags & XFS_MOUNT_NOATTR2)) | |
e6957ea4 | 650 | mp->m_flags |= XFS_MOUNT_ATTR2; |
7c12f296 TS |
651 | } |
652 | ||
653 | if (xfs_sb_version_hasattr2(&mp->m_sb) && | |
654 | (mp->m_flags & XFS_MOUNT_NOATTR2)) { | |
655 | xfs_sb_version_removeattr2(&mp->m_sb); | |
61e63ecb | 656 | mp->m_update_sb = true; |
e6957ea4 | 657 | |
7c12f296 TS |
658 | /* update sb_versionnum for the clearing of the morebits */ |
659 | if (!sbp->sb_features2) | |
61e63ecb | 660 | mp->m_update_sb = true; |
ee1c0908 DC |
661 | } |
662 | ||
263997a6 DC |
663 | /* 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; | |
61e63ecb | 666 | mp->m_update_sb = true; |
263997a6 DC |
667 | } |
668 | ||
0771fb45 ES |
669 | /* |
670 | * 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 | */ | |
7884bc86 | 675 | error = xfs_update_alignment(mp); |
0771fb45 | 676 | if (error) |
f9057e3d | 677 | goto out; |
0771fb45 ES |
678 | |
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 | ||
e6b3bb78 CM |
686 | /* enable fail_at_unmount as default */ |
687 | mp->m_fail_unmount = 1; | |
688 | ||
a31b1d3d | 689 | error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype, NULL, mp->m_fsname); |
27174203 CH |
690 | if (error) |
691 | goto out; | |
1da177e4 | 692 | |
225e4635 BD |
693 | error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype, |
694 | &mp->m_kobj, "stats"); | |
a31b1d3d BF |
695 | if (error) |
696 | goto out_remove_sysfs; | |
697 | ||
192852be | 698 | error = xfs_error_sysfs_init(mp); |
225e4635 BD |
699 | if (error) |
700 | goto out_del_stats; | |
701 | ||
192852be CM |
702 | |
703 | error = xfs_uuid_mount(mp); | |
704 | if (error) | |
705 | goto out_remove_error_sysfs; | |
706 | ||
0771fb45 ES |
707 | /* |
708 | * Set the minimum read and write sizes | |
709 | */ | |
710 | xfs_set_rw_sizes(mp); | |
711 | ||
055388a3 DC |
712 | /* set the low space thresholds for dynamic preallocation */ |
713 | xfs_set_low_space_thresholds(mp); | |
714 | ||
0771fb45 ES |
715 | /* |
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. | |
8f80587b DC |
719 | * |
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. | |
0771fb45 ES |
724 | */ |
725 | mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; | |
8f80587b DC |
726 | 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; | |
8f80587b | 732 | } |
0771fb45 | 733 | |
e5376fc1 BF |
734 | /* |
735 | * 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 | ||
0771fb45 ES |
750 | /* |
751 | * Set inode alignment fields | |
752 | */ | |
753 | xfs_set_inoalignment(mp); | |
754 | ||
755 | /* | |
c2bfbc9b | 756 | * Check that the data (and log if separate) is an ok size. |
0771fb45 | 757 | */ |
4249023a | 758 | error = xfs_check_sizes(mp); |
0771fb45 | 759 | if (error) |
f9057e3d | 760 | goto out_remove_uuid; |
0771fb45 | 761 | |
1da177e4 LT |
762 | /* |
763 | * Initialize realtime fields in the mount structure | |
764 | */ | |
0771fb45 ES |
765 | error = xfs_rtmount_init(mp); |
766 | if (error) { | |
0b932ccc | 767 | xfs_warn(mp, "RT mount failed"); |
f9057e3d | 768 | goto out_remove_uuid; |
1da177e4 LT |
769 | } |
770 | ||
1da177e4 LT |
771 | /* |
772 | * 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 | ||
1da177e4 LT |
777 | mp->m_dmevmask = 0; /* not persistent; set after each mount */ |
778 | ||
0650b554 DC |
779 | error = xfs_da_mount(mp); |
780 | if (error) { | |
781 | xfs_warn(mp, "Failed dir/attr init: %d", error); | |
782 | goto out_remove_uuid; | |
783 | } | |
1da177e4 LT |
784 | |
785 | /* | |
786 | * Initialize the precomputed transaction reservations values. | |
787 | */ | |
788 | xfs_trans_init(mp); | |
789 | ||
1da177e4 LT |
790 | /* |
791 | * Allocate and initialize the per-ag data. | |
792 | */ | |
1c1c6ebc | 793 | spin_lock_init(&mp->m_perag_lock); |
9b98b6f3 | 794 | INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC); |
1c1c6ebc DC |
795 | error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi); |
796 | if (error) { | |
0b932ccc | 797 | xfs_warn(mp, "Failed per-ag init: %d", error); |
0650b554 | 798 | goto out_free_dir; |
1c1c6ebc | 799 | } |
1da177e4 | 800 | |
f9057e3d | 801 | if (!sbp->sb_logblocks) { |
0b932ccc | 802 | xfs_warn(mp, "no log defined"); |
f9057e3d | 803 | XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp); |
2451337d | 804 | error = -EFSCORRUPTED; |
f9057e3d CH |
805 | goto out_free_perag; |
806 | } | |
807 | ||
1da177e4 | 808 | /* |
f0b2efad BF |
809 | * 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. | |
1da177e4 | 812 | */ |
f9057e3d CH |
813 | 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) { | |
0b932ccc | 817 | xfs_warn(mp, "log mount failed"); |
d4f3512b | 818 | goto out_fail_wait; |
1da177e4 LT |
819 | } |
820 | ||
92821e2b DC |
821 | /* |
822 | * 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 | */ | |
92821e2b DC |
840 | 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); | |
f9057e3d | 844 | if (error) |
6eee8972 | 845 | goto out_log_dealloc; |
92821e2b | 846 | } |
f9057e3d | 847 | |
1da177e4 LT |
848 | /* |
849 | * Get and sanity-check the root inode. | |
850 | * Save the pointer to it in the mount structure. | |
851 | */ | |
7b6259e7 | 852 | error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip); |
1da177e4 | 853 | if (error) { |
0b932ccc | 854 | xfs_warn(mp, "failed to read root inode"); |
f9057e3d | 855 | goto out_log_dealloc; |
1da177e4 LT |
856 | } |
857 | ||
858 | ASSERT(rip != NULL); | |
1da177e4 | 859 | |
c19b3b05 | 860 | if (unlikely(!S_ISDIR(VFS_I(rip)->i_mode))) { |
0b932ccc | 861 | xfs_warn(mp, "corrupted root inode %llu: not a directory", |
b6574520 | 862 | (unsigned long long)rip->i_ino); |
1da177e4 LT |
863 | xfs_iunlock(rip, XFS_ILOCK_EXCL); |
864 | XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW, | |
865 | mp); | |
2451337d | 866 | error = -EFSCORRUPTED; |
f9057e3d | 867 | goto out_rele_rip; |
1da177e4 LT |
868 | } |
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 | */ | |
0771fb45 ES |
876 | error = xfs_rtmount_inodes(mp); |
877 | if (error) { | |
1da177e4 LT |
878 | /* |
879 | * Free up the root inode. | |
880 | */ | |
0b932ccc | 881 | xfs_warn(mp, "failed to read RT inodes"); |
f9057e3d | 882 | goto out_rele_rip; |
1da177e4 LT |
883 | } |
884 | ||
885 | /* | |
7884bc86 CH |
886 | * 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. | |
1da177e4 | 889 | */ |
61e63ecb DC |
890 | if (mp->m_update_sb && !(mp->m_flags & XFS_MOUNT_RDONLY)) { |
891 | error = xfs_sync_sb(mp, false); | |
e5720eec | 892 | if (error) { |
0b932ccc | 893 | xfs_warn(mp, "failed to write sb changes"); |
b93b6e43 | 894 | goto out_rtunmount; |
e5720eec DC |
895 | } |
896 | } | |
1da177e4 LT |
897 | |
898 | /* | |
899 | * Initialise the XFS quota management subsystem for this mount | |
900 | */ | |
7d095257 CH |
901 | if (XFS_IS_QUOTA_RUNNING(mp)) { |
902 | error = xfs_qm_newmount(mp, "amount, "aflags); | |
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) { | |
0b932ccc | 914 | xfs_notice(mp, "resetting quota flags"); |
7d095257 CH |
915 | error = xfs_mount_reset_sbqflags(mp); |
916 | if (error) | |
a70a4fa5 | 917 | goto out_rtunmount; |
7d095257 CH |
918 | } |
919 | } | |
1da177e4 LT |
920 | |
921 | /* | |
f0b2efad BF |
922 | * 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. | |
1da177e4 | 925 | */ |
4249023a | 926 | error = xfs_log_mount_finish(mp); |
1da177e4 | 927 | if (error) { |
0b932ccc | 928 | xfs_warn(mp, "log mount finish failed"); |
b93b6e43 | 929 | goto out_rtunmount; |
1da177e4 LT |
930 | } |
931 | ||
932 | /* | |
933 | * Complete the quota initialisation, post-log-replay component. | |
934 | */ | |
7d095257 CH |
935 | if (quotamount) { |
936 | ASSERT(mp->m_qflags == 0); | |
937 | mp->m_qflags = quotaflags; | |
938 | ||
939 | xfs_qm_mount_quotas(mp); | |
940 | } | |
941 | ||
84e1e99f DC |
942 | /* |
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. | |
8babd8a2 DC |
949 | * |
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. | |
84e1e99f | 952 | */ |
d5db0f97 ES |
953 | if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { |
954 | resblks = xfs_default_resblks(mp); | |
955 | error = xfs_reserve_blocks(mp, &resblks, NULL); | |
956 | if (error) | |
0b932ccc DC |
957 | xfs_warn(mp, |
958 | "Unable to allocate reserve blocks. Continuing without reserve pool."); | |
d5db0f97 | 959 | } |
84e1e99f | 960 | |
1da177e4 LT |
961 | return 0; |
962 | ||
b93b6e43 CH |
963 | out_rtunmount: |
964 | xfs_rtunmount_inodes(mp); | |
f9057e3d | 965 | out_rele_rip: |
43355099 | 966 | IRELE(rip); |
0ae120f8 BF |
967 | cancel_delayed_work_sync(&mp->m_reclaim_work); |
968 | xfs_reclaim_inodes(mp, SYNC_WAIT); | |
f9057e3d | 969 | out_log_dealloc: |
e6b3bb78 | 970 | mp->m_flags |= XFS_MOUNT_UNMOUNTING; |
f0b2efad | 971 | xfs_log_mount_cancel(mp); |
d4f3512b DC |
972 | 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); | |
f9057e3d | 976 | out_free_perag: |
ff4f038c | 977 | xfs_free_perag(mp); |
0650b554 DC |
978 | out_free_dir: |
979 | xfs_da_unmount(mp); | |
f9057e3d | 980 | out_remove_uuid: |
27174203 | 981 | xfs_uuid_unmount(mp); |
192852be CM |
982 | out_remove_error_sysfs: |
983 | xfs_error_sysfs_del(mp); | |
225e4635 BD |
984 | out_del_stats: |
985 | xfs_sysfs_del(&mp->m_stats.xs_kobj); | |
a31b1d3d BF |
986 | out_remove_sysfs: |
987 | xfs_sysfs_del(&mp->m_kobj); | |
f9057e3d | 988 | out: |
1da177e4 LT |
989 | return error; |
990 | } | |
991 | ||
992 | /* | |
1da177e4 LT |
993 | * This flushes out the inodes,dquots and the superblock, unmounts the |
994 | * log and makes sure that incore structures are freed. | |
995 | */ | |
41b5c2e7 CH |
996 | void |
997 | xfs_unmountfs( | |
998 | struct xfs_mount *mp) | |
1da177e4 | 999 | { |
41b5c2e7 CH |
1000 | __uint64_t resblks; |
1001 | int error; | |
1da177e4 | 1002 | |
579b62fa BF |
1003 | cancel_delayed_work_sync(&mp->m_eofblocks_work); |
1004 | ||
7d095257 | 1005 | xfs_qm_unmount_quotas(mp); |
b93b6e43 | 1006 | xfs_rtunmount_inodes(mp); |
77508ec8 CH |
1007 | IRELE(mp->m_rootip); |
1008 | ||
641c56fb DC |
1009 | /* |
1010 | * We can potentially deadlock here if we have an inode cluster | |
9da096fd | 1011 | * that has been freed has its buffer still pinned in memory because |
641c56fb DC |
1012 | * 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 | */ | |
a14a348b | 1019 | xfs_log_force(mp, XFS_LOG_SYNC); |
c854363e | 1020 | |
e6b3bb78 CM |
1021 | /* |
1022 | * 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 | ||
c854363e | 1029 | /* |
211e4d43 CH |
1030 | * Flush all pending changes from the AIL. |
1031 | */ | |
1032 | xfs_ail_push_all_sync(mp->m_ail); | |
1033 | ||
1034 | /* | |
1035 | * And reclaim all inodes. At this point there should be no dirty | |
7e18530b DC |
1036 | * 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. | |
c854363e | 1039 | */ |
7e18530b | 1040 | cancel_delayed_work_sync(&mp->m_reclaim_work); |
c854363e | 1041 | xfs_reclaim_inodes(mp, SYNC_WAIT); |
1da177e4 | 1042 | |
7d095257 | 1043 | xfs_qm_unmount(mp); |
a357a121 | 1044 | |
84e1e99f DC |
1045 | /* |
1046 | * 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 | |
9da096fd | 1049 | * counters to be absolutely correct on clean unmount. |
84e1e99f DC |
1050 | * |
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; | |
714082bc DC |
1060 | error = xfs_reserve_blocks(mp, &resblks, NULL); |
1061 | if (error) | |
0b932ccc | 1062 | xfs_warn(mp, "Unable to free reserved block pool. " |
714082bc DC |
1063 | "Freespace may not be correct on next mount."); |
1064 | ||
adab0f67 | 1065 | error = xfs_log_sbcount(mp); |
e5720eec | 1066 | if (error) |
0b932ccc | 1067 | xfs_warn(mp, "Unable to update superblock counters. " |
e5720eec | 1068 | "Freespace may not be correct on next mount."); |
87c7bec7 | 1069 | |
225e4635 | 1070 | |
21b699c8 | 1071 | xfs_log_unmount(mp); |
0650b554 | 1072 | xfs_da_unmount(mp); |
27174203 | 1073 | xfs_uuid_unmount(mp); |
1da177e4 | 1074 | |
1550d0b0 | 1075 | #if defined(DEBUG) |
0ce4cfd4 | 1076 | xfs_errortag_clearall(mp, 0); |
1da177e4 | 1077 | #endif |
ff4f038c | 1078 | xfs_free_perag(mp); |
a31b1d3d | 1079 | |
192852be | 1080 | xfs_error_sysfs_del(mp); |
225e4635 | 1081 | xfs_sysfs_del(&mp->m_stats.xs_kobj); |
a31b1d3d | 1082 | xfs_sysfs_del(&mp->m_kobj); |
1da177e4 LT |
1083 | } |
1084 | ||
91ee575f BF |
1085 | /* |
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 | */ | |
1091 | bool | |
1092 | xfs_fs_writable( | |
1093 | struct xfs_mount *mp, | |
1094 | int level) | |
92821e2b | 1095 | { |
91ee575f BF |
1096 | 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; | |
92821e2b DC |
1102 | } |
1103 | ||
1104 | /* | |
b2ce3974 AE |
1105 | * xfs_log_sbcount |
1106 | * | |
adab0f67 | 1107 | * Sync the superblock counters to disk. |
b2ce3974 | 1108 | * |
91ee575f BF |
1109 | * 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. | |
92821e2b DC |
1112 | */ |
1113 | int | |
adab0f67 | 1114 | xfs_log_sbcount(xfs_mount_t *mp) |
92821e2b | 1115 | { |
91ee575f BF |
1116 | /* allow this to proceed during the freeze sequence... */ |
1117 | if (!xfs_fs_writable(mp, SB_FREEZE_COMPLETE)) | |
92821e2b DC |
1118 | return 0; |
1119 | ||
92821e2b DC |
1120 | /* |
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 | ||
61e63ecb | 1127 | return xfs_sync_sb(mp, true); |
92821e2b DC |
1128 | } |
1129 | ||
8c1903d3 DC |
1130 | /* |
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 | |
501ab323 DC |
1135 | int |
1136 | xfs_mod_icount( | |
1137 | struct xfs_mount *mp, | |
1138 | int64_t delta) | |
1139 | { | |
8c1903d3 DC |
1140 | __percpu_counter_add(&mp->m_icount, delta, XFS_ICOUNT_BATCH); |
1141 | if (__percpu_counter_compare(&mp->m_icount, 0, XFS_ICOUNT_BATCH) < 0) { | |
501ab323 DC |
1142 | ASSERT(0); |
1143 | percpu_counter_add(&mp->m_icount, -delta); | |
1144 | return -EINVAL; | |
1145 | } | |
1146 | return 0; | |
1147 | } | |
1148 | ||
e88b64ea DC |
1149 | int |
1150 | xfs_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 | } | |
0d485ada | 1162 | |
8c1903d3 DC |
1163 | /* |
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 | |
0d485ada DC |
1171 | int |
1172 | xfs_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 | * | |
0d485ada DC |
1209 | * 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 | */ | |
8c1903d3 DC |
1213 | if (__percpu_counter_compare(&mp->m_fdblocks, 2 * XFS_FDBLOCKS_BATCH, |
1214 | XFS_FDBLOCKS_BATCH) < 0) | |
0d485ada DC |
1215 | batch = 1; |
1216 | else | |
8c1903d3 | 1217 | batch = XFS_FDBLOCKS_BATCH; |
0d485ada DC |
1218 | |
1219 | __percpu_counter_add(&mp->m_fdblocks, delta, batch); | |
8c1903d3 DC |
1220 | if (__percpu_counter_compare(&mp->m_fdblocks, XFS_ALLOC_SET_ASIDE(mp), |
1221 | XFS_FDBLOCKS_BATCH) >= 0) { | |
0d485ada DC |
1222 | /* 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); | |
1245 | fdblocks_enospc: | |
1246 | spin_unlock(&mp->m_sb_lock); | |
1247 | return -ENOSPC; | |
1248 | } | |
1249 | ||
bab98bbe DC |
1250 | int |
1251 | xfs_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 | ||
1da177e4 LT |
1268 | /* |
1269 | * 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 | */ | |
0c842ad4 | 1277 | struct xfs_buf * |
1da177e4 | 1278 | xfs_getsb( |
0c842ad4 CH |
1279 | struct xfs_mount *mp, |
1280 | int flags) | |
1da177e4 | 1281 | { |
0c842ad4 | 1282 | struct xfs_buf *bp = mp->m_sb_bp; |
1da177e4 | 1283 | |
0c842ad4 CH |
1284 | if (!xfs_buf_trylock(bp)) { |
1285 | if (flags & XBF_TRYLOCK) | |
1da177e4 | 1286 | return NULL; |
0c842ad4 | 1287 | xfs_buf_lock(bp); |
1da177e4 | 1288 | } |
0c842ad4 | 1289 | |
72790aa1 | 1290 | xfs_buf_hold(bp); |
b0388bf1 | 1291 | ASSERT(bp->b_flags & XBF_DONE); |
014c2544 | 1292 | return bp; |
1da177e4 LT |
1293 | } |
1294 | ||
1295 | /* | |
1296 | * Used to free the superblock along various error paths. | |
1297 | */ | |
1298 | void | |
1299 | xfs_freesb( | |
26af6552 | 1300 | struct xfs_mount *mp) |
1da177e4 | 1301 | { |
26af6552 | 1302 | struct xfs_buf *bp = mp->m_sb_bp; |
1da177e4 | 1303 | |
26af6552 | 1304 | xfs_buf_lock(bp); |
1da177e4 | 1305 | mp->m_sb_bp = NULL; |
26af6552 | 1306 | xfs_buf_relse(bp); |
1da177e4 LT |
1307 | } |
1308 | ||
dda35b8f CH |
1309 | /* |
1310 | * If the underlying (data/log/rt) device is readonly, there are some | |
1311 | * operations that cannot proceed. | |
1312 | */ | |
1313 | int | |
1314 | xfs_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))) { | |
0b932ccc DC |
1321 | xfs_notice(mp, "%s required on read-only device.", message); |
1322 | xfs_notice(mp, "write access unavailable, cannot proceed."); | |
2451337d | 1323 | return -EROFS; |
dda35b8f CH |
1324 | } |
1325 | return 0; | |
1326 | } |