]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 NS |
3 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
1da177e4 | 5 | */ |
1da177e4 | 6 | #include "xfs.h" |
a844f451 | 7 | #include "xfs_fs.h" |
70a9883c | 8 | #include "xfs_shared.h" |
239880ef DC |
9 | #include "xfs_format.h" |
10 | #include "xfs_log_format.h" | |
11 | #include "xfs_trans_resv.h" | |
a844f451 | 12 | #include "xfs_bit.h" |
1da177e4 | 13 | #include "xfs_mount.h" |
1da177e4 | 14 | #include "xfs_inode.h" |
a844f451 NS |
15 | #include "xfs_btree.h" |
16 | #include "xfs_ialloc.h" | |
a4fbe6ab | 17 | #include "xfs_ialloc_btree.h" |
1da177e4 | 18 | #include "xfs_alloc.h" |
e9e899a2 | 19 | #include "xfs_errortag.h" |
1da177e4 LT |
20 | #include "xfs_error.h" |
21 | #include "xfs_bmap.h" | |
239880ef | 22 | #include "xfs_trans.h" |
983d09ff | 23 | #include "xfs_buf_item.h" |
ddf6ad01 | 24 | #include "xfs_icreate_item.h" |
7bb85ef3 | 25 | #include "xfs_icache.h" |
d123031a | 26 | #include "xfs_trace.h" |
a45086e2 | 27 | #include "xfs_log.h" |
340785cc | 28 | #include "xfs_rmap.h" |
9bbafc71 | 29 | #include "xfs_ag.h" |
de6077ec | 30 | #include "xfs_health.h" |
1da177e4 | 31 | |
fe033cc8 | 32 | /* |
21875505 | 33 | * Lookup a record by ino in the btree given by cur. |
fe033cc8 | 34 | */ |
81e25176 | 35 | int /* error */ |
21875505 | 36 | xfs_inobt_lookup( |
fe033cc8 CH |
37 | struct xfs_btree_cur *cur, /* btree cursor */ |
38 | xfs_agino_t ino, /* starting inode of chunk */ | |
21875505 | 39 | xfs_lookup_t dir, /* <=, >=, == */ |
fe033cc8 CH |
40 | int *stat) /* success/failure */ |
41 | { | |
42 | cur->bc_rec.i.ir_startino = ino; | |
5419040f BF |
43 | cur->bc_rec.i.ir_holemask = 0; |
44 | cur->bc_rec.i.ir_count = 0; | |
21875505 CH |
45 | cur->bc_rec.i.ir_freecount = 0; |
46 | cur->bc_rec.i.ir_free = 0; | |
47 | return xfs_btree_lookup(cur, dir, stat); | |
fe033cc8 CH |
48 | } |
49 | ||
278d0ca1 | 50 | /* |
afabc24a | 51 | * Update the record referred to by cur to the value given. |
278d0ca1 CH |
52 | * This either works (return 0) or gets an EFSCORRUPTED error. |
53 | */ | |
54 | STATIC int /* error */ | |
55 | xfs_inobt_update( | |
56 | struct xfs_btree_cur *cur, /* btree cursor */ | |
afabc24a | 57 | xfs_inobt_rec_incore_t *irec) /* btree record */ |
278d0ca1 CH |
58 | { |
59 | union xfs_btree_rec rec; | |
60 | ||
afabc24a | 61 | rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino); |
38c26bfd | 62 | if (xfs_has_sparseinodes(cur->bc_mp)) { |
5419040f BF |
63 | rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask); |
64 | rec.inobt.ir_u.sp.ir_count = irec->ir_count; | |
65 | rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount; | |
66 | } else { | |
67 | /* ir_holemask/ir_count not supported on-disk */ | |
68 | rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount); | |
69 | } | |
afabc24a | 70 | rec.inobt.ir_free = cpu_to_be64(irec->ir_free); |
278d0ca1 CH |
71 | return xfs_btree_update(cur, &rec); |
72 | } | |
73 | ||
e936945e DW |
74 | /* Convert on-disk btree record to incore inobt record. */ |
75 | void | |
76 | xfs_inobt_btrec_to_irec( | |
77 | struct xfs_mount *mp, | |
159eb69d | 78 | const union xfs_btree_rec *rec, |
e936945e | 79 | struct xfs_inobt_rec_incore *irec) |
8cc938fe | 80 | { |
5419040f | 81 | irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino); |
38c26bfd | 82 | if (xfs_has_sparseinodes(mp)) { |
5419040f BF |
83 | irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask); |
84 | irec->ir_count = rec->inobt.ir_u.sp.ir_count; | |
85 | irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount; | |
86 | } else { | |
87 | /* | |
88 | * ir_holemask/ir_count not supported on-disk. Fill in hardcoded | |
89 | * values for full inode chunks. | |
90 | */ | |
91 | irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL; | |
92 | irec->ir_count = XFS_INODES_PER_CHUNK; | |
93 | irec->ir_freecount = | |
94 | be32_to_cpu(rec->inobt.ir_u.f.ir_freecount); | |
8cc938fe | 95 | } |
5419040f | 96 | irec->ir_free = be64_to_cpu(rec->inobt.ir_free); |
e936945e DW |
97 | } |
98 | ||
dbfbf3bd DW |
99 | /* Compute the freecount of an incore inode record. */ |
100 | uint8_t | |
101 | xfs_inobt_rec_freecount( | |
102 | const struct xfs_inobt_rec_incore *irec) | |
103 | { | |
104 | uint64_t realfree = irec->ir_free; | |
105 | ||
106 | if (xfs_inobt_issparse(irec->ir_holemask)) | |
107 | realfree &= xfs_inobt_irec_to_allocmask(irec); | |
108 | return hweight64(realfree); | |
109 | } | |
110 | ||
366a0b8d DW |
111 | /* Simple checks for inode records. */ |
112 | xfs_failaddr_t | |
113 | xfs_inobt_check_irec( | |
dbfbf3bd | 114 | struct xfs_perag *pag, |
366a0b8d DW |
115 | const struct xfs_inobt_rec_incore *irec) |
116 | { | |
de1a9ce2 | 117 | /* Record has to be properly aligned within the AG. */ |
dbfbf3bd | 118 | if (!xfs_verify_agino(pag, irec->ir_startino)) |
366a0b8d | 119 | return __this_address; |
dbfbf3bd | 120 | if (!xfs_verify_agino(pag, |
de1a9ce2 DW |
121 | irec->ir_startino + XFS_INODES_PER_CHUNK - 1)) |
122 | return __this_address; | |
366a0b8d DW |
123 | if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT || |
124 | irec->ir_count > XFS_INODES_PER_CHUNK) | |
125 | return __this_address; | |
126 | if (irec->ir_freecount > XFS_INODES_PER_CHUNK) | |
127 | return __this_address; | |
128 | ||
dbfbf3bd | 129 | if (xfs_inobt_rec_freecount(irec) != irec->ir_freecount) |
366a0b8d DW |
130 | return __this_address; |
131 | ||
132 | return NULL; | |
133 | } | |
134 | ||
ee12eaaa DW |
135 | static inline int |
136 | xfs_inobt_complain_bad_rec( | |
137 | struct xfs_btree_cur *cur, | |
138 | xfs_failaddr_t fa, | |
139 | const struct xfs_inobt_rec_incore *irec) | |
140 | { | |
141 | struct xfs_mount *mp = cur->bc_mp; | |
142 | ||
143 | xfs_warn(mp, | |
77953b97 | 144 | "%sbt record corruption in AG %d detected at %pS!", |
77a530e6 | 145 | cur->bc_ops->name, cur->bc_group->xg_gno, fa); |
ee12eaaa DW |
146 | xfs_warn(mp, |
147 | "start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x", | |
148 | irec->ir_startino, irec->ir_count, irec->ir_freecount, | |
149 | irec->ir_free, irec->ir_holemask); | |
a78d10f4 | 150 | xfs_btree_mark_sick(cur); |
ee12eaaa DW |
151 | return -EFSCORRUPTED; |
152 | } | |
153 | ||
e936945e DW |
154 | /* |
155 | * Get the data from the pointed-to record. | |
156 | */ | |
157 | int | |
158 | xfs_inobt_get_rec( | |
159 | struct xfs_btree_cur *cur, | |
160 | struct xfs_inobt_rec_incore *irec, | |
161 | int *stat) | |
162 | { | |
9e6c08d4 | 163 | struct xfs_mount *mp = cur->bc_mp; |
e936945e | 164 | union xfs_btree_rec *rec; |
366a0b8d | 165 | xfs_failaddr_t fa; |
e936945e DW |
166 | int error; |
167 | ||
168 | error = xfs_btree_get_rec(cur, &rec, stat); | |
169 | if (error || *stat == 0) | |
170 | return error; | |
171 | ||
9e6c08d4 | 172 | xfs_inobt_btrec_to_irec(mp, rec, irec); |
77a530e6 | 173 | fa = xfs_inobt_check_irec(to_perag(cur->bc_group), irec); |
366a0b8d | 174 | if (fa) |
ee12eaaa | 175 | return xfs_inobt_complain_bad_rec(cur, fa, irec); |
5419040f BF |
176 | |
177 | return 0; | |
8cc938fe CH |
178 | } |
179 | ||
0aa0a756 BF |
180 | /* |
181 | * Insert a single inobt record. Cursor must already point to desired location. | |
182 | */ | |
7f8f1313 | 183 | int |
0aa0a756 BF |
184 | xfs_inobt_insert_rec( |
185 | struct xfs_btree_cur *cur, | |
c8ce540d DW |
186 | uint16_t holemask, |
187 | uint8_t count, | |
188 | int32_t freecount, | |
0aa0a756 BF |
189 | xfs_inofree_t free, |
190 | int *stat) | |
191 | { | |
5419040f BF |
192 | cur->bc_rec.i.ir_holemask = holemask; |
193 | cur->bc_rec.i.ir_count = count; | |
0aa0a756 BF |
194 | cur->bc_rec.i.ir_freecount = freecount; |
195 | cur->bc_rec.i.ir_free = free; | |
196 | return xfs_btree_insert(cur, stat); | |
197 | } | |
198 | ||
199 | /* | |
200 | * Insert records describing a newly allocated inode chunk into the inobt. | |
201 | */ | |
202 | STATIC int | |
203 | xfs_inobt_insert( | |
dedab3e4 | 204 | struct xfs_perag *pag, |
0aa0a756 BF |
205 | struct xfs_trans *tp, |
206 | struct xfs_buf *agbp, | |
207 | xfs_agino_t newino, | |
208 | xfs_agino_t newlen, | |
fbeef4e0 | 209 | bool is_finobt) |
0aa0a756 BF |
210 | { |
211 | struct xfs_btree_cur *cur; | |
0aa0a756 BF |
212 | xfs_agino_t thisino; |
213 | int i; | |
214 | int error; | |
215 | ||
fbeef4e0 | 216 | if (is_finobt) |
14dd46cf CH |
217 | cur = xfs_finobt_init_cursor(pag, tp, agbp); |
218 | else | |
219 | cur = xfs_inobt_init_cursor(pag, tp, agbp); | |
0aa0a756 BF |
220 | |
221 | for (thisino = newino; | |
222 | thisino < newino + newlen; | |
223 | thisino += XFS_INODES_PER_CHUNK) { | |
224 | error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i); | |
225 | if (error) { | |
226 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
227 | return error; | |
228 | } | |
229 | ASSERT(i == 0); | |
230 | ||
5419040f BF |
231 | error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL, |
232 | XFS_INODES_PER_CHUNK, | |
233 | XFS_INODES_PER_CHUNK, | |
0aa0a756 BF |
234 | XFS_INOBT_ALL_FREE, &i); |
235 | if (error) { | |
236 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
237 | return error; | |
238 | } | |
239 | ASSERT(i == 1); | |
240 | } | |
241 | ||
242 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
243 | ||
244 | return 0; | |
245 | } | |
246 | ||
0b48db80 DC |
247 | /* |
248 | * Verify that the number of free inodes in the AGI is correct. | |
249 | */ | |
250 | #ifdef DEBUG | |
9ba0889e | 251 | static int |
0b48db80 | 252 | xfs_check_agi_freecount( |
9ba0889e | 253 | struct xfs_btree_cur *cur) |
0b48db80 DC |
254 | { |
255 | if (cur->bc_nlevels == 1) { | |
256 | xfs_inobt_rec_incore_t rec; | |
257 | int freecount = 0; | |
258 | int error; | |
259 | int i; | |
260 | ||
21875505 | 261 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); |
0b48db80 DC |
262 | if (error) |
263 | return error; | |
264 | ||
265 | do { | |
266 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
267 | if (error) | |
268 | return error; | |
269 | ||
270 | if (i) { | |
271 | freecount += rec.ir_freecount; | |
272 | error = xfs_btree_increment(cur, 0, &i); | |
273 | if (error) | |
274 | return error; | |
275 | } | |
276 | } while (i == 1); | |
277 | ||
77a530e6 CH |
278 | if (!xfs_is_shutdown(cur->bc_mp)) { |
279 | ASSERT(freecount == | |
280 | to_perag(cur->bc_group)->pagi_freecount); | |
281 | } | |
0b48db80 DC |
282 | } |
283 | return 0; | |
284 | } | |
285 | #else | |
9ba0889e | 286 | #define xfs_check_agi_freecount(cur) 0 |
0b48db80 DC |
287 | #endif |
288 | ||
85c0b2ab | 289 | /* |
28c8e41a DC |
290 | * Initialise a new set of inodes. When called without a transaction context |
291 | * (e.g. from recovery) we initiate a delayed write of the inode buffers rather | |
292 | * than logging them (which in a transaction context puts them into the AIL | |
293 | * for writeback rather than the xfsbufd queue). | |
85c0b2ab | 294 | */ |
ddf6ad01 | 295 | int |
85c0b2ab DC |
296 | xfs_ialloc_inode_init( |
297 | struct xfs_mount *mp, | |
298 | struct xfs_trans *tp, | |
28c8e41a | 299 | struct list_head *buffer_list, |
463958af | 300 | int icount, |
85c0b2ab DC |
301 | xfs_agnumber_t agno, |
302 | xfs_agblock_t agbno, | |
303 | xfs_agblock_t length, | |
304 | unsigned int gen) | |
305 | { | |
306 | struct xfs_buf *fbuf; | |
307 | struct xfs_dinode *free; | |
83dcdb44 | 308 | int nbufs; |
85c0b2ab DC |
309 | int version; |
310 | int i, j; | |
311 | xfs_daddr_t d; | |
93848a99 | 312 | xfs_ino_t ino = 0; |
ce92464c | 313 | int error; |
85c0b2ab DC |
314 | |
315 | /* | |
6e0c7b8c JL |
316 | * Loop over the new block(s), filling in the inodes. For small block |
317 | * sizes, manipulate the inodes in buffers which are multiples of the | |
318 | * blocks size. | |
85c0b2ab | 319 | */ |
ef325959 | 320 | nbufs = length / M_IGEO(mp)->blocks_per_cluster; |
85c0b2ab DC |
321 | |
322 | /* | |
93848a99 CH |
323 | * Figure out what version number to use in the inodes we create. If |
324 | * the superblock version has caught up to the one that supports the new | |
325 | * inode format, then use the new inode version. Otherwise use the old | |
326 | * version so that old kernels will continue to be able to use the file | |
327 | * system. | |
328 | * | |
329 | * For v3 inodes, we also need to write the inode number into the inode, | |
330 | * so calculate the first inode number of the chunk here as | |
43004b2a | 331 | * XFS_AGB_TO_AGINO() only works within a filesystem block, not |
93848a99 CH |
332 | * across multiple filesystem blocks (such as a cluster) and so cannot |
333 | * be used in the cluster buffer loop below. | |
334 | * | |
335 | * Further, because we are writing the inode directly into the buffer | |
336 | * and calculating a CRC on the entire inode, we have ot log the entire | |
337 | * inode so that the entire range the CRC covers is present in the log. | |
338 | * That means for v3 inode we log the entire buffer rather than just the | |
339 | * inode cores. | |
85c0b2ab | 340 | */ |
ebd9027d | 341 | if (xfs_has_v3inodes(mp)) { |
93848a99 | 342 | version = 3; |
43004b2a | 343 | ino = XFS_AGINO_TO_INO(mp, agno, XFS_AGB_TO_AGINO(mp, agbno)); |
ddf6ad01 DC |
344 | |
345 | /* | |
346 | * log the initialisation that is about to take place as an | |
347 | * logical operation. This means the transaction does not | |
348 | * need to log the physical changes to the inode buffers as log | |
349 | * recovery will know what initialisation is actually needed. | |
350 | * Hence we only need to log the buffers as "ordered" buffers so | |
351 | * they track in the AIL as if they were physically logged. | |
352 | */ | |
353 | if (tp) | |
463958af | 354 | xfs_icreate_log(tp, agno, agbno, icount, |
ddf6ad01 | 355 | mp->m_sb.sb_inodesize, length, gen); |
263997a6 | 356 | } else |
85c0b2ab | 357 | version = 2; |
85c0b2ab DC |
358 | |
359 | for (j = 0; j < nbufs; j++) { | |
360 | /* | |
361 | * Get the block. | |
362 | */ | |
83dcdb44 | 363 | d = XFS_AGB_TO_DADDR(mp, agno, agbno + |
ef325959 | 364 | (j * M_IGEO(mp)->blocks_per_cluster)); |
ce92464c DW |
365 | error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, |
366 | mp->m_bsize * M_IGEO(mp)->blocks_per_cluster, | |
367 | XBF_UNMAPPED, &fbuf); | |
368 | if (error) | |
369 | return error; | |
ddf6ad01 DC |
370 | |
371 | /* Initialize the inode buffers and log them appropriately. */ | |
1813dd64 | 372 | fbuf->b_ops = &xfs_inode_buf_ops; |
93848a99 | 373 | xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length)); |
ef325959 | 374 | for (i = 0; i < M_IGEO(mp)->inodes_per_cluster; i++) { |
85c0b2ab | 375 | int ioffset = i << mp->m_sb.sb_inodelog; |
85c0b2ab DC |
376 | |
377 | free = xfs_make_iptr(mp, fbuf, i); | |
378 | free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC); | |
379 | free->di_version = version; | |
380 | free->di_gen = cpu_to_be32(gen); | |
381 | free->di_next_unlinked = cpu_to_be32(NULLAGINO); | |
93848a99 CH |
382 | |
383 | if (version == 3) { | |
384 | free->di_ino = cpu_to_be64(ino); | |
385 | ino++; | |
ce748eaa ES |
386 | uuid_copy(&free->di_uuid, |
387 | &mp->m_sb.sb_meta_uuid); | |
93848a99 | 388 | xfs_dinode_calc_crc(mp, free); |
28c8e41a | 389 | } else if (tp) { |
93848a99 CH |
390 | /* just log the inode core */ |
391 | xfs_trans_log_buf(tp, fbuf, ioffset, | |
cf28e17c | 392 | ioffset + XFS_DINODE_SIZE(mp) - 1); |
93848a99 CH |
393 | } |
394 | } | |
28c8e41a DC |
395 | |
396 | if (tp) { | |
397 | /* | |
398 | * Mark the buffer as an inode allocation buffer so it | |
399 | * sticks in AIL at the point of this allocation | |
400 | * transaction. This ensures the they are on disk before | |
401 | * the tail of the log can be moved past this | |
402 | * transaction (i.e. by preventing relogging from moving | |
403 | * it forward in the log). | |
404 | */ | |
405 | xfs_trans_inode_alloc_buf(tp, fbuf); | |
406 | if (version == 3) { | |
ddf6ad01 DC |
407 | /* |
408 | * Mark the buffer as ordered so that they are | |
409 | * not physically logged in the transaction but | |
410 | * still tracked in the AIL as part of the | |
411 | * transaction and pin the log appropriately. | |
412 | */ | |
413 | xfs_trans_ordered_buf(tp, fbuf); | |
28c8e41a DC |
414 | } |
415 | } else { | |
416 | fbuf->b_flags |= XBF_DONE; | |
417 | xfs_buf_delwri_queue(fbuf, buffer_list); | |
418 | xfs_buf_relse(fbuf); | |
85c0b2ab | 419 | } |
85c0b2ab | 420 | } |
2a30f36d | 421 | return 0; |
85c0b2ab DC |
422 | } |
423 | ||
56d1115c BF |
424 | /* |
425 | * Align startino and allocmask for a recently allocated sparse chunk such that | |
426 | * they are fit for insertion (or merge) into the on-disk inode btrees. | |
427 | * | |
428 | * Background: | |
429 | * | |
430 | * When enabled, sparse inode support increases the inode alignment from cluster | |
431 | * size to inode chunk size. This means that the minimum range between two | |
432 | * non-adjacent inode records in the inobt is large enough for a full inode | |
433 | * record. This allows for cluster sized, cluster aligned block allocation | |
434 | * without need to worry about whether the resulting inode record overlaps with | |
435 | * another record in the tree. Without this basic rule, we would have to deal | |
436 | * with the consequences of overlap by potentially undoing recent allocations in | |
437 | * the inode allocation codepath. | |
438 | * | |
439 | * Because of this alignment rule (which is enforced on mount), there are two | |
440 | * inobt possibilities for newly allocated sparse chunks. One is that the | |
441 | * aligned inode record for the chunk covers a range of inodes not already | |
442 | * covered in the inobt (i.e., it is safe to insert a new sparse record). The | |
443 | * other is that a record already exists at the aligned startino that considers | |
444 | * the newly allocated range as sparse. In the latter case, record content is | |
445 | * merged in hope that sparse inode chunks fill to full chunks over time. | |
446 | */ | |
447 | STATIC void | |
448 | xfs_align_sparse_ino( | |
449 | struct xfs_mount *mp, | |
450 | xfs_agino_t *startino, | |
451 | uint16_t *allocmask) | |
452 | { | |
453 | xfs_agblock_t agbno; | |
454 | xfs_agblock_t mod; | |
455 | int offset; | |
456 | ||
457 | agbno = XFS_AGINO_TO_AGBNO(mp, *startino); | |
458 | mod = agbno % mp->m_sb.sb_inoalignmt; | |
459 | if (!mod) | |
460 | return; | |
461 | ||
462 | /* calculate the inode offset and align startino */ | |
43004b2a | 463 | offset = XFS_AGB_TO_AGINO(mp, mod); |
56d1115c BF |
464 | *startino -= offset; |
465 | ||
466 | /* | |
467 | * Since startino has been aligned down, left shift allocmask such that | |
468 | * it continues to represent the same physical inodes relative to the | |
469 | * new startino. | |
470 | */ | |
471 | *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT; | |
472 | } | |
473 | ||
474 | /* | |
475 | * Determine whether the source inode record can merge into the target. Both | |
476 | * records must be sparse, the inode ranges must match and there must be no | |
477 | * allocation overlap between the records. | |
478 | */ | |
479 | STATIC bool | |
480 | __xfs_inobt_can_merge( | |
481 | struct xfs_inobt_rec_incore *trec, /* tgt record */ | |
482 | struct xfs_inobt_rec_incore *srec) /* src record */ | |
483 | { | |
484 | uint64_t talloc; | |
485 | uint64_t salloc; | |
486 | ||
487 | /* records must cover the same inode range */ | |
488 | if (trec->ir_startino != srec->ir_startino) | |
489 | return false; | |
490 | ||
491 | /* both records must be sparse */ | |
492 | if (!xfs_inobt_issparse(trec->ir_holemask) || | |
493 | !xfs_inobt_issparse(srec->ir_holemask)) | |
494 | return false; | |
495 | ||
496 | /* both records must track some inodes */ | |
497 | if (!trec->ir_count || !srec->ir_count) | |
498 | return false; | |
499 | ||
500 | /* can't exceed capacity of a full record */ | |
501 | if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK) | |
502 | return false; | |
503 | ||
504 | /* verify there is no allocation overlap */ | |
505 | talloc = xfs_inobt_irec_to_allocmask(trec); | |
506 | salloc = xfs_inobt_irec_to_allocmask(srec); | |
507 | if (talloc & salloc) | |
508 | return false; | |
509 | ||
510 | return true; | |
511 | } | |
512 | ||
513 | /* | |
514 | * Merge the source inode record into the target. The caller must call | |
515 | * __xfs_inobt_can_merge() to ensure the merge is valid. | |
516 | */ | |
517 | STATIC void | |
518 | __xfs_inobt_rec_merge( | |
519 | struct xfs_inobt_rec_incore *trec, /* target */ | |
520 | struct xfs_inobt_rec_incore *srec) /* src */ | |
521 | { | |
522 | ASSERT(trec->ir_startino == srec->ir_startino); | |
523 | ||
524 | /* combine the counts */ | |
525 | trec->ir_count += srec->ir_count; | |
526 | trec->ir_freecount += srec->ir_freecount; | |
527 | ||
528 | /* | |
529 | * Merge the holemask and free mask. For both fields, 0 bits refer to | |
530 | * allocated inodes. We combine the allocated ranges with bitwise AND. | |
531 | */ | |
532 | trec->ir_holemask &= srec->ir_holemask; | |
533 | trec->ir_free &= srec->ir_free; | |
534 | } | |
535 | ||
536 | /* | |
8541a7d9 CH |
537 | * Insert a new sparse inode chunk into the associated inode allocation btree. |
538 | * The inode record for the sparse chunk is pre-aligned to a startino that | |
539 | * should match any pre-existing sparse inode record in the tree. This allows | |
540 | * sparse chunks to fill over time. | |
56d1115c | 541 | * |
8541a7d9 CH |
542 | * If no preexisting record exists, the provided record is inserted. |
543 | * If there is a preexisting record, the provided record is merged with the | |
56d1115c | 544 | * existing record and updated in place. The merged record is returned in nrec. |
56d1115c BF |
545 | * |
546 | * It is considered corruption if a merge is requested and not possible. Given | |
547 | * the sparse inode alignment constraints, this should never happen. | |
548 | */ | |
549 | STATIC int | |
550 | xfs_inobt_insert_sprec( | |
dedab3e4 | 551 | struct xfs_perag *pag, |
56d1115c BF |
552 | struct xfs_trans *tp, |
553 | struct xfs_buf *agbp, | |
8541a7d9 | 554 | struct xfs_inobt_rec_incore *nrec) /* in/out: new/merged rec. */ |
56d1115c | 555 | { |
e9c4d8bf | 556 | struct xfs_mount *mp = pag_mount(pag); |
56d1115c | 557 | struct xfs_btree_cur *cur; |
56d1115c BF |
558 | int error; |
559 | int i; | |
560 | struct xfs_inobt_rec_incore rec; | |
561 | ||
14dd46cf | 562 | cur = xfs_inobt_init_cursor(pag, tp, agbp); |
56d1115c BF |
563 | |
564 | /* the new record is pre-aligned so we know where to look */ | |
565 | error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i); | |
566 | if (error) | |
567 | goto error; | |
568 | /* if nothing there, insert a new record and return */ | |
569 | if (i == 0) { | |
570 | error = xfs_inobt_insert_rec(cur, nrec->ir_holemask, | |
571 | nrec->ir_count, nrec->ir_freecount, | |
572 | nrec->ir_free, &i); | |
573 | if (error) | |
574 | goto error; | |
f9e03706 | 575 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
989d5ec3 | 576 | xfs_btree_mark_sick(cur); |
f9e03706 DW |
577 | error = -EFSCORRUPTED; |
578 | goto error; | |
579 | } | |
56d1115c BF |
580 | |
581 | goto out; | |
582 | } | |
583 | ||
584 | /* | |
8541a7d9 | 585 | * A record exists at this startino. Merge the records. |
56d1115c | 586 | */ |
8541a7d9 CH |
587 | error = xfs_inobt_get_rec(cur, &rec, &i); |
588 | if (error) | |
589 | goto error; | |
590 | if (XFS_IS_CORRUPT(mp, i != 1)) { | |
591 | xfs_btree_mark_sick(cur); | |
592 | error = -EFSCORRUPTED; | |
593 | goto error; | |
594 | } | |
595 | if (XFS_IS_CORRUPT(mp, rec.ir_startino != nrec->ir_startino)) { | |
596 | xfs_btree_mark_sick(cur); | |
597 | error = -EFSCORRUPTED; | |
598 | goto error; | |
599 | } | |
56d1115c | 600 | |
8541a7d9 CH |
601 | /* |
602 | * This should never fail. If we have coexisting records that | |
603 | * cannot merge, something is seriously wrong. | |
604 | */ | |
605 | if (XFS_IS_CORRUPT(mp, !__xfs_inobt_can_merge(nrec, &rec))) { | |
606 | xfs_btree_mark_sick(cur); | |
607 | error = -EFSCORRUPTED; | |
608 | goto error; | |
609 | } | |
56d1115c | 610 | |
487092ce | 611 | trace_xfs_irec_merge_pre(pag, &rec, nrec); |
56d1115c | 612 | |
8541a7d9 CH |
613 | /* merge to nrec to output the updated record */ |
614 | __xfs_inobt_rec_merge(nrec, &rec); | |
56d1115c | 615 | |
487092ce | 616 | trace_xfs_irec_merge_post(pag, nrec); |
56d1115c | 617 | |
8541a7d9 CH |
618 | error = xfs_inobt_rec_check_count(mp, nrec); |
619 | if (error) | |
620 | goto error; | |
56d1115c BF |
621 | |
622 | error = xfs_inobt_update(cur, nrec); | |
623 | if (error) | |
624 | goto error; | |
625 | ||
626 | out: | |
627 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
628 | return 0; | |
629 | error: | |
630 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
631 | return error; | |
632 | } | |
633 | ||
8541a7d9 CH |
634 | /* |
635 | * Insert a new sparse inode chunk into the free inode btree. The inode | |
636 | * record for the sparse chunk is pre-aligned to a startino that should match | |
637 | * any pre-existing sparse inode record in the tree. This allows sparse chunks | |
638 | * to fill over time. | |
639 | * | |
640 | * The new record is always inserted, overwriting a pre-existing record if | |
641 | * there is one. | |
642 | */ | |
643 | STATIC int | |
644 | xfs_finobt_insert_sprec( | |
645 | struct xfs_perag *pag, | |
646 | struct xfs_trans *tp, | |
647 | struct xfs_buf *agbp, | |
648 | struct xfs_inobt_rec_incore *nrec) /* in/out: new rec. */ | |
649 | { | |
e9c4d8bf | 650 | struct xfs_mount *mp = pag_mount(pag); |
8541a7d9 CH |
651 | struct xfs_btree_cur *cur; |
652 | int error; | |
653 | int i; | |
654 | ||
14dd46cf | 655 | cur = xfs_finobt_init_cursor(pag, tp, agbp); |
8541a7d9 CH |
656 | |
657 | /* the new record is pre-aligned so we know where to look */ | |
658 | error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i); | |
659 | if (error) | |
660 | goto error; | |
661 | /* if nothing there, insert a new record and return */ | |
662 | if (i == 0) { | |
663 | error = xfs_inobt_insert_rec(cur, nrec->ir_holemask, | |
664 | nrec->ir_count, nrec->ir_freecount, | |
665 | nrec->ir_free, &i); | |
666 | if (error) | |
667 | goto error; | |
668 | if (XFS_IS_CORRUPT(mp, i != 1)) { | |
669 | xfs_btree_mark_sick(cur); | |
670 | error = -EFSCORRUPTED; | |
671 | goto error; | |
672 | } | |
673 | } else { | |
674 | error = xfs_inobt_update(cur, nrec); | |
675 | if (error) | |
676 | goto error; | |
677 | } | |
678 | ||
679 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
680 | return 0; | |
681 | error: | |
682 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
683 | return error; | |
684 | } | |
685 | ||
686 | ||
1da177e4 | 687 | /* |
8237fbf5 DC |
688 | * Allocate new inodes in the allocation group specified by agbp. Returns 0 if |
689 | * inodes were allocated in this AG; -EAGAIN if there was no space in this AG so | |
690 | * the caller knows it can try another AG, a hard -ENOSPC when over the maximum | |
691 | * inode count threshold, or the usual negative error code for other errors. | |
1da177e4 | 692 | */ |
ef325959 | 693 | STATIC int |
1da177e4 | 694 | xfs_ialloc_ag_alloc( |
dedab3e4 | 695 | struct xfs_perag *pag, |
ef325959 | 696 | struct xfs_trans *tp, |
dedab3e4 | 697 | struct xfs_buf *agbp) |
1da177e4 | 698 | { |
ef325959 DW |
699 | struct xfs_agi *agi; |
700 | struct xfs_alloc_arg args; | |
ef325959 DW |
701 | int error; |
702 | xfs_agino_t newino; /* new first inode's number */ | |
703 | xfs_agino_t newlen; /* new number of inodes */ | |
704 | int isaligned = 0; /* inode allocation at stripe */ | |
705 | /* unit boundary */ | |
706 | /* init. to full chunk */ | |
56d1115c | 707 | struct xfs_inobt_rec_incore rec; |
ef325959 | 708 | struct xfs_ino_geometry *igeo = M_IGEO(tp->t_mountp); |
7b13c515 | 709 | uint16_t allocmask = (uint16_t) -1; |
ef325959 | 710 | int do_sparse = 0; |
1cdadee1 | 711 | |
a0041684 | 712 | memset(&args, 0, sizeof(args)); |
1da177e4 LT |
713 | args.tp = tp; |
714 | args.mp = tp->t_mountp; | |
1cdadee1 | 715 | args.fsbno = NULLFSBLOCK; |
7280feda | 716 | args.oinfo = XFS_RMAP_OINFO_INODES; |
74c36a86 | 717 | args.pag = pag; |
1da177e4 | 718 | |
46fc58da BF |
719 | #ifdef DEBUG |
720 | /* randomly do sparse inode allocations */ | |
ebd9027d | 721 | if (xfs_has_sparseinodes(tp->t_mountp) && |
ef325959 | 722 | igeo->ialloc_min_blks < igeo->ialloc_blks) |
8032bf12 | 723 | do_sparse = get_random_u32_below(2); |
46fc58da BF |
724 | #endif |
725 | ||
1da177e4 LT |
726 | /* |
727 | * Locking will ensure that we don't have two callers in here | |
728 | * at one time. | |
729 | */ | |
ef325959 DW |
730 | newlen = igeo->ialloc_inos; |
731 | if (igeo->maxicount && | |
74f9ce1c | 732 | percpu_counter_read_positive(&args.mp->m_icount) + newlen > |
ef325959 | 733 | igeo->maxicount) |
2451337d | 734 | return -ENOSPC; |
ef325959 | 735 | args.minlen = args.maxlen = igeo->ialloc_blks; |
1da177e4 | 736 | /* |
3ccb8b5f GO |
737 | * First try to allocate inodes contiguous with the last-allocated |
738 | * chunk of inodes. If the filesystem is striped, this will fill | |
739 | * an entire stripe unit with inodes. | |
28c8e41a | 740 | */ |
370c782b | 741 | agi = agbp->b_addr; |
3ccb8b5f | 742 | newino = be32_to_cpu(agi->agi_newino); |
019ff2d5 | 743 | args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + |
ef325959 | 744 | igeo->ialloc_blks; |
1cdadee1 BF |
745 | if (do_sparse) |
746 | goto sparse_alloc; | |
019ff2d5 NS |
747 | if (likely(newino != NULLAGINO && |
748 | (args.agbno < be32_to_cpu(agi->agi_length)))) { | |
3ccb8b5f | 749 | args.prod = 1; |
75de2a91 | 750 | |
3ccb8b5f | 751 | /* |
75de2a91 DC |
752 | * We need to take into account alignment here to ensure that |
753 | * we don't modify the free list if we fail to have an exact | |
754 | * block. If we don't have an exact match, and every oher | |
755 | * attempt allocation attempt fails, we'll end up cancelling | |
756 | * a dirty transaction and shutting down. | |
757 | * | |
758 | * For an exact allocation, alignment must be 1, | |
759 | * however we need to take cluster alignment into account when | |
760 | * fixing up the freelist. Use the minalignslop field to | |
761 | * indicate that extra blocks might be required for alignment, | |
762 | * but not to use them in the actual exact allocation. | |
3ccb8b5f | 763 | */ |
75de2a91 | 764 | args.alignment = 1; |
ef325959 | 765 | args.minalignslop = igeo->cluster_align - 1; |
75de2a91 DC |
766 | |
767 | /* Allow space for the inode btree to split. */ | |
657f1019 | 768 | args.minleft = igeo->inobt_maxlevels; |
5f36b2ce | 769 | error = xfs_alloc_vextent_exact_bno(&args, |
856a920a | 770 | xfs_agbno_to_fsb(pag, args.agbno)); |
74c36a86 | 771 | if (error) |
3ccb8b5f | 772 | return error; |
e480a723 BF |
773 | |
774 | /* | |
775 | * This request might have dirtied the transaction if the AG can | |
776 | * satisfy the request, but the exact block was not available. | |
777 | * If the allocation did fail, subsequent requests will relax | |
778 | * the exact agbno requirement and increase the alignment | |
779 | * instead. It is critical that the total size of the request | |
780 | * (len + alignment + slop) does not increase from this point | |
781 | * on, so reset minalignslop to ensure it is not included in | |
782 | * subsequent requests. | |
783 | */ | |
784 | args.minalignslop = 0; | |
1cdadee1 | 785 | } |
1da177e4 | 786 | |
3ccb8b5f GO |
787 | if (unlikely(args.fsbno == NULLFSBLOCK)) { |
788 | /* | |
789 | * Set the alignment for the allocation. | |
790 | * If stripe alignment is turned on then align at stripe unit | |
791 | * boundary. | |
019ff2d5 NS |
792 | * If the cluster size is smaller than a filesystem block |
793 | * then we're doing I/O for inodes in filesystem block size | |
3ccb8b5f GO |
794 | * pieces, so don't need alignment anyway. |
795 | */ | |
796 | isaligned = 0; | |
ef325959 | 797 | if (igeo->ialloc_align) { |
0560f31a | 798 | ASSERT(!xfs_has_noalign(args.mp)); |
3ccb8b5f GO |
799 | args.alignment = args.mp->m_dalign; |
800 | isaligned = 1; | |
75de2a91 | 801 | } else |
ef325959 | 802 | args.alignment = igeo->cluster_align; |
3ccb8b5f GO |
803 | /* |
804 | * Allocate a fixed-size extent of inodes. | |
805 | */ | |
3ccb8b5f GO |
806 | args.prod = 1; |
807 | /* | |
808 | * Allow space for the inode btree to split. | |
809 | */ | |
657f1019 | 810 | args.minleft = igeo->inobt_maxlevels; |
db4710fd | 811 | error = xfs_alloc_vextent_near_bno(&args, |
856a920a CH |
812 | xfs_agbno_to_fsb(pag, |
813 | be32_to_cpu(agi->agi_root))); | |
74c36a86 | 814 | if (error) |
3ccb8b5f GO |
815 | return error; |
816 | } | |
019ff2d5 | 817 | |
1da177e4 LT |
818 | /* |
819 | * If stripe alignment is turned on, then try again with cluster | |
820 | * alignment. | |
821 | */ | |
822 | if (isaligned && args.fsbno == NULLFSBLOCK) { | |
ef325959 | 823 | args.alignment = igeo->cluster_align; |
db4710fd | 824 | error = xfs_alloc_vextent_near_bno(&args, |
856a920a CH |
825 | xfs_agbno_to_fsb(pag, |
826 | be32_to_cpu(agi->agi_root))); | |
db4710fd | 827 | if (error) |
1da177e4 LT |
828 | return error; |
829 | } | |
830 | ||
56d1115c BF |
831 | /* |
832 | * Finally, try a sparse allocation if the filesystem supports it and | |
833 | * the sparse allocation length is smaller than a full chunk. | |
834 | */ | |
ebd9027d | 835 | if (xfs_has_sparseinodes(args.mp) && |
ef325959 | 836 | igeo->ialloc_min_blks < igeo->ialloc_blks && |
56d1115c | 837 | args.fsbno == NULLFSBLOCK) { |
1cdadee1 | 838 | sparse_alloc: |
56d1115c BF |
839 | args.alignment = args.mp->m_sb.sb_spino_align; |
840 | args.prod = 1; | |
841 | ||
ef325959 | 842 | args.minlen = igeo->ialloc_min_blks; |
56d1115c BF |
843 | args.maxlen = args.minlen; |
844 | ||
845 | /* | |
846 | * The inode record will be aligned to full chunk size. We must | |
847 | * prevent sparse allocation from AG boundaries that result in | |
848 | * invalid inode records, such as records that start at agbno 0 | |
849 | * or extend beyond the AG. | |
850 | * | |
851 | * Set min agbno to the first aligned, non-zero agbno and max to | |
852 | * the last aligned agbno that is at least one full chunk from | |
853 | * the end of the AG. | |
854 | */ | |
855 | args.min_agbno = args.mp->m_sb.sb_inoalignmt; | |
13325333 DC |
856 | args.max_agbno = round_down(xfs_ag_block_count(args.mp, |
857 | pag_agno(pag)), | |
56d1115c | 858 | args.mp->m_sb.sb_inoalignmt) - |
ef325959 | 859 | igeo->ialloc_blks; |
56d1115c | 860 | |
db4710fd | 861 | error = xfs_alloc_vextent_near_bno(&args, |
856a920a CH |
862 | xfs_agbno_to_fsb(pag, |
863 | be32_to_cpu(agi->agi_root))); | |
56d1115c BF |
864 | if (error) |
865 | return error; | |
866 | ||
43004b2a | 867 | newlen = XFS_AGB_TO_AGINO(args.mp, args.len); |
46fc58da | 868 | ASSERT(newlen <= XFS_INODES_PER_CHUNK); |
56d1115c BF |
869 | allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1; |
870 | } | |
871 | ||
3937493c | 872 | if (args.fsbno == NULLFSBLOCK) |
8237fbf5 | 873 | return -EAGAIN; |
3937493c | 874 | |
1da177e4 | 875 | ASSERT(args.len == args.minlen); |
1da177e4 | 876 | |
359346a9 | 877 | /* |
85c0b2ab DC |
878 | * Stamp and write the inode buffers. |
879 | * | |
359346a9 DC |
880 | * Seed the new inode cluster with a random generation number. This |
881 | * prevents short-term reuse of generation numbers if a chunk is | |
882 | * freed and then immediately reallocated. We use random numbers | |
883 | * rather than a linear progression to prevent the next generation | |
884 | * number from being easily guessable. | |
885 | */ | |
e9c4d8bf | 886 | error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, pag_agno(pag), |
a251c17a | 887 | args.agbno, args.len, get_random_u32()); |
d42f08f6 | 888 | |
2a30f36d CS |
889 | if (error) |
890 | return error; | |
85c0b2ab DC |
891 | /* |
892 | * Convert the results. | |
893 | */ | |
43004b2a | 894 | newino = XFS_AGB_TO_AGINO(args.mp, args.agbno); |
56d1115c BF |
895 | |
896 | if (xfs_inobt_issparse(~allocmask)) { | |
897 | /* | |
898 | * We've allocated a sparse chunk. Align the startino and mask. | |
899 | */ | |
900 | xfs_align_sparse_ino(args.mp, &newino, &allocmask); | |
901 | ||
902 | rec.ir_startino = newino; | |
903 | rec.ir_holemask = ~allocmask; | |
904 | rec.ir_count = newlen; | |
905 | rec.ir_freecount = newlen; | |
906 | rec.ir_free = XFS_INOBT_ALL_FREE; | |
907 | ||
908 | /* | |
909 | * Insert the sparse record into the inobt and allow for a merge | |
910 | * if necessary. If a merge does occur, rec is updated to the | |
911 | * merged record. | |
912 | */ | |
8541a7d9 | 913 | error = xfs_inobt_insert_sprec(pag, tp, agbp, &rec); |
56d1115c BF |
914 | if (error == -EFSCORRUPTED) { |
915 | xfs_alert(args.mp, | |
916 | "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u", | |
6abd82ab | 917 | xfs_agino_to_ino(pag, rec.ir_startino), |
56d1115c BF |
918 | rec.ir_holemask, rec.ir_count); |
919 | xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE); | |
920 | } | |
921 | if (error) | |
922 | return error; | |
923 | ||
924 | /* | |
925 | * We can't merge the part we've just allocated as for the inobt | |
926 | * due to finobt semantics. The original record may or may not | |
927 | * exist independent of whether physical inodes exist in this | |
928 | * sparse chunk. | |
929 | * | |
930 | * We must update the finobt record based on the inobt record. | |
931 | * rec contains the fully merged and up to date inobt record | |
932 | * from the previous call. Set merge false to replace any | |
933 | * existing record with this one. | |
934 | */ | |
ebd9027d | 935 | if (xfs_has_finobt(args.mp)) { |
8541a7d9 | 936 | error = xfs_finobt_insert_sprec(pag, tp, agbp, &rec); |
56d1115c BF |
937 | if (error) |
938 | return error; | |
939 | } | |
940 | } else { | |
941 | /* full chunk - insert new records to both btrees */ | |
fbeef4e0 | 942 | error = xfs_inobt_insert(pag, tp, agbp, newino, newlen, false); |
56d1115c BF |
943 | if (error) |
944 | return error; | |
945 | ||
ebd9027d | 946 | if (xfs_has_finobt(args.mp)) { |
dedab3e4 | 947 | error = xfs_inobt_insert(pag, tp, agbp, newino, |
fbeef4e0 | 948 | newlen, true); |
56d1115c BF |
949 | if (error) |
950 | return error; | |
951 | } | |
952 | } | |
953 | ||
954 | /* | |
955 | * Update AGI counts and newino. | |
956 | */ | |
413d57c9 MS |
957 | be32_add_cpu(&agi->agi_count, newlen); |
958 | be32_add_cpu(&agi->agi_freecount, newlen); | |
44b56e0a | 959 | pag->pagi_freecount += newlen; |
89e9b5c0 | 960 | pag->pagi_count += newlen; |
16259e7d | 961 | agi->agi_newino = cpu_to_be32(newino); |
85c0b2ab | 962 | |
1da177e4 LT |
963 | /* |
964 | * Log allocation group header fields | |
965 | */ | |
966 | xfs_ialloc_log_agi(tp, agbp, | |
967 | XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); | |
968 | /* | |
969 | * Modify/log superblock values for inode count and inode free count. | |
970 | */ | |
971 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); | |
972 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); | |
1da177e4 LT |
973 | return 0; |
974 | } | |
975 | ||
4254b0bb CH |
976 | /* |
977 | * Try to retrieve the next record to the left/right from the current one. | |
978 | */ | |
979 | STATIC int | |
980 | xfs_ialloc_next_rec( | |
981 | struct xfs_btree_cur *cur, | |
982 | xfs_inobt_rec_incore_t *rec, | |
983 | int *done, | |
984 | int left) | |
985 | { | |
986 | int error; | |
987 | int i; | |
988 | ||
989 | if (left) | |
990 | error = xfs_btree_decrement(cur, 0, &i); | |
991 | else | |
992 | error = xfs_btree_increment(cur, 0, &i); | |
993 | ||
994 | if (error) | |
995 | return error; | |
996 | *done = !i; | |
997 | if (i) { | |
998 | error = xfs_inobt_get_rec(cur, rec, &i); | |
999 | if (error) | |
1000 | return error; | |
989d5ec3 DW |
1001 | if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { |
1002 | xfs_btree_mark_sick(cur); | |
f9e03706 | 1003 | return -EFSCORRUPTED; |
989d5ec3 | 1004 | } |
4254b0bb CH |
1005 | } |
1006 | ||
1007 | return 0; | |
1008 | } | |
1009 | ||
bd169565 DC |
1010 | STATIC int |
1011 | xfs_ialloc_get_rec( | |
1012 | struct xfs_btree_cur *cur, | |
1013 | xfs_agino_t agino, | |
1014 | xfs_inobt_rec_incore_t *rec, | |
43df2ee6 | 1015 | int *done) |
bd169565 DC |
1016 | { |
1017 | int error; | |
1018 | int i; | |
1019 | ||
1020 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i); | |
1021 | if (error) | |
1022 | return error; | |
1023 | *done = !i; | |
1024 | if (i) { | |
1025 | error = xfs_inobt_get_rec(cur, rec, &i); | |
1026 | if (error) | |
1027 | return error; | |
989d5ec3 DW |
1028 | if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { |
1029 | xfs_btree_mark_sick(cur); | |
f9e03706 | 1030 | return -EFSCORRUPTED; |
989d5ec3 | 1031 | } |
bd169565 DC |
1032 | } |
1033 | ||
1034 | return 0; | |
1035 | } | |
0b48db80 | 1036 | |
d4cc540b | 1037 | /* |
26dd5217 BF |
1038 | * Return the offset of the first free inode in the record. If the inode chunk |
1039 | * is sparsely allocated, we convert the record holemask to inode granularity | |
1040 | * and mask off the unallocated regions from the inode free mask. | |
d4cc540b BF |
1041 | */ |
1042 | STATIC int | |
1043 | xfs_inobt_first_free_inode( | |
1044 | struct xfs_inobt_rec_incore *rec) | |
1045 | { | |
26dd5217 BF |
1046 | xfs_inofree_t realfree; |
1047 | ||
1048 | /* if there are no holes, return the first available offset */ | |
1049 | if (!xfs_inobt_issparse(rec->ir_holemask)) | |
1050 | return xfs_lowbit64(rec->ir_free); | |
1051 | ||
1052 | realfree = xfs_inobt_irec_to_allocmask(rec); | |
1053 | realfree &= rec->ir_free; | |
1054 | ||
1055 | return xfs_lowbit64(realfree); | |
d4cc540b BF |
1056 | } |
1057 | ||
2935213a DW |
1058 | /* |
1059 | * If this AG has corrupt inodes, check if allocating this inode would fail | |
1060 | * with corruption errors. Returns 0 if we're clear, or EAGAIN to try again | |
1061 | * somewhere else. | |
1062 | */ | |
1063 | static int | |
1064 | xfs_dialloc_check_ino( | |
1065 | struct xfs_perag *pag, | |
1066 | struct xfs_trans *tp, | |
1067 | xfs_ino_t ino) | |
1068 | { | |
1069 | struct xfs_imap imap; | |
1070 | struct xfs_buf *bp; | |
1071 | int error; | |
1072 | ||
1073 | error = xfs_imap(pag, tp, ino, &imap, 0); | |
1074 | if (error) | |
1075 | return -EAGAIN; | |
1076 | ||
e9c4d8bf | 1077 | error = xfs_imap_to_bp(pag_mount(pag), tp, &imap, &bp); |
2935213a DW |
1078 | if (error) |
1079 | return -EAGAIN; | |
1080 | ||
1081 | xfs_trans_brelse(tp, bp); | |
1082 | return 0; | |
1083 | } | |
1084 | ||
1da177e4 | 1085 | /* |
6dd8638e | 1086 | * Allocate an inode using the inobt-only algorithm. |
1da177e4 | 1087 | */ |
f2ecc5e4 | 1088 | STATIC int |
6dd8638e | 1089 | xfs_dialloc_ag_inobt( |
dedab3e4 | 1090 | struct xfs_perag *pag, |
f2ecc5e4 CH |
1091 | struct xfs_trans *tp, |
1092 | struct xfs_buf *agbp, | |
1093 | xfs_ino_t parent, | |
1094 | xfs_ino_t *inop) | |
1da177e4 | 1095 | { |
f2ecc5e4 | 1096 | struct xfs_mount *mp = tp->t_mountp; |
370c782b | 1097 | struct xfs_agi *agi = agbp->b_addr; |
f2ecc5e4 CH |
1098 | xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); |
1099 | xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); | |
f2ecc5e4 CH |
1100 | struct xfs_btree_cur *cur, *tcur; |
1101 | struct xfs_inobt_rec_incore rec, trec; | |
1102 | xfs_ino_t ino; | |
1103 | int error; | |
1104 | int offset; | |
1105 | int i, j; | |
2d32311c | 1106 | int searchdistance = 10; |
1da177e4 | 1107 | |
7ac2ff8b DC |
1108 | ASSERT(xfs_perag_initialised_agi(pag)); |
1109 | ASSERT(xfs_perag_allows_inodes(pag)); | |
4bb61069 CH |
1110 | ASSERT(pag->pagi_freecount > 0); |
1111 | ||
bd169565 | 1112 | restart_pagno: |
14dd46cf | 1113 | cur = xfs_inobt_init_cursor(pag, tp, agbp); |
1da177e4 LT |
1114 | /* |
1115 | * If pagino is 0 (this is the root inode allocation) use newino. | |
1116 | * This must work because we've just allocated some. | |
1117 | */ | |
1118 | if (!pagino) | |
16259e7d | 1119 | pagino = be32_to_cpu(agi->agi_newino); |
1da177e4 | 1120 | |
9ba0889e | 1121 | error = xfs_check_agi_freecount(cur); |
0b48db80 DC |
1122 | if (error) |
1123 | goto error0; | |
1da177e4 | 1124 | |
1da177e4 | 1125 | /* |
4254b0bb | 1126 | * If in the same AG as the parent, try to get near the parent. |
1da177e4 | 1127 | */ |
e9c4d8bf | 1128 | if (pagno == pag_agno(pag)) { |
4254b0bb CH |
1129 | int doneleft; /* done, to the left */ |
1130 | int doneright; /* done, to the right */ | |
1131 | ||
21875505 | 1132 | error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i); |
4254b0bb | 1133 | if (error) |
1da177e4 | 1134 | goto error0; |
f9e03706 | 1135 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
989d5ec3 | 1136 | xfs_btree_mark_sick(cur); |
f9e03706 DW |
1137 | error = -EFSCORRUPTED; |
1138 | goto error0; | |
1139 | } | |
4254b0bb CH |
1140 | |
1141 | error = xfs_inobt_get_rec(cur, &rec, &j); | |
1142 | if (error) | |
1143 | goto error0; | |
f9e03706 | 1144 | if (XFS_IS_CORRUPT(mp, j != 1)) { |
989d5ec3 | 1145 | xfs_btree_mark_sick(cur); |
f9e03706 DW |
1146 | error = -EFSCORRUPTED; |
1147 | goto error0; | |
1148 | } | |
4254b0bb CH |
1149 | |
1150 | if (rec.ir_freecount > 0) { | |
1da177e4 LT |
1151 | /* |
1152 | * Found a free inode in the same chunk | |
4254b0bb | 1153 | * as the parent, done. |
1da177e4 | 1154 | */ |
4254b0bb | 1155 | goto alloc_inode; |
1da177e4 | 1156 | } |
4254b0bb CH |
1157 | |
1158 | ||
1da177e4 | 1159 | /* |
4254b0bb | 1160 | * In the same AG as parent, but parent's chunk is full. |
1da177e4 | 1161 | */ |
1da177e4 | 1162 | |
4254b0bb CH |
1163 | /* duplicate the cursor, search left & right simultaneously */ |
1164 | error = xfs_btree_dup_cursor(cur, &tcur); | |
1165 | if (error) | |
1166 | goto error0; | |
1167 | ||
bd169565 DC |
1168 | /* |
1169 | * Skip to last blocks looked up if same parent inode. | |
1170 | */ | |
1171 | if (pagino != NULLAGINO && | |
1172 | pag->pagl_pagino == pagino && | |
1173 | pag->pagl_leftrec != NULLAGINO && | |
1174 | pag->pagl_rightrec != NULLAGINO) { | |
1175 | error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec, | |
43df2ee6 | 1176 | &trec, &doneleft); |
bd169565 DC |
1177 | if (error) |
1178 | goto error1; | |
4254b0bb | 1179 | |
bd169565 | 1180 | error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec, |
43df2ee6 | 1181 | &rec, &doneright); |
bd169565 DC |
1182 | if (error) |
1183 | goto error1; | |
1184 | } else { | |
1185 | /* search left with tcur, back up 1 record */ | |
1186 | error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1); | |
1187 | if (error) | |
1188 | goto error1; | |
1189 | ||
1190 | /* search right with cur, go forward 1 record. */ | |
1191 | error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0); | |
1192 | if (error) | |
1193 | goto error1; | |
1194 | } | |
4254b0bb CH |
1195 | |
1196 | /* | |
1197 | * Loop until we find an inode chunk with a free inode. | |
1198 | */ | |
2d32311c | 1199 | while (--searchdistance > 0 && (!doneleft || !doneright)) { |
4254b0bb CH |
1200 | int useleft; /* using left inode chunk this time */ |
1201 | ||
1202 | /* figure out the closer block if both are valid. */ | |
1203 | if (!doneleft && !doneright) { | |
1204 | useleft = pagino - | |
1205 | (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) < | |
1206 | rec.ir_startino - pagino; | |
1207 | } else { | |
1208 | useleft = !doneleft; | |
1da177e4 | 1209 | } |
4254b0bb CH |
1210 | |
1211 | /* free inodes to the left? */ | |
1212 | if (useleft && trec.ir_freecount) { | |
4254b0bb CH |
1213 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
1214 | cur = tcur; | |
bd169565 DC |
1215 | |
1216 | pag->pagl_leftrec = trec.ir_startino; | |
1217 | pag->pagl_rightrec = rec.ir_startino; | |
1218 | pag->pagl_pagino = pagino; | |
c44245b3 | 1219 | rec = trec; |
4254b0bb | 1220 | goto alloc_inode; |
1da177e4 | 1221 | } |
1da177e4 | 1222 | |
4254b0bb CH |
1223 | /* free inodes to the right? */ |
1224 | if (!useleft && rec.ir_freecount) { | |
1225 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); | |
bd169565 DC |
1226 | |
1227 | pag->pagl_leftrec = trec.ir_startino; | |
1228 | pag->pagl_rightrec = rec.ir_startino; | |
1229 | pag->pagl_pagino = pagino; | |
4254b0bb | 1230 | goto alloc_inode; |
1da177e4 | 1231 | } |
4254b0bb CH |
1232 | |
1233 | /* get next record to check */ | |
1234 | if (useleft) { | |
1235 | error = xfs_ialloc_next_rec(tcur, &trec, | |
1236 | &doneleft, 1); | |
1237 | } else { | |
1238 | error = xfs_ialloc_next_rec(cur, &rec, | |
1239 | &doneright, 0); | |
1240 | } | |
1241 | if (error) | |
1242 | goto error1; | |
1da177e4 | 1243 | } |
bd169565 | 1244 | |
2d32311c CM |
1245 | if (searchdistance <= 0) { |
1246 | /* | |
1247 | * Not in range - save last search | |
1248 | * location and allocate a new inode | |
1249 | */ | |
1250 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); | |
1251 | pag->pagl_leftrec = trec.ir_startino; | |
1252 | pag->pagl_rightrec = rec.ir_startino; | |
1253 | pag->pagl_pagino = pagino; | |
1254 | ||
1255 | } else { | |
1256 | /* | |
1257 | * We've reached the end of the btree. because | |
1258 | * we are only searching a small chunk of the | |
1259 | * btree each search, there is obviously free | |
1260 | * inodes closer to the parent inode than we | |
1261 | * are now. restart the search again. | |
1262 | */ | |
1263 | pag->pagl_pagino = NULLAGINO; | |
1264 | pag->pagl_leftrec = NULLAGINO; | |
1265 | pag->pagl_rightrec = NULLAGINO; | |
1266 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); | |
1267 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
1268 | goto restart_pagno; | |
1269 | } | |
1da177e4 | 1270 | } |
4254b0bb | 1271 | |
1da177e4 | 1272 | /* |
4254b0bb | 1273 | * In a different AG from the parent. |
1da177e4 LT |
1274 | * See if the most recently allocated block has any free. |
1275 | */ | |
69ef921b | 1276 | if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { |
21875505 CH |
1277 | error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), |
1278 | XFS_LOOKUP_EQ, &i); | |
4254b0bb | 1279 | if (error) |
1da177e4 | 1280 | goto error0; |
4254b0bb CH |
1281 | |
1282 | if (i == 1) { | |
1283 | error = xfs_inobt_get_rec(cur, &rec, &j); | |
1284 | if (error) | |
1285 | goto error0; | |
1286 | ||
1287 | if (j == 1 && rec.ir_freecount > 0) { | |
1288 | /* | |
1289 | * The last chunk allocated in the group | |
1290 | * still has a free inode. | |
1291 | */ | |
1292 | goto alloc_inode; | |
1293 | } | |
1da177e4 | 1294 | } |
bd169565 | 1295 | } |
4254b0bb | 1296 | |
bd169565 DC |
1297 | /* |
1298 | * None left in the last group, search the whole AG | |
1299 | */ | |
1300 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); | |
1301 | if (error) | |
1302 | goto error0; | |
f9e03706 | 1303 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
989d5ec3 | 1304 | xfs_btree_mark_sick(cur); |
f9e03706 DW |
1305 | error = -EFSCORRUPTED; |
1306 | goto error0; | |
1307 | } | |
bd169565 DC |
1308 | |
1309 | for (;;) { | |
1310 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
1311 | if (error) | |
1312 | goto error0; | |
f9e03706 | 1313 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
989d5ec3 | 1314 | xfs_btree_mark_sick(cur); |
f9e03706 DW |
1315 | error = -EFSCORRUPTED; |
1316 | goto error0; | |
1317 | } | |
bd169565 DC |
1318 | if (rec.ir_freecount > 0) |
1319 | break; | |
1320 | error = xfs_btree_increment(cur, 0, &i); | |
4254b0bb CH |
1321 | if (error) |
1322 | goto error0; | |
f9e03706 | 1323 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
989d5ec3 | 1324 | xfs_btree_mark_sick(cur); |
f9e03706 DW |
1325 | error = -EFSCORRUPTED; |
1326 | goto error0; | |
1327 | } | |
1da177e4 | 1328 | } |
4254b0bb CH |
1329 | |
1330 | alloc_inode: | |
d4cc540b | 1331 | offset = xfs_inobt_first_free_inode(&rec); |
1da177e4 LT |
1332 | ASSERT(offset >= 0); |
1333 | ASSERT(offset < XFS_INODES_PER_CHUNK); | |
1334 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % | |
1335 | XFS_INODES_PER_CHUNK) == 0); | |
6abd82ab | 1336 | ino = xfs_agino_to_ino(pag, rec.ir_startino + offset); |
2935213a DW |
1337 | |
1338 | if (xfs_ag_has_sickness(pag, XFS_SICK_AG_INODES)) { | |
1339 | error = xfs_dialloc_check_ino(pag, tp, ino); | |
1340 | if (error) | |
1341 | goto error0; | |
1342 | } | |
1343 | ||
0d87e656 | 1344 | rec.ir_free &= ~XFS_INOBT_MASK(offset); |
1da177e4 | 1345 | rec.ir_freecount--; |
afabc24a CH |
1346 | error = xfs_inobt_update(cur, &rec); |
1347 | if (error) | |
1da177e4 | 1348 | goto error0; |
413d57c9 | 1349 | be32_add_cpu(&agi->agi_freecount, -1); |
1da177e4 | 1350 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
44b56e0a | 1351 | pag->pagi_freecount--; |
1da177e4 | 1352 | |
9ba0889e | 1353 | error = xfs_check_agi_freecount(cur); |
0b48db80 DC |
1354 | if (error) |
1355 | goto error0; | |
1356 | ||
1da177e4 LT |
1357 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
1358 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); | |
1359 | *inop = ino; | |
1360 | return 0; | |
1361 | error1: | |
1362 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); | |
1363 | error0: | |
1364 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
1365 | return error; | |
1366 | } | |
1367 | ||
6dd8638e BF |
1368 | /* |
1369 | * Use the free inode btree to allocate an inode based on distance from the | |
1370 | * parent. Note that the provided cursor may be deleted and replaced. | |
1371 | */ | |
1372 | STATIC int | |
1373 | xfs_dialloc_ag_finobt_near( | |
1374 | xfs_agino_t pagino, | |
1375 | struct xfs_btree_cur **ocur, | |
1376 | struct xfs_inobt_rec_incore *rec) | |
1377 | { | |
1378 | struct xfs_btree_cur *lcur = *ocur; /* left search cursor */ | |
1379 | struct xfs_btree_cur *rcur; /* right search cursor */ | |
1380 | struct xfs_inobt_rec_incore rrec; | |
1381 | int error; | |
1382 | int i, j; | |
1383 | ||
1384 | error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i); | |
1385 | if (error) | |
1386 | return error; | |
1387 | ||
1388 | if (i == 1) { | |
1389 | error = xfs_inobt_get_rec(lcur, rec, &i); | |
1390 | if (error) | |
1391 | return error; | |
989d5ec3 DW |
1392 | if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1)) { |
1393 | xfs_btree_mark_sick(lcur); | |
f9e03706 | 1394 | return -EFSCORRUPTED; |
989d5ec3 | 1395 | } |
6dd8638e BF |
1396 | |
1397 | /* | |
1398 | * See if we've landed in the parent inode record. The finobt | |
1399 | * only tracks chunks with at least one free inode, so record | |
1400 | * existence is enough. | |
1401 | */ | |
1402 | if (pagino >= rec->ir_startino && | |
1403 | pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK)) | |
1404 | return 0; | |
1405 | } | |
1406 | ||
1407 | error = xfs_btree_dup_cursor(lcur, &rcur); | |
1408 | if (error) | |
1409 | return error; | |
1410 | ||
1411 | error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j); | |
1412 | if (error) | |
1413 | goto error_rcur; | |
1414 | if (j == 1) { | |
1415 | error = xfs_inobt_get_rec(rcur, &rrec, &j); | |
1416 | if (error) | |
1417 | goto error_rcur; | |
f9e03706 | 1418 | if (XFS_IS_CORRUPT(lcur->bc_mp, j != 1)) { |
989d5ec3 | 1419 | xfs_btree_mark_sick(lcur); |
f9e03706 DW |
1420 | error = -EFSCORRUPTED; |
1421 | goto error_rcur; | |
1422 | } | |
6dd8638e BF |
1423 | } |
1424 | ||
f9e03706 | 1425 | if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1 && j != 1)) { |
989d5ec3 | 1426 | xfs_btree_mark_sick(lcur); |
f9e03706 DW |
1427 | error = -EFSCORRUPTED; |
1428 | goto error_rcur; | |
1429 | } | |
6dd8638e BF |
1430 | if (i == 1 && j == 1) { |
1431 | /* | |
1432 | * Both the left and right records are valid. Choose the closer | |
1433 | * inode chunk to the target. | |
1434 | */ | |
1435 | if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) > | |
1436 | (rrec.ir_startino - pagino)) { | |
1437 | *rec = rrec; | |
1438 | xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); | |
1439 | *ocur = rcur; | |
1440 | } else { | |
1441 | xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); | |
1442 | } | |
1443 | } else if (j == 1) { | |
1444 | /* only the right record is valid */ | |
1445 | *rec = rrec; | |
1446 | xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); | |
1447 | *ocur = rcur; | |
1448 | } else if (i == 1) { | |
1449 | /* only the left record is valid */ | |
1450 | xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); | |
1451 | } | |
1452 | ||
1453 | return 0; | |
1454 | ||
1455 | error_rcur: | |
1456 | xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR); | |
1457 | return error; | |
1458 | } | |
1459 | ||
1460 | /* | |
1461 | * Use the free inode btree to find a free inode based on a newino hint. If | |
1462 | * the hint is NULL, find the first free inode in the AG. | |
1463 | */ | |
1464 | STATIC int | |
1465 | xfs_dialloc_ag_finobt_newino( | |
1466 | struct xfs_agi *agi, | |
1467 | struct xfs_btree_cur *cur, | |
1468 | struct xfs_inobt_rec_incore *rec) | |
1469 | { | |
1470 | int error; | |
1471 | int i; | |
1472 | ||
1473 | if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { | |
e68ed775 DC |
1474 | error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), |
1475 | XFS_LOOKUP_EQ, &i); | |
6dd8638e BF |
1476 | if (error) |
1477 | return error; | |
1478 | if (i == 1) { | |
1479 | error = xfs_inobt_get_rec(cur, rec, &i); | |
1480 | if (error) | |
1481 | return error; | |
989d5ec3 DW |
1482 | if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { |
1483 | xfs_btree_mark_sick(cur); | |
f9e03706 | 1484 | return -EFSCORRUPTED; |
989d5ec3 | 1485 | } |
6dd8638e BF |
1486 | return 0; |
1487 | } | |
1488 | } | |
1489 | ||
1490 | /* | |
1491 | * Find the first inode available in the AG. | |
1492 | */ | |
1493 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); | |
1494 | if (error) | |
1495 | return error; | |
989d5ec3 DW |
1496 | if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { |
1497 | xfs_btree_mark_sick(cur); | |
f9e03706 | 1498 | return -EFSCORRUPTED; |
989d5ec3 | 1499 | } |
6dd8638e BF |
1500 | |
1501 | error = xfs_inobt_get_rec(cur, rec, &i); | |
1502 | if (error) | |
1503 | return error; | |
989d5ec3 DW |
1504 | if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { |
1505 | xfs_btree_mark_sick(cur); | |
f9e03706 | 1506 | return -EFSCORRUPTED; |
989d5ec3 | 1507 | } |
6dd8638e BF |
1508 | |
1509 | return 0; | |
1510 | } | |
1511 | ||
1512 | /* | |
1513 | * Update the inobt based on a modification made to the finobt. Also ensure that | |
1514 | * the records from both trees are equivalent post-modification. | |
1515 | */ | |
1516 | STATIC int | |
1517 | xfs_dialloc_ag_update_inobt( | |
1518 | struct xfs_btree_cur *cur, /* inobt cursor */ | |
1519 | struct xfs_inobt_rec_incore *frec, /* finobt record */ | |
1520 | int offset) /* inode offset */ | |
1521 | { | |
1522 | struct xfs_inobt_rec_incore rec; | |
1523 | int error; | |
1524 | int i; | |
1525 | ||
1526 | error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i); | |
1527 | if (error) | |
1528 | return error; | |
989d5ec3 DW |
1529 | if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { |
1530 | xfs_btree_mark_sick(cur); | |
f9e03706 | 1531 | return -EFSCORRUPTED; |
989d5ec3 | 1532 | } |
6dd8638e BF |
1533 | |
1534 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
1535 | if (error) | |
1536 | return error; | |
989d5ec3 DW |
1537 | if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { |
1538 | xfs_btree_mark_sick(cur); | |
f9e03706 | 1539 | return -EFSCORRUPTED; |
989d5ec3 | 1540 | } |
6dd8638e BF |
1541 | ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) % |
1542 | XFS_INODES_PER_CHUNK) == 0); | |
1543 | ||
1544 | rec.ir_free &= ~XFS_INOBT_MASK(offset); | |
1545 | rec.ir_freecount--; | |
1546 | ||
f9e03706 DW |
1547 | if (XFS_IS_CORRUPT(cur->bc_mp, |
1548 | rec.ir_free != frec->ir_free || | |
989d5ec3 DW |
1549 | rec.ir_freecount != frec->ir_freecount)) { |
1550 | xfs_btree_mark_sick(cur); | |
f9e03706 | 1551 | return -EFSCORRUPTED; |
989d5ec3 | 1552 | } |
6dd8638e | 1553 | |
b72091f2 | 1554 | return xfs_inobt_update(cur, &rec); |
6dd8638e BF |
1555 | } |
1556 | ||
1557 | /* | |
1558 | * Allocate an inode using the free inode btree, if available. Otherwise, fall | |
1559 | * back to the inobt search algorithm. | |
1560 | * | |
1561 | * The caller selected an AG for us, and made sure that free inodes are | |
1562 | * available. | |
1563 | */ | |
b652afd9 | 1564 | static int |
6dd8638e | 1565 | xfs_dialloc_ag( |
dedab3e4 | 1566 | struct xfs_perag *pag, |
6dd8638e BF |
1567 | struct xfs_trans *tp, |
1568 | struct xfs_buf *agbp, | |
1569 | xfs_ino_t parent, | |
1570 | xfs_ino_t *inop) | |
1571 | { | |
1572 | struct xfs_mount *mp = tp->t_mountp; | |
370c782b | 1573 | struct xfs_agi *agi = agbp->b_addr; |
6dd8638e BF |
1574 | xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); |
1575 | xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); | |
6dd8638e BF |
1576 | struct xfs_btree_cur *cur; /* finobt cursor */ |
1577 | struct xfs_btree_cur *icur; /* inobt cursor */ | |
1578 | struct xfs_inobt_rec_incore rec; | |
1579 | xfs_ino_t ino; | |
1580 | int error; | |
1581 | int offset; | |
1582 | int i; | |
1583 | ||
ebd9027d | 1584 | if (!xfs_has_finobt(mp)) |
dedab3e4 | 1585 | return xfs_dialloc_ag_inobt(pag, tp, agbp, parent, inop); |
6dd8638e | 1586 | |
6dd8638e BF |
1587 | /* |
1588 | * If pagino is 0 (this is the root inode allocation) use newino. | |
1589 | * This must work because we've just allocated some. | |
1590 | */ | |
1591 | if (!pagino) | |
1592 | pagino = be32_to_cpu(agi->agi_newino); | |
1593 | ||
14dd46cf | 1594 | cur = xfs_finobt_init_cursor(pag, tp, agbp); |
6dd8638e | 1595 | |
9ba0889e | 1596 | error = xfs_check_agi_freecount(cur); |
6dd8638e BF |
1597 | if (error) |
1598 | goto error_cur; | |
1599 | ||
1600 | /* | |
1601 | * The search algorithm depends on whether we're in the same AG as the | |
1602 | * parent. If so, find the closest available inode to the parent. If | |
1603 | * not, consider the agi hint or find the first free inode in the AG. | |
1604 | */ | |
e9c4d8bf | 1605 | if (pag_agno(pag) == pagno) |
6dd8638e BF |
1606 | error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec); |
1607 | else | |
1608 | error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec); | |
1609 | if (error) | |
1610 | goto error_cur; | |
1611 | ||
d4cc540b | 1612 | offset = xfs_inobt_first_free_inode(&rec); |
6dd8638e BF |
1613 | ASSERT(offset >= 0); |
1614 | ASSERT(offset < XFS_INODES_PER_CHUNK); | |
1615 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % | |
1616 | XFS_INODES_PER_CHUNK) == 0); | |
6abd82ab | 1617 | ino = xfs_agino_to_ino(pag, rec.ir_startino + offset); |
6dd8638e | 1618 | |
2935213a DW |
1619 | if (xfs_ag_has_sickness(pag, XFS_SICK_AG_INODES)) { |
1620 | error = xfs_dialloc_check_ino(pag, tp, ino); | |
1621 | if (error) | |
1622 | goto error_cur; | |
1623 | } | |
1624 | ||
6dd8638e BF |
1625 | /* |
1626 | * Modify or remove the finobt record. | |
1627 | */ | |
1628 | rec.ir_free &= ~XFS_INOBT_MASK(offset); | |
1629 | rec.ir_freecount--; | |
1630 | if (rec.ir_freecount) | |
1631 | error = xfs_inobt_update(cur, &rec); | |
1632 | else | |
1633 | error = xfs_btree_delete(cur, &i); | |
1634 | if (error) | |
1635 | goto error_cur; | |
1636 | ||
1637 | /* | |
1638 | * The finobt has now been updated appropriately. We haven't updated the | |
1639 | * agi and superblock yet, so we can create an inobt cursor and validate | |
1640 | * the original freecount. If all is well, make the equivalent update to | |
1641 | * the inobt using the finobt record and offset information. | |
1642 | */ | |
14dd46cf | 1643 | icur = xfs_inobt_init_cursor(pag, tp, agbp); |
6dd8638e | 1644 | |
9ba0889e | 1645 | error = xfs_check_agi_freecount(icur); |
6dd8638e BF |
1646 | if (error) |
1647 | goto error_icur; | |
1648 | ||
1649 | error = xfs_dialloc_ag_update_inobt(icur, &rec, offset); | |
1650 | if (error) | |
1651 | goto error_icur; | |
1652 | ||
1653 | /* | |
1654 | * Both trees have now been updated. We must update the perag and | |
1655 | * superblock before we can check the freecount for each btree. | |
1656 | */ | |
1657 | be32_add_cpu(&agi->agi_freecount, -1); | |
1658 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); | |
7b13c515 | 1659 | pag->pagi_freecount--; |
6dd8638e BF |
1660 | |
1661 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); | |
1662 | ||
9ba0889e | 1663 | error = xfs_check_agi_freecount(icur); |
6dd8638e BF |
1664 | if (error) |
1665 | goto error_icur; | |
9ba0889e | 1666 | error = xfs_check_agi_freecount(cur); |
6dd8638e BF |
1667 | if (error) |
1668 | goto error_icur; | |
1669 | ||
1670 | xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR); | |
1671 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
6dd8638e BF |
1672 | *inop = ino; |
1673 | return 0; | |
1674 | ||
1675 | error_icur: | |
1676 | xfs_btree_del_cursor(icur, XFS_BTREE_ERROR); | |
1677 | error_cur: | |
1678 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
6dd8638e BF |
1679 | return error; |
1680 | } | |
1681 | ||
f3bf6e0f | 1682 | static int |
aececc9f DC |
1683 | xfs_dialloc_roll( |
1684 | struct xfs_trans **tpp, | |
1685 | struct xfs_buf *agibp) | |
1686 | { | |
1687 | struct xfs_trans *tp = *tpp; | |
1688 | struct xfs_dquot_acct *dqinfo; | |
1689 | int error; | |
1690 | ||
1691 | /* | |
1692 | * Hold to on to the agibp across the commit so no other allocation can | |
1693 | * come in and take the free inodes we just allocated for our caller. | |
1694 | */ | |
1695 | xfs_trans_bhold(tp, agibp); | |
1696 | ||
1697 | /* | |
1698 | * We want the quota changes to be associated with the next transaction, | |
1699 | * NOT this one. So, detach the dqinfo from this and attach it to the | |
1700 | * next transaction. | |
1701 | */ | |
1702 | dqinfo = tp->t_dqinfo; | |
1703 | tp->t_dqinfo = NULL; | |
1704 | ||
1705 | error = xfs_trans_roll(&tp); | |
1706 | ||
1707 | /* Re-attach the quota info that we detached from prev trx. */ | |
1708 | tp->t_dqinfo = dqinfo; | |
1709 | ||
8237fbf5 DC |
1710 | /* |
1711 | * Join the buffer even on commit error so that the buffer is released | |
1712 | * when the caller cancels the transaction and doesn't have to handle | |
1713 | * this error case specially. | |
1714 | */ | |
aececc9f | 1715 | xfs_trans_bjoin(tp, agibp); |
8237fbf5 DC |
1716 | *tpp = tp; |
1717 | return error; | |
aececc9f DC |
1718 | } |
1719 | ||
8237fbf5 DC |
1720 | static bool |
1721 | xfs_dialloc_good_ag( | |
8237fbf5 | 1722 | struct xfs_perag *pag, |
dedab3e4 | 1723 | struct xfs_trans *tp, |
8237fbf5 DC |
1724 | umode_t mode, |
1725 | int flags, | |
1726 | bool ok_alloc) | |
1727 | { | |
1728 | struct xfs_mount *mp = tp->t_mountp; | |
1729 | xfs_extlen_t ineed; | |
1730 | xfs_extlen_t longest = 0; | |
1731 | int needspace; | |
1732 | int error; | |
1733 | ||
dedab3e4 DC |
1734 | if (!pag) |
1735 | return false; | |
7ac2ff8b | 1736 | if (!xfs_perag_allows_inodes(pag)) |
8237fbf5 DC |
1737 | return false; |
1738 | ||
7ac2ff8b | 1739 | if (!xfs_perag_initialised_agi(pag)) { |
549d3c9a | 1740 | error = xfs_ialloc_read_agi(pag, tp, 0, NULL); |
8237fbf5 DC |
1741 | if (error) |
1742 | return false; | |
1743 | } | |
1744 | ||
1745 | if (pag->pagi_freecount) | |
1746 | return true; | |
1747 | if (!ok_alloc) | |
1748 | return false; | |
1749 | ||
7ac2ff8b | 1750 | if (!xfs_perag_initialised_agf(pag)) { |
08d3e84f | 1751 | error = xfs_alloc_read_agf(pag, tp, flags, NULL); |
8237fbf5 DC |
1752 | if (error) |
1753 | return false; | |
1754 | } | |
1755 | ||
1756 | /* | |
1757 | * Check that there is enough free space for the file plus a chunk of | |
1758 | * inodes if we need to allocate some. If this is the first pass across | |
1759 | * the AGs, take into account the potential space needed for alignment | |
1760 | * of inode chunks when checking the longest contiguous free space in | |
1761 | * the AG - this prevents us from getting ENOSPC because we have free | |
1762 | * space larger than ialloc_blks but alignment constraints prevent us | |
1763 | * from using it. | |
1764 | * | |
1765 | * If we can't find an AG with space for full alignment slack to be | |
1766 | * taken into account, we must be near ENOSPC in all AGs. Hence we | |
1767 | * don't include alignment for the second pass and so if we fail | |
1768 | * allocation due to alignment issues then it is most likely a real | |
1769 | * ENOSPC condition. | |
1770 | * | |
1771 | * XXX(dgc): this calculation is now bogus thanks to the per-ag | |
1772 | * reservations that xfs_alloc_fix_freelist() now does via | |
1773 | * xfs_alloc_space_available(). When the AG fills up, pagf_freeblks will | |
1774 | * be more than large enough for the check below to succeed, but | |
1775 | * xfs_alloc_space_available() will fail because of the non-zero | |
1776 | * metadata reservation and hence we won't actually be able to allocate | |
1777 | * more inodes in this AG. We do soooo much unnecessary work near ENOSPC | |
1778 | * because of this. | |
1779 | */ | |
1780 | ineed = M_IGEO(mp)->ialloc_min_blks; | |
1781 | if (flags && ineed > 1) | |
1782 | ineed += M_IGEO(mp)->cluster_align; | |
1783 | longest = pag->pagf_longest; | |
1784 | if (!longest) | |
1785 | longest = pag->pagf_flcount > 0; | |
1786 | needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); | |
1787 | ||
1788 | if (pag->pagf_freeblks < needspace + ineed || longest < ineed) | |
1789 | return false; | |
1790 | return true; | |
1791 | } | |
1792 | ||
1793 | static int | |
1794 | xfs_dialloc_try_ag( | |
8237fbf5 | 1795 | struct xfs_perag *pag, |
dedab3e4 | 1796 | struct xfs_trans **tpp, |
8237fbf5 DC |
1797 | xfs_ino_t parent, |
1798 | xfs_ino_t *new_ino, | |
1799 | bool ok_alloc) | |
1800 | { | |
1801 | struct xfs_buf *agbp; | |
1802 | xfs_ino_t ino; | |
1803 | int error; | |
1804 | ||
1805 | /* | |
1806 | * Then read in the AGI buffer and recheck with the AGI buffer | |
1807 | * lock held. | |
1808 | */ | |
549d3c9a | 1809 | error = xfs_ialloc_read_agi(pag, *tpp, 0, &agbp); |
8237fbf5 DC |
1810 | if (error) |
1811 | return error; | |
1812 | ||
1813 | if (!pag->pagi_freecount) { | |
1814 | if (!ok_alloc) { | |
1815 | error = -EAGAIN; | |
1816 | goto out_release; | |
1817 | } | |
1818 | ||
dedab3e4 | 1819 | error = xfs_ialloc_ag_alloc(pag, *tpp, agbp); |
8237fbf5 DC |
1820 | if (error < 0) |
1821 | goto out_release; | |
1822 | ||
1823 | /* | |
1824 | * We successfully allocated space for an inode cluster in this | |
1825 | * AG. Roll the transaction so that we can allocate one of the | |
1826 | * new inodes. | |
1827 | */ | |
1828 | ASSERT(pag->pagi_freecount > 0); | |
1829 | error = xfs_dialloc_roll(tpp, agbp); | |
1830 | if (error) | |
1831 | goto out_release; | |
1832 | } | |
1833 | ||
1834 | /* Allocate an inode in the found AG */ | |
dedab3e4 | 1835 | error = xfs_dialloc_ag(pag, *tpp, agbp, parent, &ino); |
8237fbf5 DC |
1836 | if (!error) |
1837 | *new_ino = ino; | |
1838 | return error; | |
1839 | ||
1840 | out_release: | |
1841 | xfs_trans_brelse(*tpp, agbp); | |
1842 | return error; | |
1843 | } | |
1844 | ||
8651b410 DW |
1845 | /* |
1846 | * Pick an AG for the new inode. | |
1847 | * | |
1848 | * Directories, symlinks, and regular files frequently allocate at least one | |
1849 | * block, so factor that potential expansion when we examine whether an AG has | |
1850 | * enough space for file creation. Try to keep metadata files all in the same | |
1851 | * AG. | |
1852 | */ | |
1853 | static inline xfs_agnumber_t | |
1854 | xfs_dialloc_pick_ag( | |
1855 | struct xfs_mount *mp, | |
1856 | struct xfs_inode *dp, | |
1857 | umode_t mode) | |
1858 | { | |
1859 | xfs_agnumber_t start_agno; | |
1860 | ||
1861 | if (!dp) | |
1862 | return 0; | |
1863 | if (xfs_is_metadir_inode(dp)) { | |
1864 | if (mp->m_sb.sb_logstart) | |
1865 | return XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart); | |
1866 | return 0; | |
1867 | } | |
1868 | ||
1869 | if (S_ISDIR(mode)) | |
1870 | return (atomic_inc_return(&mp->m_agirotor) - 1) % mp->m_maxagi; | |
1871 | ||
1872 | start_agno = XFS_INO_TO_AGNO(mp, dp->i_ino); | |
1873 | if (start_agno >= mp->m_maxagi) | |
1874 | start_agno = 0; | |
1875 | ||
1876 | return start_agno; | |
1877 | } | |
1878 | ||
f2ecc5e4 | 1879 | /* |
8237fbf5 | 1880 | * Allocate an on-disk inode. |
f2ecc5e4 | 1881 | * |
8d822dc3 | 1882 | * Mode is used to tell whether the new inode is a directory and hence where to |
8237fbf5 DC |
1883 | * locate it. The on-disk inode that is allocated will be returned in @new_ino |
1884 | * on success, otherwise an error will be set to indicate the failure (e.g. | |
1885 | * -ENOSPC). | |
f2ecc5e4 CH |
1886 | */ |
1887 | int | |
b652afd9 | 1888 | xfs_dialloc( |
f3bf6e0f | 1889 | struct xfs_trans **tpp, |
390b4775 | 1890 | const struct xfs_icreate_args *args, |
b652afd9 | 1891 | xfs_ino_t *new_ino) |
f2ecc5e4 | 1892 | { |
f3bf6e0f | 1893 | struct xfs_mount *mp = (*tpp)->t_mountp; |
8651b410 DW |
1894 | struct xfs_perag *pag; |
1895 | struct xfs_ino_geometry *igeo = M_IGEO(mp); | |
1896 | xfs_ino_t ino = NULLFSINO; | |
390b4775 | 1897 | xfs_ino_t parent = args->pip ? args->pip->i_ino : 0; |
f2ecc5e4 | 1898 | xfs_agnumber_t agno; |
be60fe54 | 1899 | xfs_agnumber_t start_agno; |
8651b410 | 1900 | umode_t mode = args->mode & S_IFMT; |
8237fbf5 | 1901 | bool ok_alloc = true; |
f08f984c | 1902 | bool low_space = false; |
89b1f55a | 1903 | int flags; |
8651b410 | 1904 | int error = 0; |
8d822dc3 | 1905 | |
8651b410 | 1906 | start_agno = xfs_dialloc_pick_ag(mp, args->pip, mode); |
55d6af64 | 1907 | |
f2ecc5e4 CH |
1908 | /* |
1909 | * If we have already hit the ceiling of inode blocks then clear | |
8237fbf5 | 1910 | * ok_alloc so we scan all available agi structures for a free |
f2ecc5e4 | 1911 | * inode. |
74f9ce1c GW |
1912 | * |
1913 | * Read rough value of mp->m_icount by percpu_counter_read_positive, | |
1914 | * which will sacrifice the preciseness but improve the performance. | |
f2ecc5e4 | 1915 | */ |
ef325959 DW |
1916 | if (igeo->maxicount && |
1917 | percpu_counter_read_positive(&mp->m_icount) + igeo->ialloc_inos | |
1918 | > igeo->maxicount) { | |
8237fbf5 | 1919 | ok_alloc = false; |
f2ecc5e4 CH |
1920 | } |
1921 | ||
f08f984c DC |
1922 | /* |
1923 | * If we are near to ENOSPC, we want to prefer allocation from AGs that | |
1924 | * have free inodes in them rather than use up free space allocating new | |
1925 | * inode chunks. Hence we turn off allocation for the first non-blocking | |
1926 | * pass through the AGs if we are near ENOSPC to consume free inodes | |
1927 | * that we can immediately allocate, but then we allow allocation on the | |
1928 | * second pass if we fail to find an AG with free inodes in it. | |
1929 | */ | |
1930 | if (percpu_counter_read_positive(&mp->m_fdblocks) < | |
1931 | mp->m_low_space[XFS_LOWSP_1_PCNT]) { | |
1932 | ok_alloc = false; | |
1933 | low_space = true; | |
1934 | } | |
1935 | ||
f2ecc5e4 CH |
1936 | /* |
1937 | * Loop until we find an allocation group that either has free inodes | |
1938 | * or in which we can allocate some inodes. Iterate through the | |
1939 | * allocation groups upward, wrapping at the end. | |
1940 | */ | |
89b1f55a | 1941 | flags = XFS_ALLOC_FLAG_TRYLOCK; |
76257a15 DC |
1942 | retry: |
1943 | for_each_perag_wrap_at(mp, start_agno, mp->m_maxagi, agno, pag) { | |
dedab3e4 DC |
1944 | if (xfs_dialloc_good_ag(pag, *tpp, mode, flags, ok_alloc)) { |
1945 | error = xfs_dialloc_try_ag(pag, tpp, parent, | |
8237fbf5 DC |
1946 | &ino, ok_alloc); |
1947 | if (error != -EAGAIN) | |
42685473 | 1948 | break; |
76257a15 | 1949 | error = 0; |
f2ecc5e4 | 1950 | } |
be60fe54 | 1951 | |
75c8c50f | 1952 | if (xfs_is_shutdown(mp)) { |
89b1f55a | 1953 | error = -EFSCORRUPTED; |
42685473 | 1954 | break; |
89b1f55a | 1955 | } |
76257a15 DC |
1956 | } |
1957 | if (pag) | |
1958 | xfs_perag_rele(pag); | |
1959 | if (error) | |
1960 | return error; | |
1961 | if (ino == NULLFSINO) { | |
1962 | if (flags) { | |
89b1f55a | 1963 | flags = 0; |
f08f984c DC |
1964 | if (low_space) |
1965 | ok_alloc = true; | |
76257a15 | 1966 | goto retry; |
89b1f55a | 1967 | } |
76257a15 | 1968 | return -ENOSPC; |
f2ecc5e4 | 1969 | } |
38fd3d6a DW |
1970 | |
1971 | /* | |
1972 | * Protect against obviously corrupt allocation btree records. Later | |
1973 | * xfs_iget checks will catch re-allocation of other active in-memory | |
1974 | * and on-disk inodes. If we don't catch reallocating the parent inode | |
1975 | * here we will deadlock in xfs_iget() so we have to do these checks | |
1976 | * first. | |
1977 | */ | |
1978 | if (ino == parent || !xfs_verify_dir_ino(mp, ino)) { | |
1979 | xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino); | |
1980 | xfs_agno_mark_sick(mp, XFS_INO_TO_AGNO(mp, ino), | |
1981 | XFS_SICK_AG_INOBT); | |
1982 | return -EFSCORRUPTED; | |
1983 | } | |
1984 | ||
76257a15 DC |
1985 | *new_ino = ino; |
1986 | return 0; | |
f2ecc5e4 CH |
1987 | } |
1988 | ||
10ae3dc7 BF |
1989 | /* |
1990 | * Free the blocks of an inode chunk. We must consider that the inode chunk | |
1991 | * might be sparse and only free the regions that are allocated as part of the | |
1992 | * chunk. | |
1993 | */ | |
7dfee17b | 1994 | static int |
10ae3dc7 | 1995 | xfs_difree_inode_chunk( |
0f37d178 | 1996 | struct xfs_trans *tp, |
67ce5ba5 | 1997 | struct xfs_perag *pag, |
0f37d178 | 1998 | struct xfs_inobt_rec_incore *rec) |
10ae3dc7 | 1999 | { |
0f37d178 BF |
2000 | struct xfs_mount *mp = tp->t_mountp; |
2001 | xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp, | |
2002 | rec->ir_startino); | |
2003 | int startidx, endidx; | |
2004 | int nextbit; | |
2005 | xfs_agblock_t agbno; | |
2006 | int contigblk; | |
10ae3dc7 BF |
2007 | DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS); |
2008 | ||
2009 | if (!xfs_inobt_issparse(rec->ir_holemask)) { | |
2010 | /* not sparse, calculate extent info directly */ | |
856a920a | 2011 | return xfs_free_extent_later(tp, xfs_agbno_to_fsb(pag, sagbno), |
b742d7b4 | 2012 | M_IGEO(mp)->ialloc_blks, &XFS_RMAP_OINFO_INODES, |
980faece | 2013 | XFS_AG_RESV_NONE, 0); |
10ae3dc7 BF |
2014 | } |
2015 | ||
2016 | /* holemask is only 16-bits (fits in an unsigned long) */ | |
2017 | ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0])); | |
2018 | holemask[0] = rec->ir_holemask; | |
2019 | ||
2020 | /* | |
2021 | * Find contiguous ranges of zeroes (i.e., allocated regions) in the | |
2022 | * holemask and convert the start/end index of each range to an extent. | |
2023 | * We start with the start and end index both pointing at the first 0 in | |
2024 | * the mask. | |
2025 | */ | |
2026 | startidx = endidx = find_first_zero_bit(holemask, | |
2027 | XFS_INOBT_HOLEMASK_BITS); | |
2028 | nextbit = startidx + 1; | |
2029 | while (startidx < XFS_INOBT_HOLEMASK_BITS) { | |
7dfee17b DC |
2030 | int error; |
2031 | ||
10ae3dc7 BF |
2032 | nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS, |
2033 | nextbit); | |
2034 | /* | |
2035 | * If the next zero bit is contiguous, update the end index of | |
2036 | * the current range and continue. | |
2037 | */ | |
2038 | if (nextbit != XFS_INOBT_HOLEMASK_BITS && | |
2039 | nextbit == endidx + 1) { | |
2040 | endidx = nextbit; | |
2041 | goto next; | |
2042 | } | |
2043 | ||
2044 | /* | |
2045 | * nextbit is not contiguous with the current end index. Convert | |
2046 | * the current start/end to an extent and add it to the free | |
2047 | * list. | |
2048 | */ | |
2049 | agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) / | |
2050 | mp->m_sb.sb_inopblock; | |
2051 | contigblk = ((endidx - startidx + 1) * | |
2052 | XFS_INODES_PER_HOLEMASK_BIT) / | |
2053 | mp->m_sb.sb_inopblock; | |
2054 | ||
2055 | ASSERT(agbno % mp->m_sb.sb_spino_align == 0); | |
2056 | ASSERT(contigblk % mp->m_sb.sb_spino_align == 0); | |
856a920a CH |
2057 | error = xfs_free_extent_later(tp, xfs_agbno_to_fsb(pag, agbno), |
2058 | contigblk, &XFS_RMAP_OINFO_INODES, | |
2059 | XFS_AG_RESV_NONE, 0); | |
7dfee17b DC |
2060 | if (error) |
2061 | return error; | |
10ae3dc7 BF |
2062 | |
2063 | /* reset range to current bit and carry on... */ | |
2064 | startidx = endidx = nextbit; | |
2065 | ||
2066 | next: | |
2067 | nextbit++; | |
2068 | } | |
7dfee17b | 2069 | return 0; |
10ae3dc7 BF |
2070 | } |
2071 | ||
2b64ee5c BF |
2072 | STATIC int |
2073 | xfs_difree_inobt( | |
dedab3e4 | 2074 | struct xfs_perag *pag, |
2b64ee5c BF |
2075 | struct xfs_trans *tp, |
2076 | struct xfs_buf *agbp, | |
2077 | xfs_agino_t agino, | |
09b56604 | 2078 | struct xfs_icluster *xic, |
2b64ee5c | 2079 | struct xfs_inobt_rec_incore *orec) |
1da177e4 | 2080 | { |
e9c4d8bf | 2081 | struct xfs_mount *mp = pag_mount(pag); |
370c782b | 2082 | struct xfs_agi *agi = agbp->b_addr; |
2b64ee5c BF |
2083 | struct xfs_btree_cur *cur; |
2084 | struct xfs_inobt_rec_incore rec; | |
2085 | int ilen; | |
2086 | int error; | |
2087 | int i; | |
2088 | int off; | |
1da177e4 | 2089 | |
69ef921b | 2090 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); |
2b64ee5c BF |
2091 | ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length)); |
2092 | ||
1da177e4 LT |
2093 | /* |
2094 | * Initialize the cursor. | |
2095 | */ | |
14dd46cf | 2096 | cur = xfs_inobt_init_cursor(pag, tp, agbp); |
1da177e4 | 2097 | |
9ba0889e | 2098 | error = xfs_check_agi_freecount(cur); |
0b48db80 DC |
2099 | if (error) |
2100 | goto error0; | |
2101 | ||
1da177e4 LT |
2102 | /* |
2103 | * Look for the entry describing this inode. | |
2104 | */ | |
21875505 | 2105 | if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) { |
0b932ccc DC |
2106 | xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.", |
2107 | __func__, error); | |
1da177e4 LT |
2108 | goto error0; |
2109 | } | |
f9e03706 | 2110 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
989d5ec3 | 2111 | xfs_btree_mark_sick(cur); |
f9e03706 DW |
2112 | error = -EFSCORRUPTED; |
2113 | goto error0; | |
2114 | } | |
2e287a73 CH |
2115 | error = xfs_inobt_get_rec(cur, &rec, &i); |
2116 | if (error) { | |
0b932ccc DC |
2117 | xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.", |
2118 | __func__, error); | |
1da177e4 LT |
2119 | goto error0; |
2120 | } | |
f9e03706 | 2121 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
989d5ec3 | 2122 | xfs_btree_mark_sick(cur); |
f9e03706 DW |
2123 | error = -EFSCORRUPTED; |
2124 | goto error0; | |
2125 | } | |
1da177e4 LT |
2126 | /* |
2127 | * Get the offset in the inode chunk. | |
2128 | */ | |
2129 | off = agino - rec.ir_startino; | |
2130 | ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); | |
0d87e656 | 2131 | ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); |
1da177e4 LT |
2132 | /* |
2133 | * Mark the inode free & increment the count. | |
2134 | */ | |
0d87e656 | 2135 | rec.ir_free |= XFS_INOBT_MASK(off); |
1da177e4 LT |
2136 | rec.ir_freecount++; |
2137 | ||
2138 | /* | |
999633d3 BF |
2139 | * When an inode chunk is free, it becomes eligible for removal. Don't |
2140 | * remove the chunk if the block size is large enough for multiple inode | |
2141 | * chunks (that might not be free). | |
1da177e4 | 2142 | */ |
0560f31a | 2143 | if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE && |
999633d3 | 2144 | mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) { |
749f24f3 | 2145 | xic->deleted = true; |
6abd82ab | 2146 | xic->first_ino = xfs_agino_to_ino(pag, rec.ir_startino); |
09b56604 | 2147 | xic->alloc = xfs_inobt_irec_to_allocmask(&rec); |
1da177e4 LT |
2148 | |
2149 | /* | |
2150 | * Remove the inode cluster from the AGI B+Tree, adjust the | |
2151 | * AGI and Superblock inode counts, and mark the disk space | |
2152 | * to be freed when the transaction is committed. | |
2153 | */ | |
999633d3 | 2154 | ilen = rec.ir_freecount; |
413d57c9 MS |
2155 | be32_add_cpu(&agi->agi_count, -ilen); |
2156 | be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); | |
1da177e4 | 2157 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); |
44b56e0a | 2158 | pag->pagi_freecount -= ilen - 1; |
89e9b5c0 | 2159 | pag->pagi_count -= ilen; |
1da177e4 LT |
2160 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); |
2161 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); | |
2162 | ||
91cca5df | 2163 | if ((error = xfs_btree_delete(cur, &i))) { |
0b932ccc DC |
2164 | xfs_warn(mp, "%s: xfs_btree_delete returned error %d.", |
2165 | __func__, error); | |
1da177e4 LT |
2166 | goto error0; |
2167 | } | |
2168 | ||
67ce5ba5 | 2169 | error = xfs_difree_inode_chunk(tp, pag, &rec); |
7dfee17b DC |
2170 | if (error) |
2171 | goto error0; | |
1da177e4 | 2172 | } else { |
749f24f3 | 2173 | xic->deleted = false; |
1da177e4 | 2174 | |
afabc24a CH |
2175 | error = xfs_inobt_update(cur, &rec); |
2176 | if (error) { | |
0b932ccc DC |
2177 | xfs_warn(mp, "%s: xfs_inobt_update returned error %d.", |
2178 | __func__, error); | |
1da177e4 LT |
2179 | goto error0; |
2180 | } | |
afabc24a | 2181 | |
b7df7630 | 2182 | /* |
1da177e4 LT |
2183 | * Change the inode free counts and log the ag/sb changes. |
2184 | */ | |
413d57c9 | 2185 | be32_add_cpu(&agi->agi_freecount, 1); |
1da177e4 | 2186 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
7b13c515 | 2187 | pag->pagi_freecount++; |
1da177e4 LT |
2188 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); |
2189 | } | |
2190 | ||
9ba0889e | 2191 | error = xfs_check_agi_freecount(cur); |
0b48db80 DC |
2192 | if (error) |
2193 | goto error0; | |
1da177e4 | 2194 | |
2b64ee5c | 2195 | *orec = rec; |
1da177e4 LT |
2196 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
2197 | return 0; | |
2198 | ||
2199 | error0: | |
2200 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
2201 | return error; | |
2202 | } | |
2203 | ||
3efa4ffd BF |
2204 | /* |
2205 | * Free an inode in the free inode btree. | |
2206 | */ | |
2207 | STATIC int | |
2208 | xfs_difree_finobt( | |
dedab3e4 | 2209 | struct xfs_perag *pag, |
3efa4ffd BF |
2210 | struct xfs_trans *tp, |
2211 | struct xfs_buf *agbp, | |
2212 | xfs_agino_t agino, | |
2213 | struct xfs_inobt_rec_incore *ibtrec) /* inobt record */ | |
2214 | { | |
e9c4d8bf | 2215 | struct xfs_mount *mp = pag_mount(pag); |
3efa4ffd BF |
2216 | struct xfs_btree_cur *cur; |
2217 | struct xfs_inobt_rec_incore rec; | |
2218 | int offset = agino - ibtrec->ir_startino; | |
2219 | int error; | |
2220 | int i; | |
2221 | ||
14dd46cf | 2222 | cur = xfs_finobt_init_cursor(pag, tp, agbp); |
3efa4ffd BF |
2223 | |
2224 | error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i); | |
2225 | if (error) | |
2226 | goto error; | |
2227 | if (i == 0) { | |
2228 | /* | |
2229 | * If the record does not exist in the finobt, we must have just | |
2230 | * freed an inode in a previously fully allocated chunk. If not, | |
2231 | * something is out of sync. | |
2232 | */ | |
f9e03706 | 2233 | if (XFS_IS_CORRUPT(mp, ibtrec->ir_freecount != 1)) { |
989d5ec3 | 2234 | xfs_btree_mark_sick(cur); |
f9e03706 DW |
2235 | error = -EFSCORRUPTED; |
2236 | goto error; | |
2237 | } | |
3efa4ffd | 2238 | |
5419040f BF |
2239 | error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask, |
2240 | ibtrec->ir_count, | |
2241 | ibtrec->ir_freecount, | |
3efa4ffd BF |
2242 | ibtrec->ir_free, &i); |
2243 | if (error) | |
2244 | goto error; | |
2245 | ASSERT(i == 1); | |
2246 | ||
2247 | goto out; | |
2248 | } | |
2249 | ||
2250 | /* | |
2251 | * Read and update the existing record. We could just copy the ibtrec | |
2252 | * across here, but that would defeat the purpose of having redundant | |
2253 | * metadata. By making the modifications independently, we can catch | |
2254 | * corruptions that we wouldn't see if we just copied from one record | |
2255 | * to another. | |
2256 | */ | |
2257 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
2258 | if (error) | |
2259 | goto error; | |
f9e03706 | 2260 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
989d5ec3 | 2261 | xfs_btree_mark_sick(cur); |
f9e03706 DW |
2262 | error = -EFSCORRUPTED; |
2263 | goto error; | |
2264 | } | |
3efa4ffd BF |
2265 | |
2266 | rec.ir_free |= XFS_INOBT_MASK(offset); | |
2267 | rec.ir_freecount++; | |
2268 | ||
f9e03706 DW |
2269 | if (XFS_IS_CORRUPT(mp, |
2270 | rec.ir_free != ibtrec->ir_free || | |
2271 | rec.ir_freecount != ibtrec->ir_freecount)) { | |
989d5ec3 | 2272 | xfs_btree_mark_sick(cur); |
f9e03706 DW |
2273 | error = -EFSCORRUPTED; |
2274 | goto error; | |
2275 | } | |
3efa4ffd BF |
2276 | |
2277 | /* | |
2278 | * The content of inobt records should always match between the inobt | |
2279 | * and finobt. The lifecycle of records in the finobt is different from | |
2280 | * the inobt in that the finobt only tracks records with at least one | |
2281 | * free inode. Hence, if all of the inodes are free and we aren't | |
2282 | * keeping inode chunks permanently on disk, remove the record. | |
2283 | * Otherwise, update the record with the new information. | |
999633d3 BF |
2284 | * |
2285 | * Note that we currently can't free chunks when the block size is large | |
2286 | * enough for multiple chunks. Leave the finobt record to remain in sync | |
2287 | * with the inobt. | |
3efa4ffd | 2288 | */ |
0560f31a DC |
2289 | if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE && |
2290 | mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) { | |
3efa4ffd BF |
2291 | error = xfs_btree_delete(cur, &i); |
2292 | if (error) | |
2293 | goto error; | |
2294 | ASSERT(i == 1); | |
2295 | } else { | |
2296 | error = xfs_inobt_update(cur, &rec); | |
2297 | if (error) | |
2298 | goto error; | |
2299 | } | |
2300 | ||
2301 | out: | |
9ba0889e | 2302 | error = xfs_check_agi_freecount(cur); |
3efa4ffd BF |
2303 | if (error) |
2304 | goto error; | |
2305 | ||
2306 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
2307 | return 0; | |
2308 | ||
2309 | error: | |
2310 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
2311 | return error; | |
2312 | } | |
2313 | ||
2b64ee5c BF |
2314 | /* |
2315 | * Free disk inode. Carefully avoids touching the incore inode, all | |
2316 | * manipulations incore are the caller's responsibility. | |
2317 | * The on-disk inode is not changed by this operation, only the | |
2318 | * btree (free inode mask) is changed. | |
2319 | */ | |
2320 | int | |
2321 | xfs_difree( | |
f40aadb2 DC |
2322 | struct xfs_trans *tp, |
2323 | struct xfs_perag *pag, | |
2324 | xfs_ino_t inode, | |
2325 | struct xfs_icluster *xic) | |
2b64ee5c BF |
2326 | { |
2327 | /* REFERENCED */ | |
2328 | xfs_agblock_t agbno; /* block number containing inode */ | |
2329 | struct xfs_buf *agbp; /* buffer for allocation group header */ | |
2330 | xfs_agino_t agino; /* allocation group inode number */ | |
2b64ee5c | 2331 | int error; /* error return value */ |
7b13c515 | 2332 | struct xfs_mount *mp = tp->t_mountp; |
2b64ee5c | 2333 | struct xfs_inobt_rec_incore rec;/* btree record */ |
2b64ee5c BF |
2334 | |
2335 | /* | |
2336 | * Break up inode number into its components. | |
2337 | */ | |
e9c4d8bf CH |
2338 | if (pag_agno(pag) != XFS_INO_TO_AGNO(mp, inode)) { |
2339 | xfs_warn(mp, "%s: agno != pag_agno(pag) (%d != %d).", | |
2340 | __func__, XFS_INO_TO_AGNO(mp, inode), pag_agno(pag)); | |
2b64ee5c | 2341 | ASSERT(0); |
2451337d | 2342 | return -EINVAL; |
2b64ee5c BF |
2343 | } |
2344 | agino = XFS_INO_TO_AGINO(mp, inode); | |
6abd82ab CH |
2345 | if (inode != xfs_agino_to_ino(pag, agino)) { |
2346 | xfs_warn(mp, "%s: inode != xfs_agino_to_ino() (%llu != %llu).", | |
2b64ee5c | 2347 | __func__, (unsigned long long)inode, |
6abd82ab | 2348 | (unsigned long long)xfs_agino_to_ino(pag, agino)); |
2b64ee5c | 2349 | ASSERT(0); |
2451337d | 2350 | return -EINVAL; |
2b64ee5c BF |
2351 | } |
2352 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); | |
13325333 DC |
2353 | if (agbno >= xfs_ag_block_count(mp, pag_agno(pag))) { |
2354 | xfs_warn(mp, "%s: agbno >= xfs_ag_block_count (%d >= %d).", | |
2355 | __func__, agbno, xfs_ag_block_count(mp, pag_agno(pag))); | |
2b64ee5c | 2356 | ASSERT(0); |
2451337d | 2357 | return -EINVAL; |
2b64ee5c BF |
2358 | } |
2359 | /* | |
2360 | * Get the allocation group header. | |
2361 | */ | |
549d3c9a | 2362 | error = xfs_ialloc_read_agi(pag, tp, 0, &agbp); |
2b64ee5c BF |
2363 | if (error) { |
2364 | xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.", | |
2365 | __func__, error); | |
2366 | return error; | |
2367 | } | |
2368 | ||
2369 | /* | |
2370 | * Fix up the inode allocation btree. | |
2371 | */ | |
dedab3e4 | 2372 | error = xfs_difree_inobt(pag, tp, agbp, agino, xic, &rec); |
2b64ee5c BF |
2373 | if (error) |
2374 | goto error0; | |
2375 | ||
3efa4ffd BF |
2376 | /* |
2377 | * Fix up the free inode btree. | |
2378 | */ | |
ebd9027d | 2379 | if (xfs_has_finobt(mp)) { |
dedab3e4 | 2380 | error = xfs_difree_finobt(pag, tp, agbp, agino, &rec); |
3efa4ffd BF |
2381 | if (error) |
2382 | goto error0; | |
2383 | } | |
2384 | ||
2b64ee5c BF |
2385 | return 0; |
2386 | ||
2387 | error0: | |
2388 | return error; | |
2389 | } | |
2390 | ||
7124fe0a DC |
2391 | STATIC int |
2392 | xfs_imap_lookup( | |
7b13c515 | 2393 | struct xfs_perag *pag, |
498f0adb | 2394 | struct xfs_trans *tp, |
7124fe0a DC |
2395 | xfs_agino_t agino, |
2396 | xfs_agblock_t agbno, | |
2397 | xfs_agblock_t *chunk_agbno, | |
2398 | xfs_agblock_t *offset_agbno, | |
2399 | int flags) | |
2400 | { | |
e9c4d8bf | 2401 | struct xfs_mount *mp = pag_mount(pag); |
7124fe0a DC |
2402 | struct xfs_inobt_rec_incore rec; |
2403 | struct xfs_btree_cur *cur; | |
2404 | struct xfs_buf *agbp; | |
7124fe0a DC |
2405 | int error; |
2406 | int i; | |
2407 | ||
549d3c9a | 2408 | error = xfs_ialloc_read_agi(pag, tp, 0, &agbp); |
7124fe0a | 2409 | if (error) { |
53487786 DC |
2410 | xfs_alert(mp, |
2411 | "%s: xfs_ialloc_read_agi() returned error %d, agno %d", | |
e9c4d8bf | 2412 | __func__, error, pag_agno(pag)); |
7124fe0a DC |
2413 | return error; |
2414 | } | |
2415 | ||
2416 | /* | |
4536f2ad DC |
2417 | * Lookup the inode record for the given agino. If the record cannot be |
2418 | * found, then it's an invalid inode number and we should abort. Once | |
2419 | * we have a record, we need to ensure it contains the inode number | |
2420 | * we are looking up. | |
7124fe0a | 2421 | */ |
14dd46cf | 2422 | cur = xfs_inobt_init_cursor(pag, tp, agbp); |
4536f2ad | 2423 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i); |
7124fe0a DC |
2424 | if (!error) { |
2425 | if (i) | |
2426 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
2427 | if (!error && i == 0) | |
2451337d | 2428 | error = -EINVAL; |
7124fe0a DC |
2429 | } |
2430 | ||
2431 | xfs_trans_brelse(tp, agbp); | |
0b04b6b8 | 2432 | xfs_btree_del_cursor(cur, error); |
7124fe0a DC |
2433 | if (error) |
2434 | return error; | |
2435 | ||
4536f2ad DC |
2436 | /* check that the returned record contains the required inode */ |
2437 | if (rec.ir_startino > agino || | |
ef325959 | 2438 | rec.ir_startino + M_IGEO(mp)->ialloc_inos <= agino) |
2451337d | 2439 | return -EINVAL; |
4536f2ad | 2440 | |
7124fe0a | 2441 | /* for untrusted inodes check it is allocated first */ |
1920779e | 2442 | if ((flags & XFS_IGET_UNTRUSTED) && |
7124fe0a | 2443 | (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))) |
2451337d | 2444 | return -EINVAL; |
7124fe0a DC |
2445 | |
2446 | *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino); | |
2447 | *offset_agbno = agbno - *chunk_agbno; | |
2448 | return 0; | |
2449 | } | |
2450 | ||
1da177e4 | 2451 | /* |
94e1b69d | 2452 | * Return the location of the inode in imap, for mapping it into a buffer. |
1da177e4 | 2453 | */ |
1da177e4 | 2454 | int |
94e1b69d | 2455 | xfs_imap( |
498f0adb DC |
2456 | struct xfs_perag *pag, |
2457 | struct xfs_trans *tp, | |
7b13c515 DC |
2458 | xfs_ino_t ino, /* inode to locate */ |
2459 | struct xfs_imap *imap, /* location map structure */ | |
2460 | uint flags) /* flags for inode btree lookup */ | |
1da177e4 | 2461 | { |
e9c4d8bf | 2462 | struct xfs_mount *mp = pag_mount(pag); |
7b13c515 DC |
2463 | xfs_agblock_t agbno; /* block number of inode in the alloc group */ |
2464 | xfs_agino_t agino; /* inode number within alloc group */ | |
2465 | xfs_agblock_t chunk_agbno; /* first block in inode chunk */ | |
2466 | xfs_agblock_t cluster_agbno; /* first block in inode cluster */ | |
2467 | int error; /* error code */ | |
2468 | int offset; /* index of inode in its buffer */ | |
2469 | xfs_agblock_t offset_agbno; /* blks from chunk start to inode */ | |
1da177e4 LT |
2470 | |
2471 | ASSERT(ino != NULLFSINO); | |
94e1b69d | 2472 | |
1da177e4 LT |
2473 | /* |
2474 | * Split up the inode number into its parts. | |
2475 | */ | |
1da177e4 LT |
2476 | agino = XFS_INO_TO_AGINO(mp, ino); |
2477 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); | |
13325333 | 2478 | if (agbno >= xfs_ag_block_count(mp, pag_agno(pag)) || |
6abd82ab | 2479 | ino != xfs_agino_to_ino(pag, agino)) { |
7b13c515 | 2480 | error = -EINVAL; |
1da177e4 | 2481 | #ifdef DEBUG |
1920779e DC |
2482 | /* |
2483 | * Don't output diagnostic information for untrusted inodes | |
2484 | * as they can be invalid without implying corruption. | |
2485 | */ | |
2486 | if (flags & XFS_IGET_UNTRUSTED) | |
498f0adb | 2487 | return error; |
13325333 | 2488 | if (agbno >= xfs_ag_block_count(mp, pag_agno(pag))) { |
53487786 DC |
2489 | xfs_alert(mp, |
2490 | "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)", | |
2491 | __func__, (unsigned long long)agbno, | |
13325333 DC |
2492 | (unsigned long)xfs_ag_block_count(mp, |
2493 | pag_agno(pag))); | |
1da177e4 | 2494 | } |
6abd82ab | 2495 | if (ino != xfs_agino_to_ino(pag, agino)) { |
53487786 | 2496 | xfs_alert(mp, |
6abd82ab | 2497 | "%s: ino (0x%llx) != xfs_agino_to_ino() (0x%llx)", |
53487786 | 2498 | __func__, ino, |
6abd82ab | 2499 | xfs_agino_to_ino(pag, agino)); |
1da177e4 | 2500 | } |
745b1f47 | 2501 | xfs_stack_trace(); |
1da177e4 | 2502 | #endif /* DEBUG */ |
498f0adb | 2503 | return error; |
1da177e4 | 2504 | } |
94e1b69d | 2505 | |
7124fe0a DC |
2506 | /* |
2507 | * For bulkstat and handle lookups, we have an untrusted inode number | |
2508 | * that we have to verify is valid. We cannot do this just by reading | |
2509 | * the inode buffer as it may have been unlinked and removed leaving | |
2510 | * inodes in stale state on disk. Hence we have to do a btree lookup | |
2511 | * in all cases where an untrusted inode number is passed. | |
2512 | */ | |
1920779e | 2513 | if (flags & XFS_IGET_UNTRUSTED) { |
498f0adb | 2514 | error = xfs_imap_lookup(pag, tp, agino, agbno, |
7124fe0a DC |
2515 | &chunk_agbno, &offset_agbno, flags); |
2516 | if (error) | |
498f0adb | 2517 | return error; |
7124fe0a DC |
2518 | goto out_map; |
2519 | } | |
2520 | ||
94e1b69d CH |
2521 | /* |
2522 | * If the inode cluster size is the same as the blocksize or | |
2523 | * smaller we get to the buffer by simple arithmetics. | |
2524 | */ | |
ef325959 | 2525 | if (M_IGEO(mp)->blocks_per_cluster == 1) { |
1da177e4 LT |
2526 | offset = XFS_INO_TO_OFFSET(mp, ino); |
2527 | ASSERT(offset < mp->m_sb.sb_inopblock); | |
94e1b69d | 2528 | |
856a920a | 2529 | imap->im_blkno = xfs_agbno_to_daddr(pag, agbno); |
94e1b69d | 2530 | imap->im_len = XFS_FSB_TO_BB(mp, 1); |
755c7bf5 DW |
2531 | imap->im_boffset = (unsigned short)(offset << |
2532 | mp->m_sb.sb_inodelog); | |
498f0adb | 2533 | return 0; |
1da177e4 | 2534 | } |
94e1b69d | 2535 | |
94e1b69d CH |
2536 | /* |
2537 | * If the inode chunks are aligned then use simple maths to | |
2538 | * find the location. Otherwise we have to do a btree | |
2539 | * lookup to find the location. | |
2540 | */ | |
ef325959 DW |
2541 | if (M_IGEO(mp)->inoalign_mask) { |
2542 | offset_agbno = agbno & M_IGEO(mp)->inoalign_mask; | |
1da177e4 LT |
2543 | chunk_agbno = agbno - offset_agbno; |
2544 | } else { | |
498f0adb | 2545 | error = xfs_imap_lookup(pag, tp, agino, agbno, |
7124fe0a | 2546 | &chunk_agbno, &offset_agbno, flags); |
1da177e4 | 2547 | if (error) |
498f0adb | 2548 | return error; |
1da177e4 | 2549 | } |
94e1b69d | 2550 | |
7124fe0a | 2551 | out_map: |
1da177e4 LT |
2552 | ASSERT(agbno >= chunk_agbno); |
2553 | cluster_agbno = chunk_agbno + | |
ef325959 DW |
2554 | ((offset_agbno / M_IGEO(mp)->blocks_per_cluster) * |
2555 | M_IGEO(mp)->blocks_per_cluster); | |
1da177e4 LT |
2556 | offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + |
2557 | XFS_INO_TO_OFFSET(mp, ino); | |
94e1b69d | 2558 | |
856a920a | 2559 | imap->im_blkno = xfs_agbno_to_daddr(pag, cluster_agbno); |
ef325959 | 2560 | imap->im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster); |
755c7bf5 | 2561 | imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog); |
94e1b69d CH |
2562 | |
2563 | /* | |
2564 | * If the inode number maps to a block outside the bounds | |
2565 | * of the file system then return NULL rather than calling | |
2566 | * read_buf and panicing when we get an error from the | |
2567 | * driver. | |
2568 | */ | |
2569 | if ((imap->im_blkno + imap->im_len) > | |
2570 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { | |
53487786 DC |
2571 | xfs_alert(mp, |
2572 | "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)", | |
2573 | __func__, (unsigned long long) imap->im_blkno, | |
94e1b69d CH |
2574 | (unsigned long long) imap->im_len, |
2575 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); | |
498f0adb | 2576 | return -EINVAL; |
94e1b69d | 2577 | } |
498f0adb | 2578 | return 0; |
1da177e4 LT |
2579 | } |
2580 | ||
1da177e4 | 2581 | /* |
aafc3c24 BF |
2582 | * Log specified fields for the ag hdr (inode section). The growth of the agi |
2583 | * structure over time requires that we interpret the buffer as two logical | |
2584 | * regions delineated by the end of the unlinked list. This is due to the size | |
2585 | * of the hash table and its location in the middle of the agi. | |
2586 | * | |
2587 | * For example, a request to log a field before agi_unlinked and a field after | |
2588 | * agi_unlinked could cause us to log the entire hash table and use an excessive | |
2589 | * amount of log space. To avoid this behavior, log the region up through | |
2590 | * agi_unlinked in one call and the region after agi_unlinked through the end of | |
2591 | * the structure in another. | |
1da177e4 LT |
2592 | */ |
2593 | void | |
2594 | xfs_ialloc_log_agi( | |
0d1b9769 DC |
2595 | struct xfs_trans *tp, |
2596 | struct xfs_buf *bp, | |
2597 | uint32_t fields) | |
1da177e4 LT |
2598 | { |
2599 | int first; /* first byte number */ | |
2600 | int last; /* last byte number */ | |
2601 | static const short offsets[] = { /* field starting offsets */ | |
2602 | /* keep in sync with bit definitions */ | |
2603 | offsetof(xfs_agi_t, agi_magicnum), | |
2604 | offsetof(xfs_agi_t, agi_versionnum), | |
2605 | offsetof(xfs_agi_t, agi_seqno), | |
2606 | offsetof(xfs_agi_t, agi_length), | |
2607 | offsetof(xfs_agi_t, agi_count), | |
2608 | offsetof(xfs_agi_t, agi_root), | |
2609 | offsetof(xfs_agi_t, agi_level), | |
2610 | offsetof(xfs_agi_t, agi_freecount), | |
2611 | offsetof(xfs_agi_t, agi_newino), | |
2612 | offsetof(xfs_agi_t, agi_dirino), | |
2613 | offsetof(xfs_agi_t, agi_unlinked), | |
aafc3c24 BF |
2614 | offsetof(xfs_agi_t, agi_free_root), |
2615 | offsetof(xfs_agi_t, agi_free_level), | |
2a39946c | 2616 | offsetof(xfs_agi_t, agi_iblocks), |
1da177e4 LT |
2617 | sizeof(xfs_agi_t) |
2618 | }; | |
2619 | #ifdef DEBUG | |
370c782b | 2620 | struct xfs_agi *agi = bp->b_addr; |
1da177e4 | 2621 | |
69ef921b | 2622 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); |
1da177e4 | 2623 | #endif |
aafc3c24 | 2624 | |
1da177e4 | 2625 | /* |
aafc3c24 BF |
2626 | * Compute byte offsets for the first and last fields in the first |
2627 | * region and log the agi buffer. This only logs up through | |
2628 | * agi_unlinked. | |
1da177e4 | 2629 | */ |
aafc3c24 BF |
2630 | if (fields & XFS_AGI_ALL_BITS_R1) { |
2631 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1, | |
2632 | &first, &last); | |
2633 | xfs_trans_log_buf(tp, bp, first, last); | |
2634 | } | |
2635 | ||
1da177e4 | 2636 | /* |
aafc3c24 BF |
2637 | * Mask off the bits in the first region and calculate the first and |
2638 | * last field offsets for any bits in the second region. | |
1da177e4 | 2639 | */ |
aafc3c24 BF |
2640 | fields &= ~XFS_AGI_ALL_BITS_R1; |
2641 | if (fields) { | |
2642 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2, | |
2643 | &first, &last); | |
2644 | xfs_trans_log_buf(tp, bp, first, last); | |
2645 | } | |
1da177e4 LT |
2646 | } |
2647 | ||
a6a781a5 | 2648 | static xfs_failaddr_t |
612cfbfe | 2649 | xfs_agi_verify( |
2d7d1e7e | 2650 | struct xfs_buf *bp) |
3702ce6e | 2651 | { |
2d7d1e7e DW |
2652 | struct xfs_mount *mp = bp->b_mount; |
2653 | struct xfs_agi *agi = bp->b_addr; | |
2654 | xfs_failaddr_t fa; | |
2655 | uint32_t agi_seqno = be32_to_cpu(agi->agi_seqno); | |
2656 | uint32_t agi_length = be32_to_cpu(agi->agi_length); | |
2657 | int i; | |
3702ce6e | 2658 | |
38c26bfd | 2659 | if (xfs_has_crc(mp)) { |
a45086e2 | 2660 | if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid)) |
a6a781a5 | 2661 | return __this_address; |
370c782b | 2662 | if (!xfs_log_check_lsn(mp, be64_to_cpu(agi->agi_lsn))) |
a6a781a5 | 2663 | return __this_address; |
a45086e2 BF |
2664 | } |
2665 | ||
3702ce6e DC |
2666 | /* |
2667 | * Validate the magic number of the agi block. | |
2668 | */ | |
39708c20 | 2669 | if (!xfs_verify_magic(bp, agi->agi_magicnum)) |
a6a781a5 | 2670 | return __this_address; |
983d09ff | 2671 | if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum))) |
a6a781a5 | 2672 | return __this_address; |
3702ce6e | 2673 | |
2d7d1e7e DW |
2674 | fa = xfs_validate_ag_length(bp, agi_seqno, agi_length); |
2675 | if (fa) | |
2676 | return fa; | |
2677 | ||
d2a047f3 | 2678 | if (be32_to_cpu(agi->agi_level) < 1 || |
973975b7 | 2679 | be32_to_cpu(agi->agi_level) > M_IGEO(mp)->inobt_maxlevels) |
a6a781a5 | 2680 | return __this_address; |
d2a047f3 | 2681 | |
38c26bfd | 2682 | if (xfs_has_finobt(mp) && |
d2a047f3 | 2683 | (be32_to_cpu(agi->agi_free_level) < 1 || |
973975b7 | 2684 | be32_to_cpu(agi->agi_free_level) > M_IGEO(mp)->inobt_maxlevels)) |
a6a781a5 | 2685 | return __this_address; |
d2a047f3 | 2686 | |
9f96cc95 | 2687 | for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) { |
5089eaff | 2688 | if (agi->agi_unlinked[i] == cpu_to_be32(NULLAGINO)) |
9f96cc95 DC |
2689 | continue; |
2690 | if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i]))) | |
2691 | return __this_address; | |
2692 | } | |
2693 | ||
a6a781a5 | 2694 | return NULL; |
612cfbfe DC |
2695 | } |
2696 | ||
1813dd64 DC |
2697 | static void |
2698 | xfs_agi_read_verify( | |
612cfbfe DC |
2699 | struct xfs_buf *bp) |
2700 | { | |
dbd329f1 | 2701 | struct xfs_mount *mp = bp->b_mount; |
bc1a09b8 | 2702 | xfs_failaddr_t fa; |
983d09ff | 2703 | |
38c26bfd | 2704 | if (xfs_has_crc(mp) && |
ce5028cf | 2705 | !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF)) |
bc1a09b8 DW |
2706 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
2707 | else { | |
2708 | fa = xfs_agi_verify(bp); | |
2709 | if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI)) | |
2710 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
2711 | } | |
612cfbfe DC |
2712 | } |
2713 | ||
b0f539de | 2714 | static void |
1813dd64 | 2715 | xfs_agi_write_verify( |
612cfbfe DC |
2716 | struct xfs_buf *bp) |
2717 | { | |
dbd329f1 | 2718 | struct xfs_mount *mp = bp->b_mount; |
fb1755a6 | 2719 | struct xfs_buf_log_item *bip = bp->b_log_item; |
370c782b | 2720 | struct xfs_agi *agi = bp->b_addr; |
bc1a09b8 | 2721 | xfs_failaddr_t fa; |
983d09ff | 2722 | |
bc1a09b8 DW |
2723 | fa = xfs_agi_verify(bp); |
2724 | if (fa) { | |
2725 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
983d09ff DC |
2726 | return; |
2727 | } | |
2728 | ||
38c26bfd | 2729 | if (!xfs_has_crc(mp)) |
983d09ff DC |
2730 | return; |
2731 | ||
2732 | if (bip) | |
370c782b | 2733 | agi->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn); |
f1dbcd7e | 2734 | xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF); |
3702ce6e DC |
2735 | } |
2736 | ||
1813dd64 | 2737 | const struct xfs_buf_ops xfs_agi_buf_ops = { |
233135b7 | 2738 | .name = "xfs_agi", |
39708c20 | 2739 | .magic = { cpu_to_be32(XFS_AGI_MAGIC), cpu_to_be32(XFS_AGI_MAGIC) }, |
1813dd64 DC |
2740 | .verify_read = xfs_agi_read_verify, |
2741 | .verify_write = xfs_agi_write_verify, | |
b5572597 | 2742 | .verify_struct = xfs_agi_verify, |
1813dd64 DC |
2743 | }; |
2744 | ||
1da177e4 LT |
2745 | /* |
2746 | * Read in the allocation group header (inode allocation section) | |
2747 | */ | |
2748 | int | |
5e1be0fb | 2749 | xfs_read_agi( |
61021deb DC |
2750 | struct xfs_perag *pag, |
2751 | struct xfs_trans *tp, | |
549d3c9a | 2752 | xfs_buf_flags_t flags, |
61021deb | 2753 | struct xfs_buf **agibpp) |
1da177e4 | 2754 | { |
e9c4d8bf | 2755 | struct xfs_mount *mp = pag_mount(pag); |
5e1be0fb | 2756 | int error; |
1da177e4 | 2757 | |
c4ae021b | 2758 | trace_xfs_read_agi(pag); |
5e1be0fb CH |
2759 | |
2760 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, | |
e9c4d8bf | 2761 | XFS_AG_DADDR(mp, pag_agno(pag), XFS_AGI_DADDR(mp)), |
549d3c9a | 2762 | XFS_FSS_TO_BB(mp, 1), flags, agibpp, &xfs_agi_buf_ops); |
de6077ec DW |
2763 | if (xfs_metadata_is_sick(error)) |
2764 | xfs_ag_mark_sick(pag, XFS_SICK_AG_AGI); | |
1da177e4 LT |
2765 | if (error) |
2766 | return error; | |
200237d6 | 2767 | if (tp) |
61021deb | 2768 | xfs_trans_buf_set_type(tp, *agibpp, XFS_BLFT_AGI_BUF); |
5e1be0fb | 2769 | |
61021deb | 2770 | xfs_buf_set_ref(*agibpp, XFS_AGI_REF); |
5e1be0fb CH |
2771 | return 0; |
2772 | } | |
2773 | ||
a95fee40 DC |
2774 | /* |
2775 | * Read in the agi and initialise the per-ag data. If the caller supplies a | |
2776 | * @agibpp, return the locked AGI buffer to them, otherwise release it. | |
2777 | */ | |
5e1be0fb CH |
2778 | int |
2779 | xfs_ialloc_read_agi( | |
99b13c7f DC |
2780 | struct xfs_perag *pag, |
2781 | struct xfs_trans *tp, | |
549d3c9a | 2782 | int flags, |
a95fee40 | 2783 | struct xfs_buf **agibpp) |
5e1be0fb | 2784 | { |
a95fee40 | 2785 | struct xfs_buf *agibp; |
99b13c7f | 2786 | struct xfs_agi *agi; |
5e1be0fb CH |
2787 | int error; |
2788 | ||
c4ae021b | 2789 | trace_xfs_ialloc_read_agi(pag); |
d123031a | 2790 | |
549d3c9a DW |
2791 | error = xfs_read_agi(pag, tp, |
2792 | (flags & XFS_IALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0, | |
2793 | &agibp); | |
5e1be0fb CH |
2794 | if (error) |
2795 | return error; | |
2796 | ||
a95fee40 | 2797 | agi = agibp->b_addr; |
7ac2ff8b | 2798 | if (!xfs_perag_initialised_agi(pag)) { |
16259e7d | 2799 | pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); |
92821e2b | 2800 | pag->pagi_count = be32_to_cpu(agi->agi_count); |
7ac2ff8b | 2801 | set_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate); |
1da177e4 | 2802 | } |
1da177e4 | 2803 | |
5e1be0fb CH |
2804 | /* |
2805 | * It's possible for these to be out of sync if | |
2806 | * we are in the middle of a forced shutdown. | |
2807 | */ | |
2808 | ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || | |
e9c4d8bf | 2809 | xfs_is_shutdown(pag_mount(pag))); |
a95fee40 DC |
2810 | if (agibpp) |
2811 | *agibpp = agibp; | |
2812 | else | |
2813 | xfs_trans_brelse(tp, agibp); | |
92821e2b DC |
2814 | return 0; |
2815 | } | |
91fb9afc | 2816 | |
efc0845f DW |
2817 | /* How many inodes are backed by inode clusters ondisk? */ |
2818 | STATIC int | |
2819 | xfs_ialloc_count_ondisk( | |
2820 | struct xfs_btree_cur *cur, | |
2821 | xfs_agino_t low, | |
2822 | xfs_agino_t high, | |
2823 | unsigned int *allocated) | |
2e001266 DW |
2824 | { |
2825 | struct xfs_inobt_rec_incore irec; | |
efc0845f DW |
2826 | unsigned int ret = 0; |
2827 | int has_record; | |
2828 | int error; | |
2e001266 | 2829 | |
2e001266 | 2830 | error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record); |
efc0845f DW |
2831 | if (error) |
2832 | return error; | |
2833 | ||
2834 | while (has_record) { | |
2835 | unsigned int i, hole_idx; | |
2836 | ||
2e001266 | 2837 | error = xfs_inobt_get_rec(cur, &irec, &has_record); |
efc0845f DW |
2838 | if (error) |
2839 | return error; | |
2840 | if (irec.ir_startino > high) | |
2e001266 DW |
2841 | break; |
2842 | ||
efc0845f DW |
2843 | for (i = 0; i < XFS_INODES_PER_CHUNK; i++) { |
2844 | if (irec.ir_startino + i < low) | |
2e001266 | 2845 | continue; |
efc0845f DW |
2846 | if (irec.ir_startino + i > high) |
2847 | break; | |
2848 | ||
2849 | hole_idx = i / XFS_INODES_PER_HOLEMASK_BIT; | |
2850 | if (!(irec.ir_holemask & (1U << hole_idx))) | |
2851 | ret++; | |
2e001266 DW |
2852 | } |
2853 | ||
2854 | error = xfs_btree_increment(cur, 0, &has_record); | |
efc0845f DW |
2855 | if (error) |
2856 | return error; | |
2e001266 | 2857 | } |
efc0845f DW |
2858 | |
2859 | *allocated = ret; | |
2860 | return 0; | |
2e001266 DW |
2861 | } |
2862 | ||
2863 | /* Is there an inode record covering a given extent? */ | |
2864 | int | |
2865 | xfs_ialloc_has_inodes_at_extent( | |
2866 | struct xfs_btree_cur *cur, | |
2867 | xfs_agblock_t bno, | |
2868 | xfs_extlen_t len, | |
efc0845f | 2869 | enum xbtree_recpacking *outcome) |
2e001266 | 2870 | { |
efc0845f DW |
2871 | xfs_agino_t agino; |
2872 | xfs_agino_t last_agino; | |
2873 | unsigned int allocated; | |
2874 | int error; | |
2e001266 | 2875 | |
efc0845f DW |
2876 | agino = XFS_AGB_TO_AGINO(cur->bc_mp, bno); |
2877 | last_agino = XFS_AGB_TO_AGINO(cur->bc_mp, bno + len) - 1; | |
2e001266 | 2878 | |
efc0845f DW |
2879 | error = xfs_ialloc_count_ondisk(cur, agino, last_agino, &allocated); |
2880 | if (error) | |
2881 | return error; | |
2882 | ||
2883 | if (allocated == 0) | |
2884 | *outcome = XBTREE_RECPACKING_EMPTY; | |
2885 | else if (allocated == last_agino - agino + 1) | |
2886 | *outcome = XBTREE_RECPACKING_FULL; | |
2887 | else | |
2888 | *outcome = XBTREE_RECPACKING_SPARSE; | |
2889 | return 0; | |
2e001266 DW |
2890 | } |
2891 | ||
2892 | struct xfs_ialloc_count_inodes { | |
2893 | xfs_agino_t count; | |
2894 | xfs_agino_t freecount; | |
2895 | }; | |
2896 | ||
2897 | /* Record inode counts across all inobt records. */ | |
2898 | STATIC int | |
2899 | xfs_ialloc_count_inodes_rec( | |
2900 | struct xfs_btree_cur *cur, | |
159eb69d | 2901 | const union xfs_btree_rec *rec, |
2e001266 DW |
2902 | void *priv) |
2903 | { | |
2904 | struct xfs_inobt_rec_incore irec; | |
2905 | struct xfs_ialloc_count_inodes *ci = priv; | |
ee12eaaa | 2906 | xfs_failaddr_t fa; |
2e001266 DW |
2907 | |
2908 | xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec); | |
77a530e6 | 2909 | fa = xfs_inobt_check_irec(to_perag(cur->bc_group), &irec); |
ee12eaaa DW |
2910 | if (fa) |
2911 | return xfs_inobt_complain_bad_rec(cur, fa, &irec); | |
366a0b8d | 2912 | |
2e001266 DW |
2913 | ci->count += irec.ir_count; |
2914 | ci->freecount += irec.ir_freecount; | |
2915 | ||
2916 | return 0; | |
2917 | } | |
2918 | ||
2919 | /* Count allocated and free inodes under an inobt. */ | |
2920 | int | |
2921 | xfs_ialloc_count_inodes( | |
2922 | struct xfs_btree_cur *cur, | |
2923 | xfs_agino_t *count, | |
2924 | xfs_agino_t *freecount) | |
2925 | { | |
2926 | struct xfs_ialloc_count_inodes ci = {0}; | |
2927 | int error; | |
2928 | ||
ec793e69 | 2929 | ASSERT(xfs_btree_is_ino(cur->bc_ops)); |
2e001266 DW |
2930 | error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci); |
2931 | if (error) | |
2932 | return error; | |
2933 | ||
2934 | *count = ci.count; | |
2935 | *freecount = ci.freecount; | |
2936 | return 0; | |
2937 | } | |
494dba7b DW |
2938 | |
2939 | /* | |
2940 | * Initialize inode-related geometry information. | |
2941 | * | |
2942 | * Compute the inode btree min and max levels and set maxicount. | |
2943 | * | |
2944 | * Set the inode cluster size. This may still be overridden by the file | |
2945 | * system block size if it is larger than the chosen cluster size. | |
2946 | * | |
2947 | * For v5 filesystems, scale the cluster size with the inode size to keep a | |
2948 | * constant ratio of inode per cluster buffer, but only if mkfs has set the | |
2949 | * inode alignment value appropriately for larger cluster sizes. | |
2950 | * | |
2951 | * Then compute the inode cluster alignment information. | |
2952 | */ | |
2953 | void | |
2954 | xfs_ialloc_setup_geometry( | |
2955 | struct xfs_mount *mp) | |
2956 | { | |
2957 | struct xfs_sb *sbp = &mp->m_sb; | |
2958 | struct xfs_ino_geometry *igeo = M_IGEO(mp); | |
2959 | uint64_t icount; | |
2960 | uint inodes; | |
2961 | ||
f93e5436 | 2962 | igeo->new_diflags2 = 0; |
ebd9027d | 2963 | if (xfs_has_bigtime(mp)) |
f93e5436 | 2964 | igeo->new_diflags2 |= XFS_DIFLAG2_BIGTIME; |
9b7d16e3 CB |
2965 | if (xfs_has_large_extent_counts(mp)) |
2966 | igeo->new_diflags2 |= XFS_DIFLAG2_NREXT64; | |
f93e5436 | 2967 | |
494dba7b DW |
2968 | /* Compute inode btree geometry. */ |
2969 | igeo->agino_log = sbp->sb_inopblog + sbp->sb_agblklog; | |
411a7125 DW |
2970 | igeo->inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, true); |
2971 | igeo->inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, false); | |
494dba7b DW |
2972 | igeo->inobt_mnr[0] = igeo->inobt_mxr[0] / 2; |
2973 | igeo->inobt_mnr[1] = igeo->inobt_mxr[1] / 2; | |
2974 | ||
2975 | igeo->ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK, | |
2976 | sbp->sb_inopblock); | |
2977 | igeo->ialloc_blks = igeo->ialloc_inos >> sbp->sb_inopblog; | |
2978 | ||
2979 | if (sbp->sb_spino_align) | |
2980 | igeo->ialloc_min_blks = sbp->sb_spino_align; | |
2981 | else | |
2982 | igeo->ialloc_min_blks = igeo->ialloc_blks; | |
2983 | ||
2984 | /* Compute and fill in value of m_ino_geo.inobt_maxlevels. */ | |
2985 | inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG; | |
2986 | igeo->inobt_maxlevels = xfs_btree_compute_maxlevels(igeo->inobt_mnr, | |
2987 | inodes); | |
0ed5f735 | 2988 | ASSERT(igeo->inobt_maxlevels <= xfs_iallocbt_maxlevels_ondisk()); |
494dba7b | 2989 | |
c94613fe DW |
2990 | /* |
2991 | * Set the maximum inode count for this filesystem, being careful not | |
2992 | * to use obviously garbage sb_inopblog/sb_inopblock values. Regular | |
2993 | * users should never get here due to failing sb verification, but | |
2994 | * certain users (xfs_db) need to be usable even with corrupt metadata. | |
2995 | */ | |
2996 | if (sbp->sb_imax_pct && igeo->ialloc_blks) { | |
494dba7b DW |
2997 | /* |
2998 | * Make sure the maximum inode count is a multiple | |
2999 | * of the units we allocate inodes in. | |
3000 | */ | |
3001 | icount = sbp->sb_dblocks * sbp->sb_imax_pct; | |
3002 | do_div(icount, 100); | |
3003 | do_div(icount, igeo->ialloc_blks); | |
3004 | igeo->maxicount = XFS_FSB_TO_INO(mp, | |
3005 | icount * igeo->ialloc_blks); | |
3006 | } else { | |
3007 | igeo->maxicount = 0; | |
3008 | } | |
3009 | ||
490d451f DW |
3010 | /* |
3011 | * Compute the desired size of an inode cluster buffer size, which | |
3012 | * starts at 8K and (on v5 filesystems) scales up with larger inode | |
3013 | * sizes. | |
3014 | * | |
3015 | * Preserve the desired inode cluster size because the sparse inodes | |
3016 | * feature uses that desired size (not the actual size) to compute the | |
3017 | * sparse inode alignment. The mount code validates this value, so we | |
3018 | * cannot change the behavior. | |
3019 | */ | |
3020 | igeo->inode_cluster_size_raw = XFS_INODE_BIG_CLUSTER_SIZE; | |
ebd9027d | 3021 | if (xfs_has_v3inodes(mp)) { |
490d451f | 3022 | int new_size = igeo->inode_cluster_size_raw; |
494dba7b DW |
3023 | |
3024 | new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE; | |
3025 | if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size)) | |
490d451f | 3026 | igeo->inode_cluster_size_raw = new_size; |
494dba7b DW |
3027 | } |
3028 | ||
3029 | /* Calculate inode cluster ratios. */ | |
490d451f | 3030 | if (igeo->inode_cluster_size_raw > mp->m_sb.sb_blocksize) |
494dba7b | 3031 | igeo->blocks_per_cluster = XFS_B_TO_FSBT(mp, |
490d451f | 3032 | igeo->inode_cluster_size_raw); |
494dba7b DW |
3033 | else |
3034 | igeo->blocks_per_cluster = 1; | |
490d451f | 3035 | igeo->inode_cluster_size = XFS_FSB_TO_B(mp, igeo->blocks_per_cluster); |
494dba7b DW |
3036 | igeo->inodes_per_cluster = XFS_FSB_TO_INO(mp, igeo->blocks_per_cluster); |
3037 | ||
3038 | /* Calculate inode cluster alignment. */ | |
ebd9027d | 3039 | if (xfs_has_align(mp) && |
494dba7b DW |
3040 | mp->m_sb.sb_inoalignmt >= igeo->blocks_per_cluster) |
3041 | igeo->cluster_align = mp->m_sb.sb_inoalignmt; | |
3042 | else | |
3043 | igeo->cluster_align = 1; | |
3044 | igeo->inoalign_mask = igeo->cluster_align - 1; | |
3045 | igeo->cluster_align_inodes = XFS_FSB_TO_INO(mp, igeo->cluster_align); | |
3046 | ||
3047 | /* | |
3048 | * If we are using stripe alignment, check whether | |
3049 | * the stripe unit is a multiple of the inode alignment | |
3050 | */ | |
3051 | if (mp->m_dalign && igeo->inoalign_mask && | |
3052 | !(mp->m_dalign & igeo->inoalign_mask)) | |
3053 | igeo->ialloc_align = mp->m_dalign; | |
3054 | else | |
3055 | igeo->ialloc_align = 0; | |
7df7c204 PR |
3056 | |
3057 | if (mp->m_sb.sb_blocksize > PAGE_SIZE) | |
3058 | igeo->min_folio_order = mp->m_sb.sb_blocklog - PAGE_SHIFT; | |
3059 | else | |
3060 | igeo->min_folio_order = 0; | |
494dba7b | 3061 | } |
13eaec4b DW |
3062 | |
3063 | /* Compute the location of the root directory inode that is laid out by mkfs. */ | |
3064 | xfs_ino_t | |
3065 | xfs_ialloc_calc_rootino( | |
3066 | struct xfs_mount *mp, | |
3067 | int sunit) | |
3068 | { | |
3069 | struct xfs_ino_geometry *igeo = M_IGEO(mp); | |
3070 | xfs_agblock_t first_bno; | |
3071 | ||
3072 | /* | |
3073 | * Pre-calculate the geometry of AG 0. We know what it looks like | |
3074 | * because libxfs knows how to create allocation groups now. | |
3075 | * | |
3076 | * first_bno is the first block in which mkfs could possibly have | |
3077 | * allocated the root directory inode, once we factor in the metadata | |
3078 | * that mkfs formats before it. Namely, the four AG headers... | |
3079 | */ | |
3080 | first_bno = howmany(4 * mp->m_sb.sb_sectsize, mp->m_sb.sb_blocksize); | |
3081 | ||
3082 | /* ...the two free space btree roots... */ | |
3083 | first_bno += 2; | |
3084 | ||
3085 | /* ...the inode btree root... */ | |
3086 | first_bno += 1; | |
3087 | ||
3088 | /* ...the initial AGFL... */ | |
3089 | first_bno += xfs_alloc_min_freelist(mp, NULL); | |
3090 | ||
3091 | /* ...the free inode btree root... */ | |
ebd9027d | 3092 | if (xfs_has_finobt(mp)) |
13eaec4b DW |
3093 | first_bno++; |
3094 | ||
3095 | /* ...the reverse mapping btree root... */ | |
ebd9027d | 3096 | if (xfs_has_rmapbt(mp)) |
13eaec4b DW |
3097 | first_bno++; |
3098 | ||
3099 | /* ...the reference count btree... */ | |
ebd9027d | 3100 | if (xfs_has_reflink(mp)) |
13eaec4b DW |
3101 | first_bno++; |
3102 | ||
3103 | /* | |
3104 | * ...and the log, if it is allocated in the first allocation group. | |
3105 | * | |
3106 | * This can happen with filesystems that only have a single | |
3107 | * allocation group, or very odd geometries created by old mkfs | |
3108 | * versions on very small filesystems. | |
3109 | */ | |
36029dee | 3110 | if (xfs_ag_contains_log(mp, 0)) |
13eaec4b DW |
3111 | first_bno += mp->m_sb.sb_logblocks; |
3112 | ||
3113 | /* | |
3114 | * Now round first_bno up to whatever allocation alignment is given | |
3115 | * by the filesystem or was passed in. | |
3116 | */ | |
ebd9027d | 3117 | if (xfs_has_dalign(mp) && igeo->ialloc_align > 0) |
13eaec4b | 3118 | first_bno = roundup(first_bno, sunit); |
ebd9027d | 3119 | else if (xfs_has_align(mp) && |
13eaec4b DW |
3120 | mp->m_sb.sb_inoalignmt > 1) |
3121 | first_bno = roundup(first_bno, mp->m_sb.sb_inoalignmt); | |
3122 | ||
3123 | return XFS_AGINO_TO_INO(mp, 0, XFS_AGB_TO_AGINO(mp, first_bno)); | |
3124 | } | |
da062d16 DW |
3125 | |
3126 | /* | |
3127 | * Ensure there are not sparse inode clusters that cross the new EOAG. | |
3128 | * | |
3129 | * This is a no-op for non-spinode filesystems since clusters are always fully | |
3130 | * allocated and checking the bnobt suffices. However, a spinode filesystem | |
3131 | * could have a record where the upper inodes are free blocks. If those blocks | |
3132 | * were removed from the filesystem, the inode record would extend beyond EOAG, | |
3133 | * which will be flagged as corruption. | |
3134 | */ | |
3135 | int | |
3136 | xfs_ialloc_check_shrink( | |
dedab3e4 | 3137 | struct xfs_perag *pag, |
da062d16 | 3138 | struct xfs_trans *tp, |
da062d16 DW |
3139 | struct xfs_buf *agibp, |
3140 | xfs_agblock_t new_length) | |
3141 | { | |
3142 | struct xfs_inobt_rec_incore rec; | |
3143 | struct xfs_btree_cur *cur; | |
bab8b795 | 3144 | xfs_agino_t agino; |
da062d16 DW |
3145 | int has; |
3146 | int error; | |
3147 | ||
e9c4d8bf | 3148 | if (!xfs_has_sparseinodes(pag_mount(pag))) |
da062d16 DW |
3149 | return 0; |
3150 | ||
14dd46cf | 3151 | cur = xfs_inobt_init_cursor(pag, tp, agibp); |
da062d16 DW |
3152 | |
3153 | /* Look up the inobt record that would correspond to the new EOFS. */ | |
e9c4d8bf | 3154 | agino = XFS_AGB_TO_AGINO(pag_mount(pag), new_length); |
da062d16 DW |
3155 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has); |
3156 | if (error || !has) | |
3157 | goto out; | |
3158 | ||
3159 | error = xfs_inobt_get_rec(cur, &rec, &has); | |
3160 | if (error) | |
3161 | goto out; | |
3162 | ||
3163 | if (!has) { | |
baf44fa5 | 3164 | xfs_ag_mark_sick(pag, XFS_SICK_AG_INOBT); |
da062d16 DW |
3165 | error = -EFSCORRUPTED; |
3166 | goto out; | |
3167 | } | |
3168 | ||
3169 | /* If the record covers inodes that would be beyond EOFS, bail out. */ | |
3170 | if (rec.ir_startino + XFS_INODES_PER_CHUNK > agino) { | |
3171 | error = -ENOSPC; | |
3172 | goto out; | |
3173 | } | |
3174 | out: | |
3175 | xfs_btree_del_cursor(cur, error); | |
da062d16 DW |
3176 | return error; |
3177 | } |