]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
da23017d NS |
2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
5000d01d | 4 | * |
da23017d NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
2bd0ea18 | 7 | * published by the Free Software Foundation. |
5000d01d | 8 | * |
da23017d NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
5000d01d | 13 | * |
da23017d NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
2bd0ea18 | 17 | */ |
9c799827 | 18 | #include "libxfs_priv.h" |
b626fb59 DC |
19 | #include "xfs_fs.h" |
20 | #include "xfs_shared.h" | |
21 | #include "xfs_format.h" | |
22 | #include "xfs_log_format.h" | |
23 | #include "xfs_trans_resv.h" | |
24 | #include "xfs_bit.h" | |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_mount.h" | |
27 | #include "xfs_inode.h" | |
28 | #include "xfs_btree.h" | |
29 | #include "xfs_ialloc.h" | |
30 | #include "xfs_ialloc_btree.h" | |
31 | #include "xfs_alloc.h" | |
32 | #include "xfs_bmap.h" | |
33 | #include "xfs_cksum.h" | |
34 | #include "xfs_trans.h" | |
35 | #include "xfs_trace.h" | |
2bd0ea18 | 36 | |
2bd0ea18 NS |
37 | |
38 | /* | |
39 | * Allocation group level functions. | |
40 | */ | |
5e656dbb BN |
41 | static inline int |
42 | xfs_ialloc_cluster_alignment( | |
5a35bf2c | 43 | struct xfs_mount *mp) |
5e656dbb | 44 | { |
5a35bf2c DC |
45 | if (xfs_sb_version_hasalign(&mp->m_sb) && |
46 | mp->m_sb.sb_inoalignmt >= | |
47 | XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) | |
48 | return mp->m_sb.sb_inoalignmt; | |
5e656dbb BN |
49 | return 1; |
50 | } | |
2bd0ea18 | 51 | |
b194c7d8 | 52 | /* |
56b2de80 | 53 | * Lookup a record by ino in the btree given by cur. |
b194c7d8 BN |
54 | */ |
55 | int /* error */ | |
56b2de80 | 56 | xfs_inobt_lookup( |
b194c7d8 BN |
57 | struct xfs_btree_cur *cur, /* btree cursor */ |
58 | xfs_agino_t ino, /* starting inode of chunk */ | |
56b2de80 | 59 | xfs_lookup_t dir, /* <=, >=, == */ |
b194c7d8 BN |
60 | int *stat) /* success/failure */ |
61 | { | |
62 | cur->bc_rec.i.ir_startino = ino; | |
11640e30 BF |
63 | cur->bc_rec.i.ir_holemask = 0; |
64 | cur->bc_rec.i.ir_count = 0; | |
56b2de80 DC |
65 | cur->bc_rec.i.ir_freecount = 0; |
66 | cur->bc_rec.i.ir_free = 0; | |
67 | return xfs_btree_lookup(cur, dir, stat); | |
b194c7d8 BN |
68 | } |
69 | ||
70 | /* | |
56b2de80 | 71 | * Update the record referred to by cur to the value given. |
b194c7d8 BN |
72 | * This either works (return 0) or gets an EFSCORRUPTED error. |
73 | */ | |
74 | STATIC int /* error */ | |
75 | xfs_inobt_update( | |
76 | struct xfs_btree_cur *cur, /* btree cursor */ | |
56b2de80 | 77 | xfs_inobt_rec_incore_t *irec) /* btree record */ |
b194c7d8 BN |
78 | { |
79 | union xfs_btree_rec rec; | |
80 | ||
56b2de80 | 81 | rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino); |
11640e30 BF |
82 | if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) { |
83 | rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask); | |
84 | rec.inobt.ir_u.sp.ir_count = irec->ir_count; | |
85 | rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount; | |
86 | } else { | |
87 | /* ir_holemask/ir_count not supported on-disk */ | |
88 | rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount); | |
89 | } | |
56b2de80 | 90 | rec.inobt.ir_free = cpu_to_be64(irec->ir_free); |
b194c7d8 BN |
91 | return xfs_btree_update(cur, &rec); |
92 | } | |
93 | ||
94 | /* | |
95 | * Get the data from the pointed-to record. | |
96 | */ | |
97 | int /* error */ | |
98 | xfs_inobt_get_rec( | |
99 | struct xfs_btree_cur *cur, /* btree cursor */ | |
56b2de80 | 100 | xfs_inobt_rec_incore_t *irec, /* btree record */ |
b194c7d8 BN |
101 | int *stat) /* output: success/failure */ |
102 | { | |
103 | union xfs_btree_rec *rec; | |
104 | int error; | |
105 | ||
106 | error = xfs_btree_get_rec(cur, &rec, stat); | |
11640e30 BF |
107 | if (error || *stat == 0) |
108 | return error; | |
109 | ||
110 | irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino); | |
111 | if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) { | |
112 | irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask); | |
113 | irec->ir_count = rec->inobt.ir_u.sp.ir_count; | |
114 | irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount; | |
115 | } else { | |
116 | /* | |
117 | * ir_holemask/ir_count not supported on-disk. Fill in hardcoded | |
118 | * values for full inode chunks. | |
119 | */ | |
120 | irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL; | |
121 | irec->ir_count = XFS_INODES_PER_CHUNK; | |
122 | irec->ir_freecount = | |
123 | be32_to_cpu(rec->inobt.ir_u.f.ir_freecount); | |
b194c7d8 | 124 | } |
11640e30 BF |
125 | irec->ir_free = be64_to_cpu(rec->inobt.ir_free); |
126 | ||
127 | return 0; | |
b194c7d8 BN |
128 | } |
129 | ||
3c699279 BF |
130 | /* |
131 | * Insert a single inobt record. Cursor must already point to desired location. | |
132 | */ | |
133 | STATIC int | |
134 | xfs_inobt_insert_rec( | |
135 | struct xfs_btree_cur *cur, | |
11640e30 BF |
136 | __uint16_t holemask, |
137 | __uint8_t count, | |
3c699279 BF |
138 | __int32_t freecount, |
139 | xfs_inofree_t free, | |
140 | int *stat) | |
141 | { | |
11640e30 BF |
142 | cur->bc_rec.i.ir_holemask = holemask; |
143 | cur->bc_rec.i.ir_count = count; | |
3c699279 BF |
144 | cur->bc_rec.i.ir_freecount = freecount; |
145 | cur->bc_rec.i.ir_free = free; | |
146 | return xfs_btree_insert(cur, stat); | |
147 | } | |
148 | ||
149 | /* | |
150 | * Insert records describing a newly allocated inode chunk into the inobt. | |
151 | */ | |
152 | STATIC int | |
153 | xfs_inobt_insert( | |
154 | struct xfs_mount *mp, | |
155 | struct xfs_trans *tp, | |
156 | struct xfs_buf *agbp, | |
157 | xfs_agino_t newino, | |
158 | xfs_agino_t newlen, | |
159 | xfs_btnum_t btnum) | |
160 | { | |
161 | struct xfs_btree_cur *cur; | |
162 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); | |
163 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); | |
164 | xfs_agino_t thisino; | |
165 | int i; | |
166 | int error; | |
167 | ||
168 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum); | |
169 | ||
170 | for (thisino = newino; | |
171 | thisino < newino + newlen; | |
172 | thisino += XFS_INODES_PER_CHUNK) { | |
173 | error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i); | |
174 | if (error) { | |
175 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
176 | return error; | |
177 | } | |
178 | ASSERT(i == 0); | |
179 | ||
11640e30 BF |
180 | error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL, |
181 | XFS_INODES_PER_CHUNK, | |
182 | XFS_INODES_PER_CHUNK, | |
3c699279 BF |
183 | XFS_INOBT_ALL_FREE, &i); |
184 | if (error) { | |
185 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
186 | return error; | |
187 | } | |
188 | ASSERT(i == 1); | |
189 | } | |
190 | ||
191 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
56b2de80 DC |
196 | /* |
197 | * Verify that the number of free inodes in the AGI is correct. | |
198 | */ | |
199 | #ifdef DEBUG | |
200 | STATIC int | |
201 | xfs_check_agi_freecount( | |
202 | struct xfs_btree_cur *cur, | |
203 | struct xfs_agi *agi) | |
204 | { | |
205 | if (cur->bc_nlevels == 1) { | |
206 | xfs_inobt_rec_incore_t rec; | |
207 | int freecount = 0; | |
208 | int error; | |
209 | int i; | |
210 | ||
211 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); | |
212 | if (error) | |
213 | return error; | |
214 | ||
215 | do { | |
216 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
217 | if (error) | |
218 | return error; | |
219 | ||
220 | if (i) { | |
221 | freecount += rec.ir_freecount; | |
222 | error = xfs_btree_increment(cur, 0, &i); | |
223 | if (error) | |
224 | return error; | |
225 | } | |
226 | } while (i == 1); | |
227 | ||
228 | if (!XFS_FORCED_SHUTDOWN(cur->bc_mp)) | |
229 | ASSERT(freecount == be32_to_cpu(agi->agi_freecount)); | |
230 | } | |
231 | return 0; | |
232 | } | |
233 | #else | |
234 | #define xfs_check_agi_freecount(cur, agi) 0 | |
235 | #endif | |
236 | ||
237 | /* | |
e9d35108 DC |
238 | * Initialise a new set of inodes. When called without a transaction context |
239 | * (e.g. from recovery) we initiate a delayed write of the inode buffers rather | |
240 | * than logging them (which in a transaction context puts them into the AIL | |
241 | * for writeback rather than the xfsbufd queue). | |
56b2de80 | 242 | */ |
e9d35108 | 243 | int |
56b2de80 DC |
244 | xfs_ialloc_inode_init( |
245 | struct xfs_mount *mp, | |
246 | struct xfs_trans *tp, | |
e9d35108 | 247 | struct list_head *buffer_list, |
fe8d48ac | 248 | int icount, |
56b2de80 DC |
249 | xfs_agnumber_t agno, |
250 | xfs_agblock_t agbno, | |
251 | xfs_agblock_t length, | |
252 | unsigned int gen) | |
253 | { | |
254 | struct xfs_buf *fbuf; | |
255 | struct xfs_dinode *free; | |
ff105f75 | 256 | int nbufs, blks_per_cluster, inodes_per_cluster; |
56b2de80 DC |
257 | int version; |
258 | int i, j; | |
259 | xfs_daddr_t d; | |
41ce5f36 | 260 | xfs_ino_t ino = 0; |
56b2de80 DC |
261 | |
262 | /* | |
ff105f75 DC |
263 | * Loop over the new block(s), filling in the inodes. For small block |
264 | * sizes, manipulate the inodes in buffers which are multiples of the | |
265 | * blocks size. | |
56b2de80 | 266 | */ |
ff105f75 DC |
267 | blks_per_cluster = xfs_icluster_size_fsb(mp); |
268 | inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog; | |
269 | nbufs = length / blks_per_cluster; | |
56b2de80 DC |
270 | |
271 | /* | |
e9d35108 DC |
272 | * Figure out what version number to use in the inodes we create. If |
273 | * the superblock version has caught up to the one that supports the new | |
274 | * inode format, then use the new inode version. Otherwise use the old | |
275 | * version so that old kernels will continue to be able to use the file | |
276 | * system. | |
41ce5f36 DC |
277 | * |
278 | * For v3 inodes, we also need to write the inode number into the inode, | |
279 | * so calculate the first inode number of the chunk here as | |
e9d35108 DC |
280 | * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not |
281 | * across multiple filesystem blocks (such as a cluster) and so cannot | |
282 | * be used in the cluster buffer loop below. | |
283 | * | |
284 | * Further, because we are writing the inode directly into the buffer | |
285 | * and calculating a CRC on the entire inode, we have ot log the entire | |
286 | * inode so that the entire range the CRC covers is present in the log. | |
287 | * That means for v3 inode we log the entire buffer rather than just the | |
288 | * inode cores. | |
56b2de80 | 289 | */ |
41ce5f36 DC |
290 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
291 | version = 3; | |
292 | ino = XFS_AGINO_TO_INO(mp, agno, | |
293 | XFS_OFFBNO_TO_AGINO(mp, agbno, 0)); | |
e9d35108 DC |
294 | |
295 | /* | |
296 | * log the initialisation that is about to take place as an | |
297 | * logical operation. This means the transaction does not | |
298 | * need to log the physical changes to the inode buffers as log | |
299 | * recovery will know what initialisation is actually needed. | |
300 | * Hence we only need to log the buffers as "ordered" buffers so | |
301 | * they track in the AIL as if they were physically logged. | |
302 | */ | |
303 | if (tp) | |
fe8d48ac | 304 | xfs_icreate_log(tp, agno, agbno, icount, |
e9d35108 | 305 | mp->m_sb.sb_inodesize, length, gen); |
ff105f75 | 306 | } else |
56b2de80 | 307 | version = 2; |
56b2de80 DC |
308 | |
309 | for (j = 0; j < nbufs; j++) { | |
310 | /* | |
311 | * Get the block. | |
312 | */ | |
313 | d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster)); | |
314 | fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, | |
315 | mp->m_bsize * blks_per_cluster, | |
a2ceac1f DC |
316 | XBF_UNMAPPED); |
317 | if (!fbuf) | |
12b53197 | 318 | return -ENOMEM; |
e9d35108 DC |
319 | |
320 | /* Initialize the inode buffers and log them appropriately. */ | |
a2ceac1f | 321 | fbuf->b_ops = &xfs_inode_buf_ops; |
e9d35108 | 322 | xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length)); |
ff105f75 | 323 | for (i = 0; i < inodes_per_cluster; i++) { |
56b2de80 | 324 | int ioffset = i << mp->m_sb.sb_inodelog; |
41ce5f36 | 325 | uint isize = xfs_dinode_size(version); |
56b2de80 DC |
326 | |
327 | free = xfs_make_iptr(mp, fbuf, i); | |
328 | free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC); | |
329 | free->di_version = version; | |
330 | free->di_gen = cpu_to_be32(gen); | |
331 | free->di_next_unlinked = cpu_to_be32(NULLAGINO); | |
41ce5f36 DC |
332 | |
333 | if (version == 3) { | |
334 | free->di_ino = cpu_to_be64(ino); | |
335 | ino++; | |
336 | uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid); | |
337 | xfs_dinode_calc_crc(mp, free); | |
e9d35108 DC |
338 | } else if (tp) { |
339 | /* just log the inode core */ | |
340 | xfs_trans_log_buf(tp, fbuf, ioffset, | |
341 | ioffset + isize - 1); | |
41ce5f36 | 342 | } |
e9d35108 | 343 | } |
41ce5f36 | 344 | |
e9d35108 DC |
345 | if (tp) { |
346 | /* | |
347 | * Mark the buffer as an inode allocation buffer so it | |
348 | * sticks in AIL at the point of this allocation | |
349 | * transaction. This ensures the they are on disk before | |
350 | * the tail of the log can be moved past this | |
351 | * transaction (i.e. by preventing relogging from moving | |
352 | * it forward in the log). | |
353 | */ | |
354 | xfs_trans_inode_alloc_buf(tp, fbuf); | |
355 | if (version == 3) { | |
356 | /* | |
357 | * Mark the buffer as ordered so that they are | |
358 | * not physically logged in the transaction but | |
359 | * still tracked in the AIL as part of the | |
360 | * transaction and pin the log appropriately. | |
361 | */ | |
362 | xfs_trans_ordered_buf(tp, fbuf); | |
363 | xfs_trans_log_buf(tp, fbuf, 0, | |
364 | BBTOB(fbuf->b_length) - 1); | |
365 | } | |
366 | } else { | |
367 | fbuf->b_flags |= XBF_DONE; | |
368 | xfs_buf_delwri_queue(fbuf, buffer_list); | |
369 | xfs_buf_relse(fbuf); | |
56b2de80 | 370 | } |
56b2de80 | 371 | } |
a2ceac1f | 372 | return 0; |
56b2de80 DC |
373 | } |
374 | ||
6f4c54a4 BF |
375 | /* |
376 | * Align startino and allocmask for a recently allocated sparse chunk such that | |
377 | * they are fit for insertion (or merge) into the on-disk inode btrees. | |
378 | * | |
379 | * Background: | |
380 | * | |
381 | * When enabled, sparse inode support increases the inode alignment from cluster | |
382 | * size to inode chunk size. This means that the minimum range between two | |
383 | * non-adjacent inode records in the inobt is large enough for a full inode | |
384 | * record. This allows for cluster sized, cluster aligned block allocation | |
385 | * without need to worry about whether the resulting inode record overlaps with | |
386 | * another record in the tree. Without this basic rule, we would have to deal | |
387 | * with the consequences of overlap by potentially undoing recent allocations in | |
388 | * the inode allocation codepath. | |
389 | * | |
390 | * Because of this alignment rule (which is enforced on mount), there are two | |
391 | * inobt possibilities for newly allocated sparse chunks. One is that the | |
392 | * aligned inode record for the chunk covers a range of inodes not already | |
393 | * covered in the inobt (i.e., it is safe to insert a new sparse record). The | |
394 | * other is that a record already exists at the aligned startino that considers | |
395 | * the newly allocated range as sparse. In the latter case, record content is | |
396 | * merged in hope that sparse inode chunks fill to full chunks over time. | |
397 | */ | |
398 | STATIC void | |
399 | xfs_align_sparse_ino( | |
400 | struct xfs_mount *mp, | |
401 | xfs_agino_t *startino, | |
402 | uint16_t *allocmask) | |
403 | { | |
404 | xfs_agblock_t agbno; | |
405 | xfs_agblock_t mod; | |
406 | int offset; | |
407 | ||
408 | agbno = XFS_AGINO_TO_AGBNO(mp, *startino); | |
409 | mod = agbno % mp->m_sb.sb_inoalignmt; | |
410 | if (!mod) | |
411 | return; | |
412 | ||
413 | /* calculate the inode offset and align startino */ | |
414 | offset = mod << mp->m_sb.sb_inopblog; | |
415 | *startino -= offset; | |
416 | ||
417 | /* | |
418 | * Since startino has been aligned down, left shift allocmask such that | |
419 | * it continues to represent the same physical inodes relative to the | |
420 | * new startino. | |
421 | */ | |
422 | *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT; | |
423 | } | |
424 | ||
425 | /* | |
426 | * Determine whether the source inode record can merge into the target. Both | |
427 | * records must be sparse, the inode ranges must match and there must be no | |
428 | * allocation overlap between the records. | |
429 | */ | |
430 | STATIC bool | |
431 | __xfs_inobt_can_merge( | |
432 | struct xfs_inobt_rec_incore *trec, /* tgt record */ | |
433 | struct xfs_inobt_rec_incore *srec) /* src record */ | |
434 | { | |
435 | uint64_t talloc; | |
436 | uint64_t salloc; | |
437 | ||
438 | /* records must cover the same inode range */ | |
439 | if (trec->ir_startino != srec->ir_startino) | |
440 | return false; | |
441 | ||
442 | /* both records must be sparse */ | |
443 | if (!xfs_inobt_issparse(trec->ir_holemask) || | |
444 | !xfs_inobt_issparse(srec->ir_holemask)) | |
445 | return false; | |
446 | ||
447 | /* both records must track some inodes */ | |
448 | if (!trec->ir_count || !srec->ir_count) | |
449 | return false; | |
450 | ||
451 | /* can't exceed capacity of a full record */ | |
452 | if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK) | |
453 | return false; | |
454 | ||
455 | /* verify there is no allocation overlap */ | |
456 | talloc = xfs_inobt_irec_to_allocmask(trec); | |
457 | salloc = xfs_inobt_irec_to_allocmask(srec); | |
458 | if (talloc & salloc) | |
459 | return false; | |
460 | ||
461 | return true; | |
462 | } | |
463 | ||
464 | /* | |
465 | * Merge the source inode record into the target. The caller must call | |
466 | * __xfs_inobt_can_merge() to ensure the merge is valid. | |
467 | */ | |
468 | STATIC void | |
469 | __xfs_inobt_rec_merge( | |
470 | struct xfs_inobt_rec_incore *trec, /* target */ | |
471 | struct xfs_inobt_rec_incore *srec) /* src */ | |
472 | { | |
473 | ASSERT(trec->ir_startino == srec->ir_startino); | |
474 | ||
475 | /* combine the counts */ | |
476 | trec->ir_count += srec->ir_count; | |
477 | trec->ir_freecount += srec->ir_freecount; | |
478 | ||
479 | /* | |
480 | * Merge the holemask and free mask. For both fields, 0 bits refer to | |
481 | * allocated inodes. We combine the allocated ranges with bitwise AND. | |
482 | */ | |
483 | trec->ir_holemask &= srec->ir_holemask; | |
484 | trec->ir_free &= srec->ir_free; | |
485 | } | |
486 | ||
487 | /* | |
488 | * Insert a new sparse inode chunk into the associated inode btree. The inode | |
489 | * record for the sparse chunk is pre-aligned to a startino that should match | |
490 | * any pre-existing sparse inode record in the tree. This allows sparse chunks | |
491 | * to fill over time. | |
492 | * | |
493 | * This function supports two modes of handling preexisting records depending on | |
494 | * the merge flag. If merge is true, the provided record is merged with the | |
495 | * existing record and updated in place. The merged record is returned in nrec. | |
496 | * If merge is false, an existing record is replaced with the provided record. | |
497 | * If no preexisting record exists, the provided record is always inserted. | |
498 | * | |
499 | * It is considered corruption if a merge is requested and not possible. Given | |
500 | * the sparse inode alignment constraints, this should never happen. | |
501 | */ | |
502 | STATIC int | |
503 | xfs_inobt_insert_sprec( | |
504 | struct xfs_mount *mp, | |
505 | struct xfs_trans *tp, | |
506 | struct xfs_buf *agbp, | |
507 | int btnum, | |
508 | struct xfs_inobt_rec_incore *nrec, /* in/out: new/merged rec. */ | |
509 | bool merge) /* merge or replace */ | |
510 | { | |
511 | struct xfs_btree_cur *cur; | |
512 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); | |
513 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); | |
514 | int error; | |
515 | int i; | |
516 | struct xfs_inobt_rec_incore rec; | |
517 | ||
518 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum); | |
519 | ||
520 | /* the new record is pre-aligned so we know where to look */ | |
521 | error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i); | |
522 | if (error) | |
523 | goto error; | |
524 | /* if nothing there, insert a new record and return */ | |
525 | if (i == 0) { | |
526 | error = xfs_inobt_insert_rec(cur, nrec->ir_holemask, | |
527 | nrec->ir_count, nrec->ir_freecount, | |
528 | nrec->ir_free, &i); | |
529 | if (error) | |
530 | goto error; | |
531 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); | |
532 | ||
533 | goto out; | |
534 | } | |
535 | ||
536 | /* | |
537 | * A record exists at this startino. Merge or replace the record | |
538 | * depending on what we've been asked to do. | |
539 | */ | |
540 | if (merge) { | |
541 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
542 | if (error) | |
543 | goto error; | |
544 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); | |
545 | XFS_WANT_CORRUPTED_GOTO(mp, | |
546 | rec.ir_startino == nrec->ir_startino, | |
547 | error); | |
548 | ||
549 | /* | |
550 | * This should never fail. If we have coexisting records that | |
551 | * cannot merge, something is seriously wrong. | |
552 | */ | |
553 | XFS_WANT_CORRUPTED_GOTO(mp, __xfs_inobt_can_merge(nrec, &rec), | |
554 | error); | |
555 | ||
556 | trace_xfs_irec_merge_pre(mp, agno, rec.ir_startino, | |
557 | rec.ir_holemask, nrec->ir_startino, | |
558 | nrec->ir_holemask); | |
559 | ||
560 | /* merge to nrec to output the updated record */ | |
561 | __xfs_inobt_rec_merge(nrec, &rec); | |
562 | ||
563 | trace_xfs_irec_merge_post(mp, agno, nrec->ir_startino, | |
564 | nrec->ir_holemask); | |
565 | ||
566 | error = xfs_inobt_rec_check_count(mp, nrec); | |
567 | if (error) | |
568 | goto error; | |
569 | } | |
570 | ||
571 | error = xfs_inobt_update(cur, nrec); | |
572 | if (error) | |
573 | goto error; | |
574 | ||
575 | out: | |
576 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
577 | return 0; | |
578 | error: | |
579 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
580 | return error; | |
581 | } | |
582 | ||
2bd0ea18 NS |
583 | /* |
584 | * Allocate new inodes in the allocation group specified by agbp. | |
585 | * Return 0 for success, else error code. | |
586 | */ | |
587 | STATIC int /* error code or 0 */ | |
588 | xfs_ialloc_ag_alloc( | |
589 | xfs_trans_t *tp, /* transaction pointer */ | |
590 | xfs_buf_t *agbp, /* alloc group buffer */ | |
591 | int *alloc) | |
592 | { | |
593 | xfs_agi_t *agi; /* allocation group header */ | |
dfc130f3 | 594 | xfs_alloc_arg_t args; /* allocation argument structure */ |
5e656dbb | 595 | xfs_agnumber_t agno; |
2bd0ea18 | 596 | int error; |
2bd0ea18 NS |
597 | xfs_agino_t newino; /* new first inode's number */ |
598 | xfs_agino_t newlen; /* new number of inodes */ | |
5e656dbb | 599 | int isaligned = 0; /* inode allocation at stripe unit */ |
2bd0ea18 | 600 | /* boundary */ |
6f4c54a4 BF |
601 | uint16_t allocmask = (uint16_t) -1; /* init. to full chunk */ |
602 | struct xfs_inobt_rec_incore rec; | |
56b2de80 | 603 | struct xfs_perag *pag; |
5000d01d | 604 | |
c9005f41 BF |
605 | int do_sparse = 0; |
606 | ||
607 | #ifdef DEBUG | |
608 | /* randomly do sparse inode allocations */ | |
609 | if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb)) | |
610 | do_sparse = prandom_u32() & 1; | |
611 | #endif | |
612 | ||
a2ceac1f | 613 | memset(&args, 0, sizeof(args)); |
2bd0ea18 NS |
614 | args.tp = tp; |
615 | args.mp = tp->t_mountp; | |
c9005f41 | 616 | args.fsbno = NULLFSBLOCK; |
2bd0ea18 NS |
617 | |
618 | /* | |
619 | * Locking will ensure that we don't have two callers in here | |
620 | * at one time. | |
621 | */ | |
ff105f75 | 622 | newlen = args.mp->m_ialloc_inos; |
2bd0ea18 | 623 | if (args.mp->m_maxicount && |
19ebedcf DC |
624 | percpu_counter_read(&args.mp->m_icount) + newlen > |
625 | args.mp->m_maxicount) | |
12b53197 | 626 | return -ENOSPC; |
ff105f75 | 627 | args.minlen = args.maxlen = args.mp->m_ialloc_blks; |
2bd0ea18 | 628 | /* |
5e656dbb BN |
629 | * First try to allocate inodes contiguous with the last-allocated |
630 | * chunk of inodes. If the filesystem is striped, this will fill | |
631 | * an entire stripe unit with inodes. | |
3439d03a | 632 | */ |
2bd0ea18 | 633 | agi = XFS_BUF_TO_AGI(agbp); |
5e656dbb | 634 | newino = be32_to_cpu(agi->agi_newino); |
56b2de80 | 635 | agno = be32_to_cpu(agi->agi_seqno); |
5e656dbb | 636 | args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + |
ff105f75 | 637 | args.mp->m_ialloc_blks; |
c9005f41 BF |
638 | if (do_sparse) |
639 | goto sparse_alloc; | |
5e656dbb BN |
640 | if (likely(newino != NULLAGINO && |
641 | (args.agbno < be32_to_cpu(agi->agi_length)))) { | |
56b2de80 | 642 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
5e656dbb | 643 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
5e656dbb BN |
644 | args.prod = 1; |
645 | ||
646 | /* | |
647 | * We need to take into account alignment here to ensure that | |
648 | * we don't modify the free list if we fail to have an exact | |
649 | * block. If we don't have an exact match, and every oher | |
650 | * attempt allocation attempt fails, we'll end up cancelling | |
651 | * a dirty transaction and shutting down. | |
652 | * | |
653 | * For an exact allocation, alignment must be 1, | |
654 | * however we need to take cluster alignment into account when | |
655 | * fixing up the freelist. Use the minalignslop field to | |
656 | * indicate that extra blocks might be required for alignment, | |
657 | * but not to use them in the actual exact allocation. | |
658 | */ | |
659 | args.alignment = 1; | |
5a35bf2c | 660 | args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1; |
5e656dbb BN |
661 | |
662 | /* Allow space for the inode btree to split. */ | |
56b2de80 | 663 | args.minleft = args.mp->m_in_maxlevels - 1; |
5e656dbb BN |
664 | if ((error = xfs_alloc_vextent(&args))) |
665 | return error; | |
ff105f75 DC |
666 | |
667 | /* | |
668 | * This request might have dirtied the transaction if the AG can | |
669 | * satisfy the request, but the exact block was not available. | |
670 | * If the allocation did fail, subsequent requests will relax | |
671 | * the exact agbno requirement and increase the alignment | |
672 | * instead. It is critical that the total size of the request | |
673 | * (len + alignment + slop) does not increase from this point | |
674 | * on, so reset minalignslop to ensure it is not included in | |
675 | * subsequent requests. | |
676 | */ | |
677 | args.minalignslop = 0; | |
c9005f41 | 678 | } |
5e656dbb BN |
679 | |
680 | if (unlikely(args.fsbno == NULLFSBLOCK)) { | |
681 | /* | |
682 | * Set the alignment for the allocation. | |
683 | * If stripe alignment is turned on then align at stripe unit | |
684 | * boundary. | |
685 | * If the cluster size is smaller than a filesystem block | |
686 | * then we're doing I/O for inodes in filesystem block size | |
687 | * pieces, so don't need alignment anyway. | |
688 | */ | |
689 | isaligned = 0; | |
690 | if (args.mp->m_sinoalign) { | |
691 | ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN)); | |
692 | args.alignment = args.mp->m_dalign; | |
693 | isaligned = 1; | |
694 | } else | |
5a35bf2c | 695 | args.alignment = xfs_ialloc_cluster_alignment(args.mp); |
5e656dbb BN |
696 | /* |
697 | * Need to figure out where to allocate the inode blocks. | |
698 | * Ideally they should be spaced out through the a.g. | |
699 | * For now, just allocate blocks up front. | |
700 | */ | |
701 | args.agbno = be32_to_cpu(agi->agi_root); | |
56b2de80 | 702 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
5e656dbb BN |
703 | /* |
704 | * Allocate a fixed-size extent of inodes. | |
705 | */ | |
706 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
5e656dbb BN |
707 | args.prod = 1; |
708 | /* | |
709 | * Allow space for the inode btree to split. | |
710 | */ | |
56b2de80 | 711 | args.minleft = args.mp->m_in_maxlevels - 1; |
5e656dbb BN |
712 | if ((error = xfs_alloc_vextent(&args))) |
713 | return error; | |
714 | } | |
2bd0ea18 NS |
715 | |
716 | /* | |
717 | * If stripe alignment is turned on, then try again with cluster | |
718 | * alignment. | |
719 | */ | |
720 | if (isaligned && args.fsbno == NULLFSBLOCK) { | |
721 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
6e3140c7 | 722 | args.agbno = be32_to_cpu(agi->agi_root); |
56b2de80 | 723 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
5a35bf2c | 724 | args.alignment = xfs_ialloc_cluster_alignment(args.mp); |
0e266570 | 725 | if ((error = xfs_alloc_vextent(&args))) |
dfc130f3 | 726 | return error; |
2bd0ea18 | 727 | } |
5000d01d | 728 | |
6f4c54a4 BF |
729 | /* |
730 | * Finally, try a sparse allocation if the filesystem supports it and | |
731 | * the sparse allocation length is smaller than a full chunk. | |
732 | */ | |
733 | if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) && | |
734 | args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks && | |
735 | args.fsbno == NULLFSBLOCK) { | |
c9005f41 | 736 | sparse_alloc: |
6f4c54a4 BF |
737 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
738 | args.agbno = be32_to_cpu(agi->agi_root); | |
739 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); | |
740 | args.alignment = args.mp->m_sb.sb_spino_align; | |
741 | args.prod = 1; | |
742 | ||
743 | args.minlen = args.mp->m_ialloc_min_blks; | |
744 | args.maxlen = args.minlen; | |
745 | ||
746 | /* | |
747 | * The inode record will be aligned to full chunk size. We must | |
748 | * prevent sparse allocation from AG boundaries that result in | |
749 | * invalid inode records, such as records that start at agbno 0 | |
750 | * or extend beyond the AG. | |
751 | * | |
752 | * Set min agbno to the first aligned, non-zero agbno and max to | |
753 | * the last aligned agbno that is at least one full chunk from | |
754 | * the end of the AG. | |
755 | */ | |
756 | args.min_agbno = args.mp->m_sb.sb_inoalignmt; | |
757 | args.max_agbno = round_down(args.mp->m_sb.sb_agblocks, | |
758 | args.mp->m_sb.sb_inoalignmt) - | |
759 | args.mp->m_ialloc_blks; | |
760 | ||
761 | error = xfs_alloc_vextent(&args); | |
762 | if (error) | |
763 | return error; | |
764 | ||
765 | newlen = args.len << args.mp->m_sb.sb_inopblog; | |
766 | allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1; | |
767 | } | |
768 | ||
2bd0ea18 NS |
769 | if (args.fsbno == NULLFSBLOCK) { |
770 | *alloc = 0; | |
771 | return 0; | |
772 | } | |
773 | ASSERT(args.len == args.minlen); | |
a562a63b | 774 | |
5e656dbb | 775 | /* |
56b2de80 DC |
776 | * Stamp and write the inode buffers. |
777 | * | |
5e656dbb BN |
778 | * Seed the new inode cluster with a random generation number. This |
779 | * prevents short-term reuse of generation numbers if a chunk is | |
780 | * freed and then immediately reallocated. We use random numbers | |
781 | * rather than a linear progression to prevent the next generation | |
782 | * number from being easily guessable. | |
783 | */ | |
fe8d48ac BF |
784 | error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno, |
785 | args.agbno, args.len, prandom_u32()); | |
56b2de80 | 786 | |
a2ceac1f DC |
787 | if (error) |
788 | return error; | |
56b2de80 DC |
789 | /* |
790 | * Convert the results. | |
791 | */ | |
792 | newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0); | |
6f4c54a4 BF |
793 | |
794 | if (xfs_inobt_issparse(~allocmask)) { | |
795 | /* | |
796 | * We've allocated a sparse chunk. Align the startino and mask. | |
797 | */ | |
798 | xfs_align_sparse_ino(args.mp, &newino, &allocmask); | |
799 | ||
800 | rec.ir_startino = newino; | |
801 | rec.ir_holemask = ~allocmask; | |
802 | rec.ir_count = newlen; | |
803 | rec.ir_freecount = newlen; | |
804 | rec.ir_free = XFS_INOBT_ALL_FREE; | |
805 | ||
806 | /* | |
807 | * Insert the sparse record into the inobt and allow for a merge | |
808 | * if necessary. If a merge does occur, rec is updated to the | |
809 | * merged record. | |
810 | */ | |
811 | error = xfs_inobt_insert_sprec(args.mp, tp, agbp, XFS_BTNUM_INO, | |
812 | &rec, true); | |
813 | if (error == -EFSCORRUPTED) { | |
814 | xfs_alert(args.mp, | |
815 | "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u", | |
816 | XFS_AGINO_TO_INO(args.mp, agno, | |
817 | rec.ir_startino), | |
818 | rec.ir_holemask, rec.ir_count); | |
819 | xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE); | |
820 | } | |
821 | if (error) | |
822 | return error; | |
823 | ||
824 | /* | |
825 | * We can't merge the part we've just allocated as for the inobt | |
826 | * due to finobt semantics. The original record may or may not | |
827 | * exist independent of whether physical inodes exist in this | |
828 | * sparse chunk. | |
829 | * | |
830 | * We must update the finobt record based on the inobt record. | |
831 | * rec contains the fully merged and up to date inobt record | |
832 | * from the previous call. Set merge false to replace any | |
833 | * existing record with this one. | |
834 | */ | |
835 | if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) { | |
836 | error = xfs_inobt_insert_sprec(args.mp, tp, agbp, | |
837 | XFS_BTNUM_FINO, &rec, | |
838 | false); | |
839 | if (error) | |
840 | return error; | |
841 | } | |
842 | } else { | |
843 | /* full chunk - insert new records to both btrees */ | |
844 | error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen, | |
845 | XFS_BTNUM_INO); | |
846 | if (error) | |
847 | return error; | |
848 | ||
849 | if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) { | |
850 | error = xfs_inobt_insert(args.mp, tp, agbp, newino, | |
851 | newlen, XFS_BTNUM_FINO); | |
852 | if (error) | |
853 | return error; | |
854 | } | |
855 | } | |
856 | ||
857 | /* | |
858 | * Update AGI counts and newino. | |
859 | */ | |
5e656dbb BN |
860 | be32_add_cpu(&agi->agi_count, newlen); |
861 | be32_add_cpu(&agi->agi_freecount, newlen); | |
56b2de80 DC |
862 | pag = xfs_perag_get(args.mp, agno); |
863 | pag->pagi_freecount += newlen; | |
864 | xfs_perag_put(pag); | |
6e3140c7 | 865 | agi->agi_newino = cpu_to_be32(newino); |
56b2de80 | 866 | |
2bd0ea18 NS |
867 | /* |
868 | * Log allocation group header fields | |
869 | */ | |
870 | xfs_ialloc_log_agi(tp, agbp, | |
871 | XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); | |
872 | /* | |
873 | * Modify/log superblock values for inode count and inode free count. | |
874 | */ | |
875 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); | |
876 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); | |
877 | *alloc = 1; | |
878 | return 0; | |
879 | } | |
880 | ||
56b2de80 | 881 | STATIC xfs_agnumber_t |
321717ae NS |
882 | xfs_ialloc_next_ag( |
883 | xfs_mount_t *mp) | |
884 | { | |
885 | xfs_agnumber_t agno; | |
886 | ||
887 | spin_lock(&mp->m_agirotor_lock); | |
888 | agno = mp->m_agirotor; | |
a2ceac1f | 889 | if (++mp->m_agirotor >= mp->m_maxagi) |
321717ae NS |
890 | mp->m_agirotor = 0; |
891 | spin_unlock(&mp->m_agirotor_lock); | |
892 | ||
893 | return agno; | |
894 | } | |
895 | ||
2bd0ea18 NS |
896 | /* |
897 | * Select an allocation group to look for a free inode in, based on the parent | |
e6d77a21 | 898 | * inode and the mode. Return the allocation group buffer. |
2bd0ea18 | 899 | */ |
a2ceac1f | 900 | STATIC xfs_agnumber_t |
2bd0ea18 NS |
901 | xfs_ialloc_ag_select( |
902 | xfs_trans_t *tp, /* transaction pointer */ | |
903 | xfs_ino_t parent, /* parent directory inode number */ | |
a2ceac1f | 904 | umode_t mode, /* bits set to indicate file type */ |
2bd0ea18 NS |
905 | int okalloc) /* ok to allocate more space */ |
906 | { | |
2bd0ea18 NS |
907 | xfs_agnumber_t agcount; /* number of ag's in the filesystem */ |
908 | xfs_agnumber_t agno; /* current ag number */ | |
909 | int flags; /* alloc buffer locking flags */ | |
910 | xfs_extlen_t ineed; /* blocks needed for inode allocation */ | |
275ae71f | 911 | xfs_extlen_t longest = 0; /* longest extent available */ |
2bd0ea18 NS |
912 | xfs_mount_t *mp; /* mount point structure */ |
913 | int needspace; /* file mode implies space allocated */ | |
914 | xfs_perag_t *pag; /* per allocation group data */ | |
915 | xfs_agnumber_t pagno; /* parent (starting) ag number */ | |
a2ceac1f | 916 | int error; |
2bd0ea18 NS |
917 | |
918 | /* | |
919 | * Files of these types need at least one block if length > 0 | |
920 | * (and they won't fit in the inode, but that's hard to figure out). | |
921 | */ | |
922 | needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); | |
923 | mp = tp->t_mountp; | |
34317449 | 924 | agcount = mp->m_maxagi; |
2bd0ea18 | 925 | if (S_ISDIR(mode)) |
321717ae | 926 | pagno = xfs_ialloc_next_ag(mp); |
5ce1d1f7 | 927 | else { |
2bd0ea18 | 928 | pagno = XFS_INO_TO_AGNO(mp, parent); |
5ce1d1f7 NS |
929 | if (pagno >= agcount) |
930 | pagno = 0; | |
931 | } | |
a2ceac1f | 932 | |
2bd0ea18 | 933 | ASSERT(pagno < agcount); |
a2ceac1f | 934 | |
2bd0ea18 NS |
935 | /* |
936 | * Loop through allocation groups, looking for one with a little | |
937 | * free space in it. Note we don't look for free inodes, exactly. | |
938 | * Instead, we include whether there is a need to allocate inodes | |
5000d01d | 939 | * to mean that blocks must be allocated for them, |
2bd0ea18 NS |
940 | * if none are currently free. |
941 | */ | |
942 | agno = pagno; | |
943 | flags = XFS_ALLOC_FLAG_TRYLOCK; | |
944 | for (;;) { | |
56b2de80 | 945 | pag = xfs_perag_get(mp, agno); |
a2ceac1f DC |
946 | if (!pag->pagi_inodeok) { |
947 | xfs_ialloc_next_ag(mp); | |
948 | goto nextag; | |
949 | } | |
950 | ||
2bd0ea18 | 951 | if (!pag->pagi_init) { |
a2ceac1f DC |
952 | error = xfs_ialloc_pagi_init(mp, tp, agno); |
953 | if (error) | |
2bd0ea18 | 954 | goto nextag; |
a2ceac1f | 955 | } |
34317449 | 956 | |
a2ceac1f DC |
957 | if (pag->pagi_freecount) { |
958 | xfs_perag_put(pag); | |
959 | return agno; | |
34317449 NS |
960 | } |
961 | ||
a2ceac1f DC |
962 | if (!okalloc) |
963 | goto nextag; | |
964 | ||
965 | if (!pag->pagf_init) { | |
966 | error = xfs_alloc_pagf_init(mp, tp, agno, flags); | |
967 | if (error) | |
2bd0ea18 | 968 | goto nextag; |
2bd0ea18 | 969 | } |
a2ceac1f DC |
970 | |
971 | /* | |
5a35bf2c DC |
972 | * Check that there is enough free space for the file plus a |
973 | * chunk of inodes if we need to allocate some. If this is the | |
974 | * first pass across the AGs, take into account the potential | |
975 | * space needed for alignment of inode chunks when checking the | |
976 | * longest contiguous free space in the AG - this prevents us | |
977 | * from getting ENOSPC because we have free space larger than | |
978 | * m_ialloc_blks but alignment constraints prevent us from using | |
979 | * it. | |
980 | * | |
981 | * If we can't find an AG with space for full alignment slack to | |
982 | * be taken into account, we must be near ENOSPC in all AGs. | |
983 | * Hence we don't include alignment for the second pass and so | |
984 | * if we fail allocation due to alignment issues then it is most | |
985 | * likely a real ENOSPC condition. | |
a2ceac1f | 986 | */ |
62dc6cdb | 987 | ineed = mp->m_ialloc_min_blks; |
5a35bf2c DC |
988 | if (flags && ineed > 1) |
989 | ineed += xfs_ialloc_cluster_alignment(mp); | |
a2ceac1f DC |
990 | longest = pag->pagf_longest; |
991 | if (!longest) | |
992 | longest = pag->pagf_flcount > 0; | |
993 | ||
994 | if (pag->pagf_freeblks >= needspace + ineed && | |
995 | longest >= ineed) { | |
996 | xfs_perag_put(pag); | |
997 | return agno; | |
2bd0ea18 | 998 | } |
5000d01d | 999 | nextag: |
56b2de80 | 1000 | xfs_perag_put(pag); |
5000d01d | 1001 | /* |
2bd0ea18 NS |
1002 | * No point in iterating over the rest, if we're shutting |
1003 | * down. | |
1004 | */ | |
56b2de80 | 1005 | if (XFS_FORCED_SHUTDOWN(mp)) |
a2ceac1f | 1006 | return NULLAGNUMBER; |
2bd0ea18 | 1007 | agno++; |
5ce1d1f7 | 1008 | if (agno >= agcount) |
2bd0ea18 NS |
1009 | agno = 0; |
1010 | if (agno == pagno) { | |
56b2de80 | 1011 | if (flags == 0) |
a2ceac1f | 1012 | return NULLAGNUMBER; |
2bd0ea18 NS |
1013 | flags = 0; |
1014 | } | |
1015 | } | |
1016 | } | |
1017 | ||
56b2de80 DC |
1018 | /* |
1019 | * Try to retrieve the next record to the left/right from the current one. | |
1020 | */ | |
1021 | STATIC int | |
1022 | xfs_ialloc_next_rec( | |
1023 | struct xfs_btree_cur *cur, | |
1024 | xfs_inobt_rec_incore_t *rec, | |
1025 | int *done, | |
1026 | int left) | |
1027 | { | |
1028 | int error; | |
1029 | int i; | |
1030 | ||
1031 | if (left) | |
1032 | error = xfs_btree_decrement(cur, 0, &i); | |
1033 | else | |
1034 | error = xfs_btree_increment(cur, 0, &i); | |
1035 | ||
1036 | if (error) | |
1037 | return error; | |
1038 | *done = !i; | |
1039 | if (i) { | |
1040 | error = xfs_inobt_get_rec(cur, rec, &i); | |
1041 | if (error) | |
1042 | return error; | |
19ebedcf | 1043 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
56b2de80 DC |
1044 | } |
1045 | ||
1046 | return 0; | |
1047 | } | |
1048 | ||
1049 | STATIC int | |
1050 | xfs_ialloc_get_rec( | |
1051 | struct xfs_btree_cur *cur, | |
1052 | xfs_agino_t agino, | |
1053 | xfs_inobt_rec_incore_t *rec, | |
3439d03a | 1054 | int *done) |
56b2de80 DC |
1055 | { |
1056 | int error; | |
1057 | int i; | |
1058 | ||
1059 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i); | |
1060 | if (error) | |
1061 | return error; | |
1062 | *done = !i; | |
1063 | if (i) { | |
1064 | error = xfs_inobt_get_rec(cur, rec, &i); | |
1065 | if (error) | |
1066 | return error; | |
19ebedcf | 1067 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
56b2de80 DC |
1068 | } |
1069 | ||
1070 | return 0; | |
1071 | } | |
1072 | ||
01792d3b | 1073 | /* |
fa58183c BF |
1074 | * Return the offset of the first free inode in the record. If the inode chunk |
1075 | * is sparsely allocated, we convert the record holemask to inode granularity | |
1076 | * and mask off the unallocated regions from the inode free mask. | |
01792d3b BF |
1077 | */ |
1078 | STATIC int | |
1079 | xfs_inobt_first_free_inode( | |
1080 | struct xfs_inobt_rec_incore *rec) | |
1081 | { | |
fa58183c BF |
1082 | xfs_inofree_t realfree; |
1083 | ||
1084 | /* if there are no holes, return the first available offset */ | |
1085 | if (!xfs_inobt_issparse(rec->ir_holemask)) | |
1086 | return xfs_lowbit64(rec->ir_free); | |
1087 | ||
1088 | realfree = xfs_inobt_irec_to_allocmask(rec); | |
1089 | realfree &= rec->ir_free; | |
1090 | ||
1091 | return xfs_lowbit64(realfree); | |
01792d3b BF |
1092 | } |
1093 | ||
5000d01d | 1094 | /* |
ff105f75 | 1095 | * Allocate an inode using the inobt-only algorithm. |
2bd0ea18 | 1096 | */ |
a2ceac1f | 1097 | STATIC int |
ff105f75 | 1098 | xfs_dialloc_ag_inobt( |
a2ceac1f DC |
1099 | struct xfs_trans *tp, |
1100 | struct xfs_buf *agbp, | |
1101 | xfs_ino_t parent, | |
1102 | xfs_ino_t *inop) | |
2bd0ea18 | 1103 | { |
a2ceac1f DC |
1104 | struct xfs_mount *mp = tp->t_mountp; |
1105 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); | |
1106 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); | |
1107 | xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); | |
1108 | xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); | |
1109 | struct xfs_perag *pag; | |
1110 | struct xfs_btree_cur *cur, *tcur; | |
1111 | struct xfs_inobt_rec_incore rec, trec; | |
1112 | xfs_ino_t ino; | |
1113 | int error; | |
1114 | int offset; | |
1115 | int i, j; | |
2bd0ea18 | 1116 | |
56b2de80 DC |
1117 | pag = xfs_perag_get(mp, agno); |
1118 | ||
a2ceac1f DC |
1119 | ASSERT(pag->pagi_init); |
1120 | ASSERT(pag->pagi_inodeok); | |
1121 | ASSERT(pag->pagi_freecount > 0); | |
1122 | ||
56b2de80 | 1123 | restart_pagno: |
70eb7337 | 1124 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
2bd0ea18 NS |
1125 | /* |
1126 | * If pagino is 0 (this is the root inode allocation) use newino. | |
1127 | * This must work because we've just allocated some. | |
1128 | */ | |
1129 | if (!pagino) | |
6e3140c7 | 1130 | pagino = be32_to_cpu(agi->agi_newino); |
2bd0ea18 | 1131 | |
56b2de80 DC |
1132 | error = xfs_check_agi_freecount(cur, agi); |
1133 | if (error) | |
1134 | goto error0; | |
2bd0ea18 | 1135 | |
2bd0ea18 | 1136 | /* |
56b2de80 | 1137 | * If in the same AG as the parent, try to get near the parent. |
2bd0ea18 NS |
1138 | */ |
1139 | if (pagno == agno) { | |
56b2de80 DC |
1140 | int doneleft; /* done, to the left */ |
1141 | int doneright; /* done, to the right */ | |
1142 | int searchdistance = 10; | |
1143 | ||
1144 | error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i); | |
1145 | if (error) | |
2bd0ea18 | 1146 | goto error0; |
19ebedcf | 1147 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
56b2de80 DC |
1148 | |
1149 | error = xfs_inobt_get_rec(cur, &rec, &j); | |
1150 | if (error) | |
1151 | goto error0; | |
19ebedcf | 1152 | XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0); |
56b2de80 DC |
1153 | |
1154 | if (rec.ir_freecount > 0) { | |
2bd0ea18 NS |
1155 | /* |
1156 | * Found a free inode in the same chunk | |
56b2de80 | 1157 | * as the parent, done. |
2bd0ea18 | 1158 | */ |
56b2de80 | 1159 | goto alloc_inode; |
2bd0ea18 | 1160 | } |
56b2de80 DC |
1161 | |
1162 | ||
1163 | /* | |
1164 | * In the same AG as parent, but parent's chunk is full. | |
1165 | */ | |
1166 | ||
1167 | /* duplicate the cursor, search left & right simultaneously */ | |
1168 | error = xfs_btree_dup_cursor(cur, &tcur); | |
1169 | if (error) | |
1170 | goto error0; | |
1171 | ||
2bd0ea18 | 1172 | /* |
56b2de80 | 1173 | * Skip to last blocks looked up if same parent inode. |
2bd0ea18 | 1174 | */ |
56b2de80 DC |
1175 | if (pagino != NULLAGINO && |
1176 | pag->pagl_pagino == pagino && | |
1177 | pag->pagl_leftrec != NULLAGINO && | |
1178 | pag->pagl_rightrec != NULLAGINO) { | |
1179 | error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec, | |
3439d03a | 1180 | &trec, &doneleft); |
56b2de80 DC |
1181 | if (error) |
1182 | goto error1; | |
2bd0ea18 | 1183 | |
56b2de80 | 1184 | error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec, |
3439d03a | 1185 | &rec, &doneright); |
2bd0ea18 | 1186 | if (error) |
2bd0ea18 | 1187 | goto error1; |
56b2de80 DC |
1188 | } else { |
1189 | /* search left with tcur, back up 1 record */ | |
1190 | error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1); | |
1191 | if (error) | |
2bd0ea18 | 1192 | goto error1; |
2bd0ea18 | 1193 | |
56b2de80 DC |
1194 | /* search right with cur, go forward 1 record. */ |
1195 | error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0); | |
1196 | if (error) | |
1197 | goto error1; | |
1198 | } | |
1199 | ||
1200 | /* | |
1201 | * Loop until we find an inode chunk with a free inode. | |
1202 | */ | |
1203 | while (!doneleft || !doneright) { | |
1204 | int useleft; /* using left inode chunk this time */ | |
1205 | ||
1206 | if (!--searchdistance) { | |
2bd0ea18 | 1207 | /* |
56b2de80 DC |
1208 | * Not in range - save last search |
1209 | * location and allocate a new inode | |
2bd0ea18 | 1210 | */ |
56b2de80 DC |
1211 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
1212 | pag->pagl_leftrec = trec.ir_startino; | |
1213 | pag->pagl_rightrec = rec.ir_startino; | |
1214 | pag->pagl_pagino = pagino; | |
1215 | goto newino; | |
2bd0ea18 | 1216 | } |
56b2de80 DC |
1217 | |
1218 | /* figure out the closer block if both are valid. */ | |
1219 | if (!doneleft && !doneright) { | |
1220 | useleft = pagino - | |
1221 | (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) < | |
1222 | rec.ir_startino - pagino; | |
1223 | } else { | |
1224 | useleft = !doneleft; | |
1225 | } | |
1226 | ||
1227 | /* free inodes to the left? */ | |
1228 | if (useleft && trec.ir_freecount) { | |
1229 | rec = trec; | |
1230 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
1231 | cur = tcur; | |
1232 | ||
1233 | pag->pagl_leftrec = trec.ir_startino; | |
1234 | pag->pagl_rightrec = rec.ir_startino; | |
1235 | pag->pagl_pagino = pagino; | |
1236 | goto alloc_inode; | |
1237 | } | |
1238 | ||
1239 | /* free inodes to the right? */ | |
1240 | if (!useleft && rec.ir_freecount) { | |
1241 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); | |
1242 | ||
1243 | pag->pagl_leftrec = trec.ir_startino; | |
1244 | pag->pagl_rightrec = rec.ir_startino; | |
1245 | pag->pagl_pagino = pagino; | |
1246 | goto alloc_inode; | |
1247 | } | |
1248 | ||
1249 | /* get next record to check */ | |
1250 | if (useleft) { | |
1251 | error = xfs_ialloc_next_rec(tcur, &trec, | |
1252 | &doneleft, 1); | |
1253 | } else { | |
1254 | error = xfs_ialloc_next_rec(cur, &rec, | |
1255 | &doneright, 0); | |
1256 | } | |
1257 | if (error) | |
1258 | goto error1; | |
2bd0ea18 | 1259 | } |
56b2de80 DC |
1260 | |
1261 | /* | |
1262 | * We've reached the end of the btree. because | |
1263 | * we are only searching a small chunk of the | |
1264 | * btree each search, there is obviously free | |
1265 | * inodes closer to the parent inode than we | |
1266 | * are now. restart the search again. | |
1267 | */ | |
1268 | pag->pagl_pagino = NULLAGINO; | |
1269 | pag->pagl_leftrec = NULLAGINO; | |
1270 | pag->pagl_rightrec = NULLAGINO; | |
1271 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); | |
1272 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
1273 | goto restart_pagno; | |
2bd0ea18 | 1274 | } |
56b2de80 | 1275 | |
2bd0ea18 | 1276 | /* |
56b2de80 | 1277 | * In a different AG from the parent. |
2bd0ea18 NS |
1278 | * See if the most recently allocated block has any free. |
1279 | */ | |
56b2de80 | 1280 | newino: |
a2ceac1f | 1281 | if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { |
56b2de80 DC |
1282 | error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), |
1283 | XFS_LOOKUP_EQ, &i); | |
1284 | if (error) | |
2bd0ea18 | 1285 | goto error0; |
56b2de80 DC |
1286 | |
1287 | if (i == 1) { | |
1288 | error = xfs_inobt_get_rec(cur, &rec, &j); | |
2bd0ea18 NS |
1289 | if (error) |
1290 | goto error0; | |
56b2de80 DC |
1291 | |
1292 | if (j == 1 && rec.ir_freecount > 0) { | |
1293 | /* | |
1294 | * The last chunk allocated in the group | |
1295 | * still has a free inode. | |
1296 | */ | |
1297 | goto alloc_inode; | |
2bd0ea18 NS |
1298 | } |
1299 | } | |
1300 | } | |
56b2de80 DC |
1301 | |
1302 | /* | |
1303 | * None left in the last group, search the whole AG | |
1304 | */ | |
1305 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); | |
1306 | if (error) | |
1307 | goto error0; | |
19ebedcf | 1308 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
56b2de80 DC |
1309 | |
1310 | for (;;) { | |
1311 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
1312 | if (error) | |
1313 | goto error0; | |
19ebedcf | 1314 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
56b2de80 DC |
1315 | if (rec.ir_freecount > 0) |
1316 | break; | |
1317 | error = xfs_btree_increment(cur, 0, &i); | |
1318 | if (error) | |
1319 | goto error0; | |
19ebedcf | 1320 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
56b2de80 DC |
1321 | } |
1322 | ||
1323 | alloc_inode: | |
01792d3b | 1324 | offset = xfs_inobt_first_free_inode(&rec); |
2bd0ea18 NS |
1325 | ASSERT(offset >= 0); |
1326 | ASSERT(offset < XFS_INODES_PER_CHUNK); | |
1327 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % | |
1328 | XFS_INODES_PER_CHUNK) == 0); | |
1329 | ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); | |
56b2de80 | 1330 | rec.ir_free &= ~XFS_INOBT_MASK(offset); |
2bd0ea18 | 1331 | rec.ir_freecount--; |
56b2de80 DC |
1332 | error = xfs_inobt_update(cur, &rec); |
1333 | if (error) | |
2bd0ea18 | 1334 | goto error0; |
5e656dbb | 1335 | be32_add_cpu(&agi->agi_freecount, -1); |
2bd0ea18 | 1336 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
56b2de80 DC |
1337 | pag->pagi_freecount--; |
1338 | ||
1339 | error = xfs_check_agi_freecount(cur, agi); | |
1340 | if (error) | |
1341 | goto error0; | |
2bd0ea18 | 1342 | |
2bd0ea18 NS |
1343 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
1344 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); | |
56b2de80 | 1345 | xfs_perag_put(pag); |
2bd0ea18 NS |
1346 | *inop = ino; |
1347 | return 0; | |
1348 | error1: | |
1349 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); | |
1350 | error0: | |
1351 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
56b2de80 | 1352 | xfs_perag_put(pag); |
2bd0ea18 NS |
1353 | return error; |
1354 | } | |
1355 | ||
ff105f75 DC |
1356 | /* |
1357 | * Use the free inode btree to allocate an inode based on distance from the | |
1358 | * parent. Note that the provided cursor may be deleted and replaced. | |
1359 | */ | |
1360 | STATIC int | |
1361 | xfs_dialloc_ag_finobt_near( | |
1362 | xfs_agino_t pagino, | |
1363 | struct xfs_btree_cur **ocur, | |
1364 | struct xfs_inobt_rec_incore *rec) | |
1365 | { | |
1366 | struct xfs_btree_cur *lcur = *ocur; /* left search cursor */ | |
1367 | struct xfs_btree_cur *rcur; /* right search cursor */ | |
1368 | struct xfs_inobt_rec_incore rrec; | |
1369 | int error; | |
1370 | int i, j; | |
1371 | ||
1372 | error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i); | |
1373 | if (error) | |
1374 | return error; | |
1375 | ||
1376 | if (i == 1) { | |
1377 | error = xfs_inobt_get_rec(lcur, rec, &i); | |
1378 | if (error) | |
1379 | return error; | |
19ebedcf | 1380 | XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1); |
ff105f75 DC |
1381 | |
1382 | /* | |
1383 | * See if we've landed in the parent inode record. The finobt | |
1384 | * only tracks chunks with at least one free inode, so record | |
1385 | * existence is enough. | |
1386 | */ | |
1387 | if (pagino >= rec->ir_startino && | |
1388 | pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK)) | |
1389 | return 0; | |
1390 | } | |
1391 | ||
1392 | error = xfs_btree_dup_cursor(lcur, &rcur); | |
1393 | if (error) | |
1394 | return error; | |
1395 | ||
1396 | error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j); | |
1397 | if (error) | |
1398 | goto error_rcur; | |
1399 | if (j == 1) { | |
1400 | error = xfs_inobt_get_rec(rcur, &rrec, &j); | |
1401 | if (error) | |
1402 | goto error_rcur; | |
19ebedcf | 1403 | XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur); |
ff105f75 DC |
1404 | } |
1405 | ||
19ebedcf | 1406 | XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur); |
ff105f75 DC |
1407 | if (i == 1 && j == 1) { |
1408 | /* | |
1409 | * Both the left and right records are valid. Choose the closer | |
1410 | * inode chunk to the target. | |
1411 | */ | |
1412 | if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) > | |
1413 | (rrec.ir_startino - pagino)) { | |
1414 | *rec = rrec; | |
1415 | xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); | |
1416 | *ocur = rcur; | |
1417 | } else { | |
1418 | xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); | |
1419 | } | |
1420 | } else if (j == 1) { | |
1421 | /* only the right record is valid */ | |
1422 | *rec = rrec; | |
1423 | xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); | |
1424 | *ocur = rcur; | |
1425 | } else if (i == 1) { | |
1426 | /* only the left record is valid */ | |
1427 | xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); | |
1428 | } | |
1429 | ||
1430 | return 0; | |
1431 | ||
1432 | error_rcur: | |
1433 | xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR); | |
1434 | return error; | |
1435 | } | |
1436 | ||
1437 | /* | |
1438 | * Use the free inode btree to find a free inode based on a newino hint. If | |
1439 | * the hint is NULL, find the first free inode in the AG. | |
1440 | */ | |
1441 | STATIC int | |
1442 | xfs_dialloc_ag_finobt_newino( | |
1443 | struct xfs_agi *agi, | |
1444 | struct xfs_btree_cur *cur, | |
1445 | struct xfs_inobt_rec_incore *rec) | |
1446 | { | |
1447 | int error; | |
1448 | int i; | |
1449 | ||
1450 | if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { | |
1451 | error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), | |
1452 | XFS_LOOKUP_EQ, &i); | |
1453 | if (error) | |
1454 | return error; | |
1455 | if (i == 1) { | |
1456 | error = xfs_inobt_get_rec(cur, rec, &i); | |
1457 | if (error) | |
1458 | return error; | |
19ebedcf | 1459 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
ff105f75 DC |
1460 | return 0; |
1461 | } | |
1462 | } | |
1463 | ||
1464 | /* | |
1465 | * Find the first inode available in the AG. | |
1466 | */ | |
1467 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); | |
1468 | if (error) | |
1469 | return error; | |
19ebedcf | 1470 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
ff105f75 DC |
1471 | |
1472 | error = xfs_inobt_get_rec(cur, rec, &i); | |
1473 | if (error) | |
1474 | return error; | |
19ebedcf | 1475 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
ff105f75 DC |
1476 | |
1477 | return 0; | |
1478 | } | |
1479 | ||
1480 | /* | |
1481 | * Update the inobt based on a modification made to the finobt. Also ensure that | |
1482 | * the records from both trees are equivalent post-modification. | |
1483 | */ | |
1484 | STATIC int | |
1485 | xfs_dialloc_ag_update_inobt( | |
1486 | struct xfs_btree_cur *cur, /* inobt cursor */ | |
1487 | struct xfs_inobt_rec_incore *frec, /* finobt record */ | |
1488 | int offset) /* inode offset */ | |
1489 | { | |
1490 | struct xfs_inobt_rec_incore rec; | |
1491 | int error; | |
1492 | int i; | |
1493 | ||
1494 | error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i); | |
1495 | if (error) | |
1496 | return error; | |
19ebedcf | 1497 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
ff105f75 DC |
1498 | |
1499 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
1500 | if (error) | |
1501 | return error; | |
19ebedcf | 1502 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
ff105f75 DC |
1503 | ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) % |
1504 | XFS_INODES_PER_CHUNK) == 0); | |
1505 | ||
1506 | rec.ir_free &= ~XFS_INOBT_MASK(offset); | |
1507 | rec.ir_freecount--; | |
1508 | ||
19ebedcf | 1509 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) && |
ff105f75 DC |
1510 | (rec.ir_freecount == frec->ir_freecount)); |
1511 | ||
5a35bf2c | 1512 | return xfs_inobt_update(cur, &rec); |
ff105f75 DC |
1513 | } |
1514 | ||
1515 | /* | |
1516 | * Allocate an inode using the free inode btree, if available. Otherwise, fall | |
1517 | * back to the inobt search algorithm. | |
1518 | * | |
1519 | * The caller selected an AG for us, and made sure that free inodes are | |
1520 | * available. | |
1521 | */ | |
88fc7306 BF |
1522 | STATIC int |
1523 | xfs_dialloc_ag( | |
1524 | struct xfs_trans *tp, | |
1525 | struct xfs_buf *agbp, | |
1526 | xfs_ino_t parent, | |
1527 | xfs_ino_t *inop) | |
1528 | { | |
1529 | struct xfs_mount *mp = tp->t_mountp; | |
1530 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); | |
1531 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); | |
1532 | xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); | |
1533 | xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); | |
1534 | struct xfs_perag *pag; | |
ff105f75 DC |
1535 | struct xfs_btree_cur *cur; /* finobt cursor */ |
1536 | struct xfs_btree_cur *icur; /* inobt cursor */ | |
88fc7306 | 1537 | struct xfs_inobt_rec_incore rec; |
88fc7306 BF |
1538 | xfs_ino_t ino; |
1539 | int error; | |
1540 | int offset; | |
ff105f75 | 1541 | int i; |
88fc7306 BF |
1542 | |
1543 | if (!xfs_sb_version_hasfinobt(&mp->m_sb)) | |
ff105f75 | 1544 | return xfs_dialloc_ag_inobt(tp, agbp, parent, inop); |
88fc7306 BF |
1545 | |
1546 | pag = xfs_perag_get(mp, agno); | |
1547 | ||
1548 | /* | |
1549 | * If pagino is 0 (this is the root inode allocation) use newino. | |
1550 | * This must work because we've just allocated some. | |
1551 | */ | |
1552 | if (!pagino) | |
1553 | pagino = be32_to_cpu(agi->agi_newino); | |
1554 | ||
1555 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO); | |
1556 | ||
1557 | error = xfs_check_agi_freecount(cur, agi); | |
1558 | if (error) | |
1559 | goto error_cur; | |
1560 | ||
ff105f75 DC |
1561 | /* |
1562 | * The search algorithm depends on whether we're in the same AG as the | |
1563 | * parent. If so, find the closest available inode to the parent. If | |
1564 | * not, consider the agi hint or find the first free inode in the AG. | |
1565 | */ | |
1566 | if (agno == pagno) | |
1567 | error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec); | |
1568 | else | |
1569 | error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec); | |
1570 | if (error) | |
1571 | goto error_cur; | |
88fc7306 | 1572 | |
01792d3b | 1573 | offset = xfs_inobt_first_free_inode(&rec); |
88fc7306 BF |
1574 | ASSERT(offset >= 0); |
1575 | ASSERT(offset < XFS_INODES_PER_CHUNK); | |
1576 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % | |
1577 | XFS_INODES_PER_CHUNK) == 0); | |
1578 | ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); | |
1579 | ||
1580 | /* | |
1581 | * Modify or remove the finobt record. | |
1582 | */ | |
1583 | rec.ir_free &= ~XFS_INOBT_MASK(offset); | |
1584 | rec.ir_freecount--; | |
ff105f75 | 1585 | if (rec.ir_freecount) |
88fc7306 BF |
1586 | error = xfs_inobt_update(cur, &rec); |
1587 | else | |
1588 | error = xfs_btree_delete(cur, &i); | |
1589 | if (error) | |
1590 | goto error_cur; | |
1591 | ||
1592 | /* | |
ff105f75 DC |
1593 | * The finobt has now been updated appropriately. We haven't updated the |
1594 | * agi and superblock yet, so we can create an inobt cursor and validate | |
1595 | * the original freecount. If all is well, make the equivalent update to | |
1596 | * the inobt using the finobt record and offset information. | |
88fc7306 | 1597 | */ |
ff105f75 | 1598 | icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
88fc7306 | 1599 | |
ff105f75 | 1600 | error = xfs_check_agi_freecount(icur, agi); |
88fc7306 | 1601 | if (error) |
ff105f75 | 1602 | goto error_icur; |
88fc7306 | 1603 | |
ff105f75 | 1604 | error = xfs_dialloc_ag_update_inobt(icur, &rec, offset); |
88fc7306 | 1605 | if (error) |
ff105f75 | 1606 | goto error_icur; |
88fc7306 BF |
1607 | |
1608 | /* | |
ff105f75 DC |
1609 | * Both trees have now been updated. We must update the perag and |
1610 | * superblock before we can check the freecount for each btree. | |
88fc7306 BF |
1611 | */ |
1612 | be32_add_cpu(&agi->agi_freecount, -1); | |
1613 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); | |
1614 | pag->pagi_freecount--; | |
1615 | ||
1616 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); | |
1617 | ||
ff105f75 | 1618 | error = xfs_check_agi_freecount(icur, agi); |
88fc7306 | 1619 | if (error) |
ff105f75 | 1620 | goto error_icur; |
88fc7306 BF |
1621 | error = xfs_check_agi_freecount(cur, agi); |
1622 | if (error) | |
ff105f75 | 1623 | goto error_icur; |
88fc7306 | 1624 | |
ff105f75 | 1625 | xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR); |
88fc7306 BF |
1626 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
1627 | xfs_perag_put(pag); | |
1628 | *inop = ino; | |
1629 | return 0; | |
1630 | ||
ff105f75 DC |
1631 | error_icur: |
1632 | xfs_btree_del_cursor(icur, XFS_BTREE_ERROR); | |
88fc7306 BF |
1633 | error_cur: |
1634 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
1635 | xfs_perag_put(pag); | |
1636 | return error; | |
1637 | } | |
1638 | ||
a2ceac1f DC |
1639 | /* |
1640 | * Allocate an inode on disk. | |
1641 | * | |
1642 | * Mode is used to tell whether the new inode will need space, and whether it | |
1643 | * is a directory. | |
1644 | * | |
1645 | * This function is designed to be called twice if it has to do an allocation | |
1646 | * to make more free inodes. On the first call, *IO_agbp should be set to NULL. | |
1647 | * If an inode is available without having to performn an allocation, an inode | |
1648 | * number is returned. In this case, *IO_agbp is set to NULL. If an allocation | |
1649 | * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp. | |
1650 | * The caller should then commit the current transaction, allocate a | |
1651 | * new transaction, and call xfs_dialloc() again, passing in the previous value | |
1652 | * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI | |
1653 | * buffer is locked across the two calls, the second call is guaranteed to have | |
1654 | * a free inode available. | |
1655 | * | |
1656 | * Once we successfully pick an inode its number is returned and the on-disk | |
1657 | * data structures are updated. The inode itself is not read in, since doing so | |
1658 | * would break ordering constraints with xfs_reclaim. | |
1659 | */ | |
1660 | int | |
1661 | xfs_dialloc( | |
1662 | struct xfs_trans *tp, | |
1663 | xfs_ino_t parent, | |
1664 | umode_t mode, | |
1665 | int okalloc, | |
1666 | struct xfs_buf **IO_agbp, | |
1667 | xfs_ino_t *inop) | |
1668 | { | |
1669 | struct xfs_mount *mp = tp->t_mountp; | |
1670 | struct xfs_buf *agbp; | |
1671 | xfs_agnumber_t agno; | |
1672 | int error; | |
1673 | int ialloced; | |
1674 | int noroom = 0; | |
1675 | xfs_agnumber_t start_agno; | |
1676 | struct xfs_perag *pag; | |
1677 | ||
1678 | if (*IO_agbp) { | |
1679 | /* | |
1680 | * If the caller passes in a pointer to the AGI buffer, | |
1681 | * continue where we left off before. In this case, we | |
1682 | * know that the allocation group has free inodes. | |
1683 | */ | |
1684 | agbp = *IO_agbp; | |
1685 | goto out_alloc; | |
1686 | } | |
1687 | ||
1688 | /* | |
1689 | * We do not have an agbp, so select an initial allocation | |
1690 | * group for inode allocation. | |
1691 | */ | |
1692 | start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc); | |
1693 | if (start_agno == NULLAGNUMBER) { | |
1694 | *inop = NULLFSINO; | |
1695 | return 0; | |
1696 | } | |
1697 | ||
1698 | /* | |
1699 | * If we have already hit the ceiling of inode blocks then clear | |
1700 | * okalloc so we scan all available agi structures for a free | |
1701 | * inode. | |
1702 | */ | |
1703 | if (mp->m_maxicount && | |
19ebedcf DC |
1704 | percpu_counter_read(&mp->m_icount) + mp->m_ialloc_inos > |
1705 | mp->m_maxicount) { | |
a2ceac1f DC |
1706 | noroom = 1; |
1707 | okalloc = 0; | |
1708 | } | |
1709 | ||
1710 | /* | |
1711 | * Loop until we find an allocation group that either has free inodes | |
1712 | * or in which we can allocate some inodes. Iterate through the | |
1713 | * allocation groups upward, wrapping at the end. | |
1714 | */ | |
1715 | agno = start_agno; | |
1716 | for (;;) { | |
1717 | pag = xfs_perag_get(mp, agno); | |
1718 | if (!pag->pagi_inodeok) { | |
1719 | xfs_ialloc_next_ag(mp); | |
1720 | goto nextag; | |
1721 | } | |
1722 | ||
1723 | if (!pag->pagi_init) { | |
1724 | error = xfs_ialloc_pagi_init(mp, tp, agno); | |
1725 | if (error) | |
1726 | goto out_error; | |
1727 | } | |
1728 | ||
1729 | /* | |
1730 | * Do a first racy fast path check if this AG is usable. | |
1731 | */ | |
1732 | if (!pag->pagi_freecount && !okalloc) | |
1733 | goto nextag; | |
1734 | ||
1735 | /* | |
1736 | * Then read in the AGI buffer and recheck with the AGI buffer | |
1737 | * lock held. | |
1738 | */ | |
1739 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); | |
1740 | if (error) | |
1741 | goto out_error; | |
1742 | ||
1743 | if (pag->pagi_freecount) { | |
1744 | xfs_perag_put(pag); | |
1745 | goto out_alloc; | |
1746 | } | |
1747 | ||
1748 | if (!okalloc) | |
1749 | goto nextag_relse_buffer; | |
1750 | ||
1751 | ||
1752 | error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced); | |
1753 | if (error) { | |
1754 | xfs_trans_brelse(tp, agbp); | |
1755 | ||
12b53197 | 1756 | if (error != -ENOSPC) |
a2ceac1f DC |
1757 | goto out_error; |
1758 | ||
1759 | xfs_perag_put(pag); | |
1760 | *inop = NULLFSINO; | |
1761 | return 0; | |
1762 | } | |
1763 | ||
1764 | if (ialloced) { | |
1765 | /* | |
1766 | * We successfully allocated some inodes, return | |
1767 | * the current context to the caller so that it | |
1768 | * can commit the current transaction and call | |
1769 | * us again where we left off. | |
1770 | */ | |
1771 | ASSERT(pag->pagi_freecount > 0); | |
1772 | xfs_perag_put(pag); | |
1773 | ||
1774 | *IO_agbp = agbp; | |
1775 | *inop = NULLFSINO; | |
1776 | return 0; | |
1777 | } | |
1778 | ||
1779 | nextag_relse_buffer: | |
1780 | xfs_trans_brelse(tp, agbp); | |
1781 | nextag: | |
1782 | xfs_perag_put(pag); | |
1783 | if (++agno == mp->m_sb.sb_agcount) | |
1784 | agno = 0; | |
1785 | if (agno == start_agno) { | |
1786 | *inop = NULLFSINO; | |
12b53197 | 1787 | return noroom ? -ENOSPC : 0; |
a2ceac1f DC |
1788 | } |
1789 | } | |
1790 | ||
1791 | out_alloc: | |
1792 | *IO_agbp = NULL; | |
1793 | return xfs_dialloc_ag(tp, agbp, parent, inop); | |
1794 | out_error: | |
1795 | xfs_perag_put(pag); | |
1e68581b | 1796 | return error; |
a2ceac1f DC |
1797 | } |
1798 | ||
7338c4b8 BF |
1799 | /* |
1800 | * Free the blocks of an inode chunk. We must consider that the inode chunk | |
1801 | * might be sparse and only free the regions that are allocated as part of the | |
1802 | * chunk. | |
1803 | */ | |
1804 | STATIC void | |
1805 | xfs_difree_inode_chunk( | |
1806 | struct xfs_mount *mp, | |
1807 | xfs_agnumber_t agno, | |
1808 | struct xfs_inobt_rec_incore *rec, | |
1809 | struct xfs_bmap_free *flist) | |
1810 | { | |
1811 | xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp, rec->ir_startino); | |
1812 | int startidx, endidx; | |
1813 | int nextbit; | |
1814 | xfs_agblock_t agbno; | |
1815 | int contigblk; | |
1816 | DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS); | |
1817 | ||
1818 | if (!xfs_inobt_issparse(rec->ir_holemask)) { | |
1819 | /* not sparse, calculate extent info directly */ | |
1820 | xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno, | |
1821 | XFS_AGINO_TO_AGBNO(mp, rec->ir_startino)), | |
1822 | mp->m_ialloc_blks, flist, mp); | |
1823 | return; | |
1824 | } | |
1825 | ||
1826 | /* holemask is only 16-bits (fits in an unsigned long) */ | |
1827 | ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0])); | |
1828 | holemask[0] = rec->ir_holemask; | |
1829 | ||
1830 | /* | |
1831 | * Find contiguous ranges of zeroes (i.e., allocated regions) in the | |
1832 | * holemask and convert the start/end index of each range to an extent. | |
1833 | * We start with the start and end index both pointing at the first 0 in | |
1834 | * the mask. | |
1835 | */ | |
1836 | startidx = endidx = find_first_zero_bit(holemask, | |
1837 | XFS_INOBT_HOLEMASK_BITS); | |
1838 | nextbit = startidx + 1; | |
1839 | while (startidx < XFS_INOBT_HOLEMASK_BITS) { | |
1840 | nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS, | |
1841 | nextbit); | |
1842 | /* | |
1843 | * If the next zero bit is contiguous, update the end index of | |
1844 | * the current range and continue. | |
1845 | */ | |
1846 | if (nextbit != XFS_INOBT_HOLEMASK_BITS && | |
1847 | nextbit == endidx + 1) { | |
1848 | endidx = nextbit; | |
1849 | goto next; | |
1850 | } | |
1851 | ||
1852 | /* | |
1853 | * nextbit is not contiguous with the current end index. Convert | |
1854 | * the current start/end to an extent and add it to the free | |
1855 | * list. | |
1856 | */ | |
1857 | agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) / | |
1858 | mp->m_sb.sb_inopblock; | |
1859 | contigblk = ((endidx - startidx + 1) * | |
1860 | XFS_INODES_PER_HOLEMASK_BIT) / | |
1861 | mp->m_sb.sb_inopblock; | |
1862 | ||
1863 | ASSERT(agbno % mp->m_sb.sb_spino_align == 0); | |
1864 | ASSERT(contigblk % mp->m_sb.sb_spino_align == 0); | |
1865 | xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno, agbno), contigblk, | |
1866 | flist, mp); | |
1867 | ||
1868 | /* reset range to current bit and carry on... */ | |
1869 | startidx = endidx = nextbit; | |
1870 | ||
1871 | next: | |
1872 | nextbit++; | |
1873 | } | |
1874 | } | |
1875 | ||
eb9a297a BF |
1876 | STATIC int |
1877 | xfs_difree_inobt( | |
1878 | struct xfs_mount *mp, | |
1879 | struct xfs_trans *tp, | |
1880 | struct xfs_buf *agbp, | |
1881 | xfs_agino_t agino, | |
1882 | struct xfs_bmap_free *flist, | |
1883 | int *deleted, | |
1884 | xfs_ino_t *first_ino, | |
1885 | struct xfs_inobt_rec_incore *orec) | |
3439d03a | 1886 | { |
eb9a297a BF |
1887 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
1888 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); | |
1889 | struct xfs_perag *pag; | |
1890 | struct xfs_btree_cur *cur; | |
1891 | struct xfs_inobt_rec_incore rec; | |
1892 | int ilen; | |
1893 | int error; | |
1894 | int i; | |
1895 | int off; | |
3439d03a | 1896 | |
3439d03a | 1897 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); |
eb9a297a BF |
1898 | ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length)); |
1899 | ||
3439d03a DC |
1900 | /* |
1901 | * Initialize the cursor. | |
1902 | */ | |
70eb7337 | 1903 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
3439d03a DC |
1904 | |
1905 | error = xfs_check_agi_freecount(cur, agi); | |
1906 | if (error) | |
1907 | goto error0; | |
1908 | ||
1909 | /* | |
1910 | * Look for the entry describing this inode. | |
1911 | */ | |
1912 | if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) { | |
1913 | xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.", | |
1914 | __func__, error); | |
1915 | goto error0; | |
1916 | } | |
19ebedcf | 1917 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
3439d03a DC |
1918 | error = xfs_inobt_get_rec(cur, &rec, &i); |
1919 | if (error) { | |
1920 | xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.", | |
1921 | __func__, error); | |
1922 | goto error0; | |
1923 | } | |
19ebedcf | 1924 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
3439d03a DC |
1925 | /* |
1926 | * Get the offset in the inode chunk. | |
1927 | */ | |
1928 | off = agino - rec.ir_startino; | |
1929 | ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); | |
1930 | ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); | |
1931 | /* | |
1932 | * Mark the inode free & increment the count. | |
1933 | */ | |
1934 | rec.ir_free |= XFS_INOBT_MASK(off); | |
1935 | rec.ir_freecount++; | |
1936 | ||
1937 | /* | |
f6580bcf BF |
1938 | * When an inode chunk is free, it becomes eligible for removal. Don't |
1939 | * remove the chunk if the block size is large enough for multiple inode | |
1940 | * chunks (that might not be free). | |
3439d03a DC |
1941 | */ |
1942 | if (!(mp->m_flags & XFS_MOUNT_IKEEP) && | |
f6580bcf BF |
1943 | rec.ir_free == XFS_INOBT_ALL_FREE && |
1944 | mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) { | |
3439d03a | 1945 | |
66260204 | 1946 | *deleted = 1; |
3439d03a DC |
1947 | *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino); |
1948 | ||
1949 | /* | |
1950 | * Remove the inode cluster from the AGI B+Tree, adjust the | |
1951 | * AGI and Superblock inode counts, and mark the disk space | |
1952 | * to be freed when the transaction is committed. | |
1953 | */ | |
f6580bcf | 1954 | ilen = rec.ir_freecount; |
3439d03a DC |
1955 | be32_add_cpu(&agi->agi_count, -ilen); |
1956 | be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); | |
1957 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); | |
1958 | pag = xfs_perag_get(mp, agno); | |
1959 | pag->pagi_freecount -= ilen - 1; | |
1960 | xfs_perag_put(pag); | |
1961 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); | |
1962 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); | |
1963 | ||
1964 | if ((error = xfs_btree_delete(cur, &i))) { | |
1965 | xfs_warn(mp, "%s: xfs_btree_delete returned error %d.", | |
1966 | __func__, error); | |
1967 | goto error0; | |
1968 | } | |
1969 | ||
7338c4b8 | 1970 | xfs_difree_inode_chunk(mp, agno, &rec, flist); |
3439d03a | 1971 | } else { |
66260204 | 1972 | *deleted = 0; |
3439d03a DC |
1973 | |
1974 | error = xfs_inobt_update(cur, &rec); | |
1975 | if (error) { | |
1976 | xfs_warn(mp, "%s: xfs_inobt_update returned error %d.", | |
1977 | __func__, error); | |
1978 | goto error0; | |
1979 | } | |
1980 | ||
1981 | /* | |
1982 | * Change the inode free counts and log the ag/sb changes. | |
1983 | */ | |
1984 | be32_add_cpu(&agi->agi_freecount, 1); | |
1985 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); | |
1986 | pag = xfs_perag_get(mp, agno); | |
1987 | pag->pagi_freecount++; | |
1988 | xfs_perag_put(pag); | |
1989 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); | |
1990 | } | |
1991 | ||
1992 | error = xfs_check_agi_freecount(cur, agi); | |
1993 | if (error) | |
1994 | goto error0; | |
1995 | ||
eb9a297a | 1996 | *orec = rec; |
3439d03a DC |
1997 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
1998 | return 0; | |
1999 | ||
2000 | error0: | |
2001 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
2002 | return error; | |
2003 | } | |
2004 | ||
1bb93fd1 BF |
2005 | /* |
2006 | * Free an inode in the free inode btree. | |
2007 | */ | |
2008 | STATIC int | |
2009 | xfs_difree_finobt( | |
2010 | struct xfs_mount *mp, | |
2011 | struct xfs_trans *tp, | |
2012 | struct xfs_buf *agbp, | |
2013 | xfs_agino_t agino, | |
2014 | struct xfs_inobt_rec_incore *ibtrec) /* inobt record */ | |
2015 | { | |
2016 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); | |
2017 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); | |
2018 | struct xfs_btree_cur *cur; | |
2019 | struct xfs_inobt_rec_incore rec; | |
2020 | int offset = agino - ibtrec->ir_startino; | |
2021 | int error; | |
2022 | int i; | |
2023 | ||
2024 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO); | |
2025 | ||
2026 | error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i); | |
2027 | if (error) | |
2028 | goto error; | |
2029 | if (i == 0) { | |
2030 | /* | |
2031 | * If the record does not exist in the finobt, we must have just | |
2032 | * freed an inode in a previously fully allocated chunk. If not, | |
2033 | * something is out of sync. | |
2034 | */ | |
19ebedcf | 2035 | XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error); |
1bb93fd1 | 2036 | |
11640e30 BF |
2037 | error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask, |
2038 | ibtrec->ir_count, | |
2039 | ibtrec->ir_freecount, | |
1bb93fd1 BF |
2040 | ibtrec->ir_free, &i); |
2041 | if (error) | |
2042 | goto error; | |
2043 | ASSERT(i == 1); | |
2044 | ||
2045 | goto out; | |
2046 | } | |
2047 | ||
2048 | /* | |
ff105f75 DC |
2049 | * Read and update the existing record. We could just copy the ibtrec |
2050 | * across here, but that would defeat the purpose of having redundant | |
2051 | * metadata. By making the modifications independently, we can catch | |
2052 | * corruptions that we wouldn't see if we just copied from one record | |
2053 | * to another. | |
1bb93fd1 BF |
2054 | */ |
2055 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
2056 | if (error) | |
2057 | goto error; | |
19ebedcf | 2058 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); |
1bb93fd1 BF |
2059 | |
2060 | rec.ir_free |= XFS_INOBT_MASK(offset); | |
2061 | rec.ir_freecount++; | |
2062 | ||
19ebedcf | 2063 | XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) && |
1bb93fd1 BF |
2064 | (rec.ir_freecount == ibtrec->ir_freecount), |
2065 | error); | |
2066 | ||
2067 | /* | |
2068 | * The content of inobt records should always match between the inobt | |
2069 | * and finobt. The lifecycle of records in the finobt is different from | |
2070 | * the inobt in that the finobt only tracks records with at least one | |
ff105f75 DC |
2071 | * free inode. Hence, if all of the inodes are free and we aren't |
2072 | * keeping inode chunks permanently on disk, remove the record. | |
2073 | * Otherwise, update the record with the new information. | |
f6580bcf BF |
2074 | * |
2075 | * Note that we currently can't free chunks when the block size is large | |
2076 | * enough for multiple chunks. Leave the finobt record to remain in sync | |
2077 | * with the inobt. | |
1bb93fd1 | 2078 | */ |
f6580bcf BF |
2079 | if (rec.ir_free == XFS_INOBT_ALL_FREE && |
2080 | mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK && | |
1bb93fd1 | 2081 | !(mp->m_flags & XFS_MOUNT_IKEEP)) { |
1bb93fd1 BF |
2082 | error = xfs_btree_delete(cur, &i); |
2083 | if (error) | |
2084 | goto error; | |
2085 | ASSERT(i == 1); | |
2086 | } else { | |
1bb93fd1 BF |
2087 | error = xfs_inobt_update(cur, &rec); |
2088 | if (error) | |
2089 | goto error; | |
2090 | } | |
2091 | ||
2092 | out: | |
2093 | error = xfs_check_agi_freecount(cur, agi); | |
2094 | if (error) | |
2095 | goto error; | |
2096 | ||
2097 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
2098 | return 0; | |
2099 | ||
2100 | error: | |
2101 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
2102 | return error; | |
2103 | } | |
2104 | ||
eb9a297a BF |
2105 | /* |
2106 | * Free disk inode. Carefully avoids touching the incore inode, all | |
2107 | * manipulations incore are the caller's responsibility. | |
2108 | * The on-disk inode is not changed by this operation, only the | |
2109 | * btree (free inode mask) is changed. | |
2110 | */ | |
2111 | int | |
2112 | xfs_difree( | |
2113 | struct xfs_trans *tp, /* transaction pointer */ | |
2114 | xfs_ino_t inode, /* inode to be freed */ | |
2115 | struct xfs_bmap_free *flist, /* extents to free */ | |
2116 | int *deleted,/* set if inode cluster was deleted */ | |
2117 | xfs_ino_t *first_ino)/* first inode in deleted cluster */ | |
2118 | { | |
2119 | /* REFERENCED */ | |
2120 | xfs_agblock_t agbno; /* block number containing inode */ | |
2121 | struct xfs_buf *agbp; /* buffer for allocation group header */ | |
2122 | xfs_agino_t agino; /* allocation group inode number */ | |
2123 | xfs_agnumber_t agno; /* allocation group number */ | |
2124 | int error; /* error return value */ | |
2125 | struct xfs_mount *mp; /* mount structure for filesystem */ | |
2126 | struct xfs_inobt_rec_incore rec;/* btree record */ | |
2127 | ||
2128 | mp = tp->t_mountp; | |
2129 | ||
2130 | /* | |
2131 | * Break up inode number into its components. | |
2132 | */ | |
2133 | agno = XFS_INO_TO_AGNO(mp, inode); | |
2134 | if (agno >= mp->m_sb.sb_agcount) { | |
2135 | xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).", | |
2136 | __func__, agno, mp->m_sb.sb_agcount); | |
2137 | ASSERT(0); | |
12b53197 | 2138 | return -EINVAL; |
eb9a297a BF |
2139 | } |
2140 | agino = XFS_INO_TO_AGINO(mp, inode); | |
2141 | if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) { | |
2142 | xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).", | |
2143 | __func__, (unsigned long long)inode, | |
2144 | (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino)); | |
2145 | ASSERT(0); | |
12b53197 | 2146 | return -EINVAL; |
eb9a297a BF |
2147 | } |
2148 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); | |
2149 | if (agbno >= mp->m_sb.sb_agblocks) { | |
2150 | xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).", | |
2151 | __func__, agbno, mp->m_sb.sb_agblocks); | |
2152 | ASSERT(0); | |
12b53197 | 2153 | return -EINVAL; |
eb9a297a BF |
2154 | } |
2155 | /* | |
2156 | * Get the allocation group header. | |
2157 | */ | |
2158 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); | |
2159 | if (error) { | |
2160 | xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.", | |
2161 | __func__, error); | |
2162 | return error; | |
2163 | } | |
2164 | ||
2165 | /* | |
2166 | * Fix up the inode allocation btree. | |
2167 | */ | |
2168 | error = xfs_difree_inobt(mp, tp, agbp, agino, flist, deleted, first_ino, | |
2169 | &rec); | |
2170 | if (error) | |
2171 | goto error0; | |
2172 | ||
1bb93fd1 BF |
2173 | /* |
2174 | * Fix up the free inode btree. | |
2175 | */ | |
2176 | if (xfs_sb_version_hasfinobt(&mp->m_sb)) { | |
2177 | error = xfs_difree_finobt(mp, tp, agbp, agino, &rec); | |
2178 | if (error) | |
2179 | goto error0; | |
2180 | } | |
2181 | ||
eb9a297a BF |
2182 | return 0; |
2183 | ||
2184 | error0: | |
2185 | return error; | |
2186 | } | |
2187 | ||
56b2de80 DC |
2188 | STATIC int |
2189 | xfs_imap_lookup( | |
2190 | struct xfs_mount *mp, | |
2191 | struct xfs_trans *tp, | |
2192 | xfs_agnumber_t agno, | |
2193 | xfs_agino_t agino, | |
2194 | xfs_agblock_t agbno, | |
2195 | xfs_agblock_t *chunk_agbno, | |
2196 | xfs_agblock_t *offset_agbno, | |
2197 | int flags) | |
2198 | { | |
2199 | struct xfs_inobt_rec_incore rec; | |
2200 | struct xfs_btree_cur *cur; | |
2201 | struct xfs_buf *agbp; | |
2202 | int error; | |
2203 | int i; | |
2204 | ||
2205 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); | |
2206 | if (error) { | |
a2ceac1f DC |
2207 | xfs_alert(mp, |
2208 | "%s: xfs_ialloc_read_agi() returned error %d, agno %d", | |
2209 | __func__, error, agno); | |
56b2de80 DC |
2210 | return error; |
2211 | } | |
2212 | ||
2213 | /* | |
2214 | * Lookup the inode record for the given agino. If the record cannot be | |
2215 | * found, then it's an invalid inode number and we should abort. Once | |
2216 | * we have a record, we need to ensure it contains the inode number | |
2217 | * we are looking up. | |
2218 | */ | |
70eb7337 | 2219 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
56b2de80 DC |
2220 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i); |
2221 | if (!error) { | |
2222 | if (i) | |
2223 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
2224 | if (!error && i == 0) | |
12b53197 | 2225 | error = -EINVAL; |
56b2de80 DC |
2226 | } |
2227 | ||
2228 | xfs_trans_brelse(tp, agbp); | |
2229 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
2230 | if (error) | |
2231 | return error; | |
2232 | ||
2233 | /* check that the returned record contains the required inode */ | |
2234 | if (rec.ir_startino > agino || | |
ff105f75 | 2235 | rec.ir_startino + mp->m_ialloc_inos <= agino) |
12b53197 | 2236 | return -EINVAL; |
56b2de80 DC |
2237 | |
2238 | /* for untrusted inodes check it is allocated first */ | |
2239 | if ((flags & XFS_IGET_UNTRUSTED) && | |
2240 | (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))) | |
12b53197 | 2241 | return -EINVAL; |
56b2de80 DC |
2242 | |
2243 | *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino); | |
2244 | *offset_agbno = agbno - *chunk_agbno; | |
2245 | return 0; | |
2246 | } | |
2bd0ea18 NS |
2247 | |
2248 | /* | |
56b2de80 | 2249 | * Return the location of the inode in imap, for mapping it into a buffer. |
2bd0ea18 | 2250 | */ |
2bd0ea18 | 2251 | int |
56b2de80 DC |
2252 | xfs_imap( |
2253 | xfs_mount_t *mp, /* file system mount structure */ | |
2254 | xfs_trans_t *tp, /* transaction pointer */ | |
2bd0ea18 | 2255 | xfs_ino_t ino, /* inode to locate */ |
56b2de80 DC |
2256 | struct xfs_imap *imap, /* location map structure */ |
2257 | uint flags) /* flags for inode btree lookup */ | |
2bd0ea18 NS |
2258 | { |
2259 | xfs_agblock_t agbno; /* block number of inode in the alloc group */ | |
2bd0ea18 NS |
2260 | xfs_agino_t agino; /* inode number within alloc group */ |
2261 | xfs_agnumber_t agno; /* allocation group number */ | |
2262 | int blks_per_cluster; /* num blocks per inode cluster */ | |
2263 | xfs_agblock_t chunk_agbno; /* first block in inode chunk */ | |
2bd0ea18 | 2264 | xfs_agblock_t cluster_agbno; /* first block in inode cluster */ |
2bd0ea18 | 2265 | int error; /* error code */ |
dfc130f3 | 2266 | int offset; /* index of inode in its buffer */ |
6bddecbc | 2267 | xfs_agblock_t offset_agbno; /* blks from chunk start to inode */ |
2bd0ea18 NS |
2268 | |
2269 | ASSERT(ino != NULLFSINO); | |
56b2de80 | 2270 | |
2bd0ea18 NS |
2271 | /* |
2272 | * Split up the inode number into its parts. | |
2273 | */ | |
2274 | agno = XFS_INO_TO_AGNO(mp, ino); | |
2275 | agino = XFS_INO_TO_AGINO(mp, ino); | |
2276 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); | |
2277 | if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || | |
63518810 NS |
2278 | ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
2279 | #ifdef DEBUG | |
56b2de80 DC |
2280 | /* |
2281 | * Don't output diagnostic information for untrusted inodes | |
2282 | * as they can be invalid without implying corruption. | |
2283 | */ | |
2284 | if (flags & XFS_IGET_UNTRUSTED) | |
12b53197 | 2285 | return -EINVAL; |
5000d01d | 2286 | if (agno >= mp->m_sb.sb_agcount) { |
a2ceac1f DC |
2287 | xfs_alert(mp, |
2288 | "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)", | |
2289 | __func__, agno, mp->m_sb.sb_agcount); | |
63518810 NS |
2290 | } |
2291 | if (agbno >= mp->m_sb.sb_agblocks) { | |
a2ceac1f DC |
2292 | xfs_alert(mp, |
2293 | "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)", | |
2294 | __func__, (unsigned long long)agbno, | |
2295 | (unsigned long)mp->m_sb.sb_agblocks); | |
63518810 NS |
2296 | } |
2297 | if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) { | |
a2ceac1f DC |
2298 | xfs_alert(mp, |
2299 | "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)", | |
2300 | __func__, ino, | |
2301 | XFS_AGINO_TO_INO(mp, agno, agino)); | |
63518810 | 2302 | } |
5e656dbb | 2303 | xfs_stack_trace(); |
63518810 | 2304 | #endif /* DEBUG */ |
12b53197 | 2305 | return -EINVAL; |
63518810 | 2306 | } |
56b2de80 | 2307 | |
ff105f75 | 2308 | blks_per_cluster = xfs_icluster_size_fsb(mp); |
56b2de80 DC |
2309 | |
2310 | /* | |
2311 | * For bulkstat and handle lookups, we have an untrusted inode number | |
2312 | * that we have to verify is valid. We cannot do this just by reading | |
2313 | * the inode buffer as it may have been unlinked and removed leaving | |
2314 | * inodes in stale state on disk. Hence we have to do a btree lookup | |
2315 | * in all cases where an untrusted inode number is passed. | |
2316 | */ | |
2317 | if (flags & XFS_IGET_UNTRUSTED) { | |
2318 | error = xfs_imap_lookup(mp, tp, agno, agino, agbno, | |
2319 | &chunk_agbno, &offset_agbno, flags); | |
2320 | if (error) | |
2321 | return error; | |
2322 | goto out_map; | |
2323 | } | |
2324 | ||
2325 | /* | |
2326 | * If the inode cluster size is the same as the blocksize or | |
2327 | * smaller we get to the buffer by simple arithmetics. | |
2328 | */ | |
ff105f75 | 2329 | if (blks_per_cluster == 1) { |
2bd0ea18 NS |
2330 | offset = XFS_INO_TO_OFFSET(mp, ino); |
2331 | ASSERT(offset < mp->m_sb.sb_inopblock); | |
56b2de80 DC |
2332 | |
2333 | imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno); | |
2334 | imap->im_len = XFS_FSB_TO_BB(mp, 1); | |
2335 | imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); | |
2bd0ea18 NS |
2336 | return 0; |
2337 | } | |
56b2de80 DC |
2338 | |
2339 | /* | |
2340 | * If the inode chunks are aligned then use simple maths to | |
2341 | * find the location. Otherwise we have to do a btree | |
2342 | * lookup to find the location. | |
2343 | */ | |
2bd0ea18 NS |
2344 | if (mp->m_inoalign_mask) { |
2345 | offset_agbno = agbno & mp->m_inoalign_mask; | |
2346 | chunk_agbno = agbno - offset_agbno; | |
2347 | } else { | |
56b2de80 DC |
2348 | error = xfs_imap_lookup(mp, tp, agno, agino, agbno, |
2349 | &chunk_agbno, &offset_agbno, flags); | |
2bd0ea18 NS |
2350 | if (error) |
2351 | return error; | |
2bd0ea18 | 2352 | } |
56b2de80 DC |
2353 | |
2354 | out_map: | |
2bd0ea18 NS |
2355 | ASSERT(agbno >= chunk_agbno); |
2356 | cluster_agbno = chunk_agbno + | |
2357 | ((offset_agbno / blks_per_cluster) * blks_per_cluster); | |
2358 | offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + | |
2359 | XFS_INO_TO_OFFSET(mp, ino); | |
56b2de80 DC |
2360 | |
2361 | imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno); | |
2362 | imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster); | |
2363 | imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); | |
2364 | ||
2365 | /* | |
2366 | * If the inode number maps to a block outside the bounds | |
2367 | * of the file system then return NULL rather than calling | |
2368 | * read_buf and panicing when we get an error from the | |
2369 | * driver. | |
2370 | */ | |
2371 | if ((imap->im_blkno + imap->im_len) > | |
2372 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { | |
a2ceac1f DC |
2373 | xfs_alert(mp, |
2374 | "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)", | |
2375 | __func__, (unsigned long long) imap->im_blkno, | |
56b2de80 DC |
2376 | (unsigned long long) imap->im_len, |
2377 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); | |
12b53197 | 2378 | return -EINVAL; |
56b2de80 | 2379 | } |
2bd0ea18 | 2380 | return 0; |
2bd0ea18 NS |
2381 | } |
2382 | ||
2383 | /* | |
2384 | * Compute and fill in value of m_in_maxlevels. | |
2385 | */ | |
2386 | void | |
2387 | xfs_ialloc_compute_maxlevels( | |
2388 | xfs_mount_t *mp) /* file system mount structure */ | |
2389 | { | |
2390 | int level; | |
2391 | uint maxblocks; | |
2392 | uint maxleafents; | |
2393 | int minleafrecs; | |
2394 | int minnoderecs; | |
2395 | ||
2396 | maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >> | |
2397 | XFS_INODES_PER_CHUNK_LOG; | |
2398 | minleafrecs = mp->m_alloc_mnr[0]; | |
2399 | minnoderecs = mp->m_alloc_mnr[1]; | |
2400 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; | |
2401 | for (level = 1; maxblocks > 1; level++) | |
2402 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; | |
2403 | mp->m_in_maxlevels = level; | |
2404 | } | |
2405 | ||
2406 | /* | |
ff105f75 DC |
2407 | * Log specified fields for the ag hdr (inode section). The growth of the agi |
2408 | * structure over time requires that we interpret the buffer as two logical | |
2409 | * regions delineated by the end of the unlinked list. This is due to the size | |
2410 | * of the hash table and its location in the middle of the agi. | |
2411 | * | |
2412 | * For example, a request to log a field before agi_unlinked and a field after | |
2413 | * agi_unlinked could cause us to log the entire hash table and use an excessive | |
2414 | * amount of log space. To avoid this behavior, log the region up through | |
2415 | * agi_unlinked in one call and the region after agi_unlinked through the end of | |
2416 | * the structure in another. | |
2bd0ea18 NS |
2417 | */ |
2418 | void | |
2419 | xfs_ialloc_log_agi( | |
2420 | xfs_trans_t *tp, /* transaction pointer */ | |
2421 | xfs_buf_t *bp, /* allocation group header buffer */ | |
2422 | int fields) /* bitmask of fields to log */ | |
2423 | { | |
2424 | int first; /* first byte number */ | |
2425 | int last; /* last byte number */ | |
2426 | static const short offsets[] = { /* field starting offsets */ | |
2427 | /* keep in sync with bit definitions */ | |
2428 | offsetof(xfs_agi_t, agi_magicnum), | |
2429 | offsetof(xfs_agi_t, agi_versionnum), | |
2430 | offsetof(xfs_agi_t, agi_seqno), | |
2431 | offsetof(xfs_agi_t, agi_length), | |
2432 | offsetof(xfs_agi_t, agi_count), | |
2433 | offsetof(xfs_agi_t, agi_root), | |
2434 | offsetof(xfs_agi_t, agi_level), | |
2435 | offsetof(xfs_agi_t, agi_freecount), | |
2436 | offsetof(xfs_agi_t, agi_newino), | |
2437 | offsetof(xfs_agi_t, agi_dirino), | |
2438 | offsetof(xfs_agi_t, agi_unlinked), | |
c0a4c227 BF |
2439 | offsetof(xfs_agi_t, agi_free_root), |
2440 | offsetof(xfs_agi_t, agi_free_level), | |
2bd0ea18 NS |
2441 | sizeof(xfs_agi_t) |
2442 | }; | |
2443 | #ifdef DEBUG | |
2444 | xfs_agi_t *agi; /* allocation group header */ | |
2445 | ||
2446 | agi = XFS_BUF_TO_AGI(bp); | |
a2ceac1f | 2447 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); |
2bd0ea18 | 2448 | #endif |
ff105f75 | 2449 | |
c0a4c227 BF |
2450 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF); |
2451 | ||
2bd0ea18 | 2452 | /* |
c0a4c227 | 2453 | * Compute byte offsets for the first and last fields in the first |
ff105f75 DC |
2454 | * region and log the agi buffer. This only logs up through |
2455 | * agi_unlinked. | |
2bd0ea18 | 2456 | */ |
c0a4c227 BF |
2457 | if (fields & XFS_AGI_ALL_BITS_R1) { |
2458 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1, | |
2459 | &first, &last); | |
2460 | xfs_trans_log_buf(tp, bp, first, last); | |
2461 | } | |
2462 | ||
2463 | /* | |
ff105f75 DC |
2464 | * Mask off the bits in the first region and calculate the first and |
2465 | * last field offsets for any bits in the second region. | |
c0a4c227 BF |
2466 | */ |
2467 | fields &= ~XFS_AGI_ALL_BITS_R1; | |
2468 | if (fields) { | |
2469 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2, | |
2470 | &first, &last); | |
2471 | xfs_trans_log_buf(tp, bp, first, last); | |
2472 | } | |
2bd0ea18 NS |
2473 | } |
2474 | ||
56b2de80 DC |
2475 | #ifdef DEBUG |
2476 | STATIC void | |
2477 | xfs_check_agi_unlinked( | |
2478 | struct xfs_agi *agi) | |
2479 | { | |
2480 | int i; | |
2481 | ||
2482 | for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) | |
2483 | ASSERT(agi->agi_unlinked[i]); | |
2484 | } | |
2485 | #else | |
2486 | #define xfs_check_agi_unlinked(agi) | |
2487 | #endif | |
2488 | ||
dd5b876e | 2489 | static bool |
a2ceac1f DC |
2490 | xfs_agi_verify( |
2491 | struct xfs_buf *bp) | |
2492 | { | |
2493 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
2494 | struct xfs_agi *agi = XFS_BUF_TO_AGI(bp); | |
a2ceac1f | 2495 | |
dd5b876e DC |
2496 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
2497 | !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid)) | |
2498 | return false; | |
a2ceac1f DC |
2499 | /* |
2500 | * Validate the magic number of the agi block. | |
2501 | */ | |
dd5b876e DC |
2502 | if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC)) |
2503 | return false; | |
2504 | if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum))) | |
2505 | return false; | |
a2ceac1f | 2506 | |
5a35bf2c DC |
2507 | if (be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS) |
2508 | return false; | |
a2ceac1f DC |
2509 | /* |
2510 | * during growfs operations, the perag is not fully initialised, | |
2511 | * so we can't use it for any useful checking. growfs ensures we can't | |
2512 | * use it by using uncached buffers that don't have the perag attached | |
2513 | * so we can detect and avoid this problem. | |
2514 | */ | |
dd5b876e DC |
2515 | if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno) |
2516 | return false; | |
a2ceac1f | 2517 | |
a2ceac1f | 2518 | xfs_check_agi_unlinked(agi); |
dd5b876e | 2519 | return true; |
a2ceac1f DC |
2520 | } |
2521 | ||
2522 | static void | |
2523 | xfs_agi_read_verify( | |
2524 | struct xfs_buf *bp) | |
2525 | { | |
dd5b876e | 2526 | struct xfs_mount *mp = bp->b_target->bt_mount; |
dd5b876e | 2527 | |
45922933 DC |
2528 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
2529 | !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF)) | |
12b53197 | 2530 | xfs_buf_ioerror(bp, -EFSBADCRC); |
45922933 DC |
2531 | else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp, |
2532 | XFS_ERRTAG_IALLOC_READ_AGI, | |
2533 | XFS_RANDOM_IALLOC_READ_AGI)) | |
12b53197 | 2534 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
45922933 DC |
2535 | |
2536 | if (bp->b_error) | |
2537 | xfs_verifier_error(bp); | |
a2ceac1f DC |
2538 | } |
2539 | ||
2540 | static void | |
2541 | xfs_agi_write_verify( | |
2542 | struct xfs_buf *bp) | |
2543 | { | |
dd5b876e DC |
2544 | struct xfs_mount *mp = bp->b_target->bt_mount; |
2545 | struct xfs_buf_log_item *bip = bp->b_fspriv; | |
2546 | ||
2547 | if (!xfs_agi_verify(bp)) { | |
12b53197 | 2548 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
45922933 | 2549 | xfs_verifier_error(bp); |
dd5b876e DC |
2550 | return; |
2551 | } | |
2552 | ||
2553 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
2554 | return; | |
2555 | ||
2556 | if (bip) | |
2557 | XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
43b5aeed | 2558 | xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF); |
a2ceac1f DC |
2559 | } |
2560 | ||
2561 | const struct xfs_buf_ops xfs_agi_buf_ops = { | |
2562 | .verify_read = xfs_agi_read_verify, | |
2563 | .verify_write = xfs_agi_write_verify, | |
2564 | }; | |
2565 | ||
2bd0ea18 NS |
2566 | /* |
2567 | * Read in the allocation group header (inode allocation section) | |
2568 | */ | |
2569 | int | |
56b2de80 DC |
2570 | xfs_read_agi( |
2571 | struct xfs_mount *mp, /* file system mount structure */ | |
2572 | struct xfs_trans *tp, /* transaction pointer */ | |
2573 | xfs_agnumber_t agno, /* allocation group number */ | |
2574 | struct xfs_buf **bpp) /* allocation group hdr buf */ | |
2bd0ea18 | 2575 | { |
56b2de80 | 2576 | int error; |
2bd0ea18 | 2577 | |
ff105f75 | 2578 | trace_xfs_read_agi(mp, agno); |
56b2de80 | 2579 | |
ff105f75 | 2580 | ASSERT(agno != NULLAGNUMBER); |
56b2de80 | 2581 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, |
9440d84d | 2582 | XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), |
a2ceac1f | 2583 | XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops); |
9440d84d | 2584 | if (error) |
2bd0ea18 | 2585 | return error; |
56b2de80 | 2586 | |
a2ceac1f | 2587 | xfs_buf_set_ref(*bpp, XFS_AGI_REF); |
56b2de80 DC |
2588 | return 0; |
2589 | } | |
2590 | ||
2591 | int | |
2592 | xfs_ialloc_read_agi( | |
2593 | struct xfs_mount *mp, /* file system mount structure */ | |
2594 | struct xfs_trans *tp, /* transaction pointer */ | |
2595 | xfs_agnumber_t agno, /* allocation group number */ | |
2596 | struct xfs_buf **bpp) /* allocation group hdr buf */ | |
2597 | { | |
2598 | struct xfs_agi *agi; /* allocation group header */ | |
2599 | struct xfs_perag *pag; /* per allocation group data */ | |
2600 | int error; | |
2601 | ||
ff105f75 DC |
2602 | trace_xfs_ialloc_read_agi(mp, agno); |
2603 | ||
56b2de80 DC |
2604 | error = xfs_read_agi(mp, tp, agno, bpp); |
2605 | if (error) | |
2606 | return error; | |
2607 | ||
2608 | agi = XFS_BUF_TO_AGI(*bpp); | |
2609 | pag = xfs_perag_get(mp, agno); | |
2bd0ea18 | 2610 | if (!pag->pagi_init) { |
6e3140c7 | 2611 | pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); |
cdded3d8 | 2612 | pag->pagi_count = be32_to_cpu(agi->agi_count); |
2bd0ea18 | 2613 | pag->pagi_init = 1; |
9440d84d | 2614 | } |
9440d84d | 2615 | |
56b2de80 DC |
2616 | /* |
2617 | * It's possible for these to be out of sync if | |
2618 | * we are in the middle of a forced shutdown. | |
2619 | */ | |
2620 | ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || | |
2621 | XFS_FORCED_SHUTDOWN(mp)); | |
2622 | xfs_perag_put(pag); | |
2bd0ea18 NS |
2623 | return 0; |
2624 | } | |
cdded3d8 DC |
2625 | |
2626 | /* | |
2627 | * Read in the agi to initialise the per-ag data in the mount structure | |
2628 | */ | |
2629 | int | |
2630 | xfs_ialloc_pagi_init( | |
2631 | xfs_mount_t *mp, /* file system mount structure */ | |
2632 | xfs_trans_t *tp, /* transaction pointer */ | |
2633 | xfs_agnumber_t agno) /* allocation group number */ | |
2634 | { | |
2635 | xfs_buf_t *bp = NULL; | |
2636 | int error; | |
2637 | ||
5e656dbb BN |
2638 | error = xfs_ialloc_read_agi(mp, tp, agno, &bp); |
2639 | if (error) | |
cdded3d8 DC |
2640 | return error; |
2641 | if (bp) | |
2642 | xfs_trans_brelse(tp, bp); | |
2643 | return 0; | |
2644 | } |