]>
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 NS |
17 | */ |
18 | ||
19 | #include <xfs.h> | |
20 | ||
2bd0ea18 NS |
21 | |
22 | /* | |
23 | * Allocation group level functions. | |
24 | */ | |
5e656dbb BN |
25 | static inline int |
26 | xfs_ialloc_cluster_alignment( | |
27 | xfs_alloc_arg_t *args) | |
28 | { | |
29 | if (xfs_sb_version_hasalign(&args->mp->m_sb) && | |
30 | args->mp->m_sb.sb_inoalignmt >= | |
31 | XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp))) | |
32 | return args->mp->m_sb.sb_inoalignmt; | |
33 | return 1; | |
34 | } | |
2bd0ea18 | 35 | |
b194c7d8 | 36 | /* |
56b2de80 | 37 | * Lookup a record by ino in the btree given by cur. |
b194c7d8 BN |
38 | */ |
39 | int /* error */ | |
56b2de80 | 40 | xfs_inobt_lookup( |
b194c7d8 BN |
41 | struct xfs_btree_cur *cur, /* btree cursor */ |
42 | xfs_agino_t ino, /* starting inode of chunk */ | |
56b2de80 | 43 | xfs_lookup_t dir, /* <=, >=, == */ |
b194c7d8 BN |
44 | int *stat) /* success/failure */ |
45 | { | |
46 | cur->bc_rec.i.ir_startino = ino; | |
56b2de80 DC |
47 | cur->bc_rec.i.ir_freecount = 0; |
48 | cur->bc_rec.i.ir_free = 0; | |
49 | return xfs_btree_lookup(cur, dir, stat); | |
b194c7d8 BN |
50 | } |
51 | ||
52 | /* | |
56b2de80 | 53 | * Update the record referred to by cur to the value given. |
b194c7d8 BN |
54 | * This either works (return 0) or gets an EFSCORRUPTED error. |
55 | */ | |
56 | STATIC int /* error */ | |
57 | xfs_inobt_update( | |
58 | struct xfs_btree_cur *cur, /* btree cursor */ | |
56b2de80 | 59 | xfs_inobt_rec_incore_t *irec) /* btree record */ |
b194c7d8 BN |
60 | { |
61 | union xfs_btree_rec rec; | |
62 | ||
56b2de80 DC |
63 | rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino); |
64 | rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount); | |
65 | rec.inobt.ir_free = cpu_to_be64(irec->ir_free); | |
b194c7d8 BN |
66 | return xfs_btree_update(cur, &rec); |
67 | } | |
68 | ||
69 | /* | |
70 | * Get the data from the pointed-to record. | |
71 | */ | |
72 | int /* error */ | |
73 | xfs_inobt_get_rec( | |
74 | struct xfs_btree_cur *cur, /* btree cursor */ | |
56b2de80 | 75 | xfs_inobt_rec_incore_t *irec, /* btree record */ |
b194c7d8 BN |
76 | int *stat) /* output: success/failure */ |
77 | { | |
78 | union xfs_btree_rec *rec; | |
79 | int error; | |
80 | ||
81 | error = xfs_btree_get_rec(cur, &rec, stat); | |
82 | if (!error && *stat == 1) { | |
56b2de80 DC |
83 | irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino); |
84 | irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount); | |
85 | irec->ir_free = be64_to_cpu(rec->inobt.ir_free); | |
b194c7d8 BN |
86 | } |
87 | return error; | |
88 | } | |
89 | ||
56b2de80 DC |
90 | /* |
91 | * Verify that the number of free inodes in the AGI is correct. | |
92 | */ | |
93 | #ifdef DEBUG | |
94 | STATIC int | |
95 | xfs_check_agi_freecount( | |
96 | struct xfs_btree_cur *cur, | |
97 | struct xfs_agi *agi) | |
98 | { | |
99 | if (cur->bc_nlevels == 1) { | |
100 | xfs_inobt_rec_incore_t rec; | |
101 | int freecount = 0; | |
102 | int error; | |
103 | int i; | |
104 | ||
105 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); | |
106 | if (error) | |
107 | return error; | |
108 | ||
109 | do { | |
110 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
111 | if (error) | |
112 | return error; | |
113 | ||
114 | if (i) { | |
115 | freecount += rec.ir_freecount; | |
116 | error = xfs_btree_increment(cur, 0, &i); | |
117 | if (error) | |
118 | return error; | |
119 | } | |
120 | } while (i == 1); | |
121 | ||
122 | if (!XFS_FORCED_SHUTDOWN(cur->bc_mp)) | |
123 | ASSERT(freecount == be32_to_cpu(agi->agi_freecount)); | |
124 | } | |
125 | return 0; | |
126 | } | |
127 | #else | |
128 | #define xfs_check_agi_freecount(cur, agi) 0 | |
129 | #endif | |
130 | ||
131 | /* | |
e9d35108 DC |
132 | * Initialise a new set of inodes. When called without a transaction context |
133 | * (e.g. from recovery) we initiate a delayed write of the inode buffers rather | |
134 | * than logging them (which in a transaction context puts them into the AIL | |
135 | * for writeback rather than the xfsbufd queue). | |
56b2de80 | 136 | */ |
e9d35108 | 137 | int |
56b2de80 DC |
138 | xfs_ialloc_inode_init( |
139 | struct xfs_mount *mp, | |
140 | struct xfs_trans *tp, | |
e9d35108 | 141 | struct list_head *buffer_list, |
56b2de80 DC |
142 | xfs_agnumber_t agno, |
143 | xfs_agblock_t agbno, | |
144 | xfs_agblock_t length, | |
145 | unsigned int gen) | |
146 | { | |
147 | struct xfs_buf *fbuf; | |
148 | struct xfs_dinode *free; | |
149 | int blks_per_cluster, nbufs, ninodes; | |
150 | int version; | |
151 | int i, j; | |
152 | xfs_daddr_t d; | |
41ce5f36 | 153 | xfs_ino_t ino = 0; |
56b2de80 DC |
154 | |
155 | /* | |
156 | * Loop over the new block(s), filling in the inodes. | |
157 | * For small block sizes, manipulate the inodes in buffers | |
158 | * which are multiples of the blocks size. | |
159 | */ | |
160 | if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) { | |
161 | blks_per_cluster = 1; | |
162 | nbufs = length; | |
163 | ninodes = mp->m_sb.sb_inopblock; | |
164 | } else { | |
165 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) / | |
166 | mp->m_sb.sb_blocksize; | |
167 | nbufs = length / blks_per_cluster; | |
168 | ninodes = blks_per_cluster * mp->m_sb.sb_inopblock; | |
169 | } | |
170 | ||
171 | /* | |
e9d35108 DC |
172 | * Figure out what version number to use in the inodes we create. If |
173 | * the superblock version has caught up to the one that supports the new | |
174 | * inode format, then use the new inode version. Otherwise use the old | |
175 | * version so that old kernels will continue to be able to use the file | |
176 | * system. | |
41ce5f36 DC |
177 | * |
178 | * For v3 inodes, we also need to write the inode number into the inode, | |
179 | * so calculate the first inode number of the chunk here as | |
e9d35108 DC |
180 | * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not |
181 | * across multiple filesystem blocks (such as a cluster) and so cannot | |
182 | * be used in the cluster buffer loop below. | |
183 | * | |
184 | * Further, because we are writing the inode directly into the buffer | |
185 | * and calculating a CRC on the entire inode, we have ot log the entire | |
186 | * inode so that the entire range the CRC covers is present in the log. | |
187 | * That means for v3 inode we log the entire buffer rather than just the | |
188 | * inode cores. | |
56b2de80 | 189 | */ |
41ce5f36 DC |
190 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
191 | version = 3; | |
192 | ino = XFS_AGINO_TO_INO(mp, agno, | |
193 | XFS_OFFBNO_TO_AGINO(mp, agbno, 0)); | |
e9d35108 DC |
194 | |
195 | /* | |
196 | * log the initialisation that is about to take place as an | |
197 | * logical operation. This means the transaction does not | |
198 | * need to log the physical changes to the inode buffers as log | |
199 | * recovery will know what initialisation is actually needed. | |
200 | * Hence we only need to log the buffers as "ordered" buffers so | |
201 | * they track in the AIL as if they were physically logged. | |
202 | */ | |
203 | if (tp) | |
204 | xfs_icreate_log(tp, agno, agbno, XFS_IALLOC_INODES(mp), | |
205 | mp->m_sb.sb_inodesize, length, gen); | |
41ce5f36 | 206 | } else if (xfs_sb_version_hasnlink(&mp->m_sb)) |
56b2de80 DC |
207 | version = 2; |
208 | else | |
209 | version = 1; | |
210 | ||
211 | for (j = 0; j < nbufs; j++) { | |
212 | /* | |
213 | * Get the block. | |
214 | */ | |
215 | d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster)); | |
216 | fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, | |
217 | mp->m_bsize * blks_per_cluster, | |
a2ceac1f DC |
218 | XBF_UNMAPPED); |
219 | if (!fbuf) | |
220 | return ENOMEM; | |
e9d35108 DC |
221 | |
222 | /* Initialize the inode buffers and log them appropriately. */ | |
a2ceac1f | 223 | fbuf->b_ops = &xfs_inode_buf_ops; |
e9d35108 | 224 | xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length)); |
56b2de80 DC |
225 | for (i = 0; i < ninodes; i++) { |
226 | int ioffset = i << mp->m_sb.sb_inodelog; | |
41ce5f36 | 227 | uint isize = xfs_dinode_size(version); |
56b2de80 DC |
228 | |
229 | free = xfs_make_iptr(mp, fbuf, i); | |
230 | free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC); | |
231 | free->di_version = version; | |
232 | free->di_gen = cpu_to_be32(gen); | |
233 | free->di_next_unlinked = cpu_to_be32(NULLAGINO); | |
41ce5f36 DC |
234 | |
235 | if (version == 3) { | |
236 | free->di_ino = cpu_to_be64(ino); | |
237 | ino++; | |
238 | uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid); | |
239 | xfs_dinode_calc_crc(mp, free); | |
e9d35108 DC |
240 | } else if (tp) { |
241 | /* just log the inode core */ | |
242 | xfs_trans_log_buf(tp, fbuf, ioffset, | |
243 | ioffset + isize - 1); | |
41ce5f36 | 244 | } |
e9d35108 | 245 | } |
41ce5f36 | 246 | |
e9d35108 DC |
247 | if (tp) { |
248 | /* | |
249 | * Mark the buffer as an inode allocation buffer so it | |
250 | * sticks in AIL at the point of this allocation | |
251 | * transaction. This ensures the they are on disk before | |
252 | * the tail of the log can be moved past this | |
253 | * transaction (i.e. by preventing relogging from moving | |
254 | * it forward in the log). | |
255 | */ | |
256 | xfs_trans_inode_alloc_buf(tp, fbuf); | |
257 | if (version == 3) { | |
258 | /* | |
259 | * Mark the buffer as ordered so that they are | |
260 | * not physically logged in the transaction but | |
261 | * still tracked in the AIL as part of the | |
262 | * transaction and pin the log appropriately. | |
263 | */ | |
264 | xfs_trans_ordered_buf(tp, fbuf); | |
265 | xfs_trans_log_buf(tp, fbuf, 0, | |
266 | BBTOB(fbuf->b_length) - 1); | |
267 | } | |
268 | } else { | |
269 | fbuf->b_flags |= XBF_DONE; | |
270 | xfs_buf_delwri_queue(fbuf, buffer_list); | |
271 | xfs_buf_relse(fbuf); | |
56b2de80 | 272 | } |
56b2de80 | 273 | } |
a2ceac1f | 274 | return 0; |
56b2de80 DC |
275 | } |
276 | ||
2bd0ea18 NS |
277 | /* |
278 | * Allocate new inodes in the allocation group specified by agbp. | |
279 | * Return 0 for success, else error code. | |
280 | */ | |
281 | STATIC int /* error code or 0 */ | |
282 | xfs_ialloc_ag_alloc( | |
283 | xfs_trans_t *tp, /* transaction pointer */ | |
284 | xfs_buf_t *agbp, /* alloc group buffer */ | |
285 | int *alloc) | |
286 | { | |
287 | xfs_agi_t *agi; /* allocation group header */ | |
dfc130f3 | 288 | xfs_alloc_arg_t args; /* allocation argument structure */ |
dfc130f3 | 289 | xfs_btree_cur_t *cur; /* inode btree cursor */ |
5e656dbb | 290 | xfs_agnumber_t agno; |
2bd0ea18 | 291 | int error; |
56b2de80 | 292 | int i; |
2bd0ea18 NS |
293 | xfs_agino_t newino; /* new first inode's number */ |
294 | xfs_agino_t newlen; /* new number of inodes */ | |
2bd0ea18 | 295 | xfs_agino_t thisino; /* current inode number, for loop */ |
5e656dbb | 296 | int isaligned = 0; /* inode allocation at stripe unit */ |
2bd0ea18 | 297 | /* boundary */ |
56b2de80 | 298 | struct xfs_perag *pag; |
5000d01d | 299 | |
a2ceac1f | 300 | memset(&args, 0, sizeof(args)); |
2bd0ea18 NS |
301 | args.tp = tp; |
302 | args.mp = tp->t_mountp; | |
303 | ||
304 | /* | |
305 | * Locking will ensure that we don't have two callers in here | |
306 | * at one time. | |
307 | */ | |
308 | newlen = XFS_IALLOC_INODES(args.mp); | |
309 | if (args.mp->m_maxicount && | |
310 | args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount) | |
311 | return XFS_ERROR(ENOSPC); | |
312 | args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp); | |
313 | /* | |
5e656dbb BN |
314 | * First try to allocate inodes contiguous with the last-allocated |
315 | * chunk of inodes. If the filesystem is striped, this will fill | |
316 | * an entire stripe unit with inodes. | |
3439d03a | 317 | */ |
2bd0ea18 | 318 | agi = XFS_BUF_TO_AGI(agbp); |
5e656dbb | 319 | newino = be32_to_cpu(agi->agi_newino); |
56b2de80 | 320 | agno = be32_to_cpu(agi->agi_seqno); |
5e656dbb BN |
321 | args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + |
322 | XFS_IALLOC_BLOCKS(args.mp); | |
323 | if (likely(newino != NULLAGINO && | |
324 | (args.agbno < be32_to_cpu(agi->agi_length)))) { | |
56b2de80 | 325 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
5e656dbb | 326 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
5e656dbb BN |
327 | args.prod = 1; |
328 | ||
329 | /* | |
330 | * We need to take into account alignment here to ensure that | |
331 | * we don't modify the free list if we fail to have an exact | |
332 | * block. If we don't have an exact match, and every oher | |
333 | * attempt allocation attempt fails, we'll end up cancelling | |
334 | * a dirty transaction and shutting down. | |
335 | * | |
336 | * For an exact allocation, alignment must be 1, | |
337 | * however we need to take cluster alignment into account when | |
338 | * fixing up the freelist. Use the minalignslop field to | |
339 | * indicate that extra blocks might be required for alignment, | |
340 | * but not to use them in the actual exact allocation. | |
341 | */ | |
342 | args.alignment = 1; | |
343 | args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1; | |
344 | ||
345 | /* Allow space for the inode btree to split. */ | |
56b2de80 | 346 | args.minleft = args.mp->m_in_maxlevels - 1; |
5e656dbb BN |
347 | if ((error = xfs_alloc_vextent(&args))) |
348 | return error; | |
349 | } else | |
350 | args.fsbno = NULLFSBLOCK; | |
351 | ||
352 | if (unlikely(args.fsbno == NULLFSBLOCK)) { | |
353 | /* | |
354 | * Set the alignment for the allocation. | |
355 | * If stripe alignment is turned on then align at stripe unit | |
356 | * boundary. | |
357 | * If the cluster size is smaller than a filesystem block | |
358 | * then we're doing I/O for inodes in filesystem block size | |
359 | * pieces, so don't need alignment anyway. | |
360 | */ | |
361 | isaligned = 0; | |
362 | if (args.mp->m_sinoalign) { | |
363 | ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN)); | |
364 | args.alignment = args.mp->m_dalign; | |
365 | isaligned = 1; | |
366 | } else | |
367 | args.alignment = xfs_ialloc_cluster_alignment(&args); | |
368 | /* | |
369 | * Need to figure out where to allocate the inode blocks. | |
370 | * Ideally they should be spaced out through the a.g. | |
371 | * For now, just allocate blocks up front. | |
372 | */ | |
373 | args.agbno = be32_to_cpu(agi->agi_root); | |
56b2de80 | 374 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
5e656dbb BN |
375 | /* |
376 | * Allocate a fixed-size extent of inodes. | |
377 | */ | |
378 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
5e656dbb BN |
379 | args.prod = 1; |
380 | /* | |
381 | * Allow space for the inode btree to split. | |
382 | */ | |
56b2de80 | 383 | args.minleft = args.mp->m_in_maxlevels - 1; |
5e656dbb BN |
384 | if ((error = xfs_alloc_vextent(&args))) |
385 | return error; | |
386 | } | |
2bd0ea18 NS |
387 | |
388 | /* | |
389 | * If stripe alignment is turned on, then try again with cluster | |
390 | * alignment. | |
391 | */ | |
392 | if (isaligned && args.fsbno == NULLFSBLOCK) { | |
393 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
6e3140c7 | 394 | args.agbno = be32_to_cpu(agi->agi_root); |
56b2de80 | 395 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
5e656dbb | 396 | args.alignment = xfs_ialloc_cluster_alignment(&args); |
0e266570 | 397 | if ((error = xfs_alloc_vextent(&args))) |
dfc130f3 | 398 | return error; |
2bd0ea18 | 399 | } |
5000d01d | 400 | |
2bd0ea18 NS |
401 | if (args.fsbno == NULLFSBLOCK) { |
402 | *alloc = 0; | |
403 | return 0; | |
404 | } | |
405 | ASSERT(args.len == args.minlen); | |
a562a63b | 406 | |
5e656dbb | 407 | /* |
56b2de80 DC |
408 | * Stamp and write the inode buffers. |
409 | * | |
5e656dbb BN |
410 | * Seed the new inode cluster with a random generation number. This |
411 | * prevents short-term reuse of generation numbers if a chunk is | |
412 | * freed and then immediately reallocated. We use random numbers | |
413 | * rather than a linear progression to prevent the next generation | |
414 | * number from being easily guessable. | |
415 | */ | |
e9d35108 | 416 | error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno, |
49f693fa | 417 | args.len, prandom_u32()); |
56b2de80 | 418 | |
a2ceac1f DC |
419 | if (error) |
420 | return error; | |
56b2de80 DC |
421 | /* |
422 | * Convert the results. | |
423 | */ | |
424 | newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0); | |
5e656dbb BN |
425 | be32_add_cpu(&agi->agi_count, newlen); |
426 | be32_add_cpu(&agi->agi_freecount, newlen); | |
56b2de80 DC |
427 | pag = xfs_perag_get(args.mp, agno); |
428 | pag->pagi_freecount += newlen; | |
429 | xfs_perag_put(pag); | |
6e3140c7 | 430 | agi->agi_newino = cpu_to_be32(newino); |
56b2de80 | 431 | |
2bd0ea18 NS |
432 | /* |
433 | * Insert records describing the new inode chunk into the btree. | |
434 | */ | |
70eb7337 | 435 | cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno, XFS_BTNUM_INO); |
2bd0ea18 NS |
436 | for (thisino = newino; |
437 | thisino < newino + newlen; | |
438 | thisino += XFS_INODES_PER_CHUNK) { | |
56b2de80 DC |
439 | cur->bc_rec.i.ir_startino = thisino; |
440 | cur->bc_rec.i.ir_freecount = XFS_INODES_PER_CHUNK; | |
441 | cur->bc_rec.i.ir_free = XFS_INOBT_ALL_FREE; | |
442 | error = xfs_btree_lookup(cur, XFS_LOOKUP_EQ, &i); | |
443 | if (error) { | |
2bd0ea18 NS |
444 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
445 | return error; | |
446 | } | |
447 | ASSERT(i == 0); | |
56b2de80 DC |
448 | error = xfs_btree_insert(cur, &i); |
449 | if (error) { | |
2bd0ea18 NS |
450 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
451 | return error; | |
452 | } | |
453 | ASSERT(i == 1); | |
454 | } | |
455 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
456 | /* | |
457 | * Log allocation group header fields | |
458 | */ | |
459 | xfs_ialloc_log_agi(tp, agbp, | |
460 | XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); | |
461 | /* | |
462 | * Modify/log superblock values for inode count and inode free count. | |
463 | */ | |
464 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); | |
465 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); | |
466 | *alloc = 1; | |
467 | return 0; | |
468 | } | |
469 | ||
56b2de80 | 470 | STATIC xfs_agnumber_t |
321717ae NS |
471 | xfs_ialloc_next_ag( |
472 | xfs_mount_t *mp) | |
473 | { | |
474 | xfs_agnumber_t agno; | |
475 | ||
476 | spin_lock(&mp->m_agirotor_lock); | |
477 | agno = mp->m_agirotor; | |
a2ceac1f | 478 | if (++mp->m_agirotor >= mp->m_maxagi) |
321717ae NS |
479 | mp->m_agirotor = 0; |
480 | spin_unlock(&mp->m_agirotor_lock); | |
481 | ||
482 | return agno; | |
483 | } | |
484 | ||
2bd0ea18 NS |
485 | /* |
486 | * Select an allocation group to look for a free inode in, based on the parent | |
e6d77a21 | 487 | * inode and the mode. Return the allocation group buffer. |
2bd0ea18 | 488 | */ |
a2ceac1f | 489 | STATIC xfs_agnumber_t |
2bd0ea18 NS |
490 | xfs_ialloc_ag_select( |
491 | xfs_trans_t *tp, /* transaction pointer */ | |
492 | xfs_ino_t parent, /* parent directory inode number */ | |
a2ceac1f | 493 | umode_t mode, /* bits set to indicate file type */ |
2bd0ea18 NS |
494 | int okalloc) /* ok to allocate more space */ |
495 | { | |
2bd0ea18 NS |
496 | xfs_agnumber_t agcount; /* number of ag's in the filesystem */ |
497 | xfs_agnumber_t agno; /* current ag number */ | |
498 | int flags; /* alloc buffer locking flags */ | |
499 | xfs_extlen_t ineed; /* blocks needed for inode allocation */ | |
275ae71f | 500 | xfs_extlen_t longest = 0; /* longest extent available */ |
2bd0ea18 NS |
501 | xfs_mount_t *mp; /* mount point structure */ |
502 | int needspace; /* file mode implies space allocated */ | |
503 | xfs_perag_t *pag; /* per allocation group data */ | |
504 | xfs_agnumber_t pagno; /* parent (starting) ag number */ | |
a2ceac1f | 505 | int error; |
2bd0ea18 NS |
506 | |
507 | /* | |
508 | * Files of these types need at least one block if length > 0 | |
509 | * (and they won't fit in the inode, but that's hard to figure out). | |
510 | */ | |
511 | needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); | |
512 | mp = tp->t_mountp; | |
34317449 | 513 | agcount = mp->m_maxagi; |
2bd0ea18 | 514 | if (S_ISDIR(mode)) |
321717ae | 515 | pagno = xfs_ialloc_next_ag(mp); |
5ce1d1f7 | 516 | else { |
2bd0ea18 | 517 | pagno = XFS_INO_TO_AGNO(mp, parent); |
5ce1d1f7 NS |
518 | if (pagno >= agcount) |
519 | pagno = 0; | |
520 | } | |
a2ceac1f | 521 | |
2bd0ea18 | 522 | ASSERT(pagno < agcount); |
a2ceac1f | 523 | |
2bd0ea18 NS |
524 | /* |
525 | * Loop through allocation groups, looking for one with a little | |
526 | * free space in it. Note we don't look for free inodes, exactly. | |
527 | * Instead, we include whether there is a need to allocate inodes | |
5000d01d | 528 | * to mean that blocks must be allocated for them, |
2bd0ea18 NS |
529 | * if none are currently free. |
530 | */ | |
531 | agno = pagno; | |
532 | flags = XFS_ALLOC_FLAG_TRYLOCK; | |
533 | for (;;) { | |
56b2de80 | 534 | pag = xfs_perag_get(mp, agno); |
a2ceac1f DC |
535 | if (!pag->pagi_inodeok) { |
536 | xfs_ialloc_next_ag(mp); | |
537 | goto nextag; | |
538 | } | |
539 | ||
2bd0ea18 | 540 | if (!pag->pagi_init) { |
a2ceac1f DC |
541 | error = xfs_ialloc_pagi_init(mp, tp, agno); |
542 | if (error) | |
2bd0ea18 | 543 | goto nextag; |
a2ceac1f | 544 | } |
34317449 | 545 | |
a2ceac1f DC |
546 | if (pag->pagi_freecount) { |
547 | xfs_perag_put(pag); | |
548 | return agno; | |
34317449 NS |
549 | } |
550 | ||
a2ceac1f DC |
551 | if (!okalloc) |
552 | goto nextag; | |
553 | ||
554 | if (!pag->pagf_init) { | |
555 | error = xfs_alloc_pagf_init(mp, tp, agno, flags); | |
556 | if (error) | |
2bd0ea18 | 557 | goto nextag; |
2bd0ea18 | 558 | } |
a2ceac1f DC |
559 | |
560 | /* | |
561 | * Is there enough free space for the file plus a block of | |
562 | * inodes? (if we need to allocate some)? | |
563 | */ | |
564 | ineed = XFS_IALLOC_BLOCKS(mp); | |
565 | longest = pag->pagf_longest; | |
566 | if (!longest) | |
567 | longest = pag->pagf_flcount > 0; | |
568 | ||
569 | if (pag->pagf_freeblks >= needspace + ineed && | |
570 | longest >= ineed) { | |
571 | xfs_perag_put(pag); | |
572 | return agno; | |
2bd0ea18 | 573 | } |
5000d01d | 574 | nextag: |
56b2de80 | 575 | xfs_perag_put(pag); |
5000d01d | 576 | /* |
2bd0ea18 NS |
577 | * No point in iterating over the rest, if we're shutting |
578 | * down. | |
579 | */ | |
56b2de80 | 580 | if (XFS_FORCED_SHUTDOWN(mp)) |
a2ceac1f | 581 | return NULLAGNUMBER; |
2bd0ea18 | 582 | agno++; |
5ce1d1f7 | 583 | if (agno >= agcount) |
2bd0ea18 NS |
584 | agno = 0; |
585 | if (agno == pagno) { | |
56b2de80 | 586 | if (flags == 0) |
a2ceac1f | 587 | return NULLAGNUMBER; |
2bd0ea18 NS |
588 | flags = 0; |
589 | } | |
590 | } | |
591 | } | |
592 | ||
56b2de80 DC |
593 | /* |
594 | * Try to retrieve the next record to the left/right from the current one. | |
595 | */ | |
596 | STATIC int | |
597 | xfs_ialloc_next_rec( | |
598 | struct xfs_btree_cur *cur, | |
599 | xfs_inobt_rec_incore_t *rec, | |
600 | int *done, | |
601 | int left) | |
602 | { | |
603 | int error; | |
604 | int i; | |
605 | ||
606 | if (left) | |
607 | error = xfs_btree_decrement(cur, 0, &i); | |
608 | else | |
609 | error = xfs_btree_increment(cur, 0, &i); | |
610 | ||
611 | if (error) | |
612 | return error; | |
613 | *done = !i; | |
614 | if (i) { | |
615 | error = xfs_inobt_get_rec(cur, rec, &i); | |
616 | if (error) | |
617 | return error; | |
618 | XFS_WANT_CORRUPTED_RETURN(i == 1); | |
619 | } | |
620 | ||
621 | return 0; | |
622 | } | |
623 | ||
624 | STATIC int | |
625 | xfs_ialloc_get_rec( | |
626 | struct xfs_btree_cur *cur, | |
627 | xfs_agino_t agino, | |
628 | xfs_inobt_rec_incore_t *rec, | |
3439d03a | 629 | int *done) |
56b2de80 DC |
630 | { |
631 | int error; | |
632 | int i; | |
633 | ||
634 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i); | |
635 | if (error) | |
636 | return error; | |
637 | *done = !i; | |
638 | if (i) { | |
639 | error = xfs_inobt_get_rec(cur, rec, &i); | |
640 | if (error) | |
641 | return error; | |
642 | XFS_WANT_CORRUPTED_RETURN(i == 1); | |
643 | } | |
644 | ||
645 | return 0; | |
646 | } | |
647 | ||
5000d01d | 648 | /* |
a2ceac1f | 649 | * Allocate an inode. |
2bd0ea18 | 650 | * |
a2ceac1f DC |
651 | * The caller selected an AG for us, and made sure that free inodes are |
652 | * available. | |
2bd0ea18 | 653 | */ |
a2ceac1f DC |
654 | STATIC int |
655 | xfs_dialloc_ag( | |
656 | struct xfs_trans *tp, | |
657 | struct xfs_buf *agbp, | |
658 | xfs_ino_t parent, | |
659 | xfs_ino_t *inop) | |
2bd0ea18 | 660 | { |
a2ceac1f DC |
661 | struct xfs_mount *mp = tp->t_mountp; |
662 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); | |
663 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); | |
664 | xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); | |
665 | xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); | |
666 | struct xfs_perag *pag; | |
667 | struct xfs_btree_cur *cur, *tcur; | |
668 | struct xfs_inobt_rec_incore rec, trec; | |
669 | xfs_ino_t ino; | |
670 | int error; | |
671 | int offset; | |
672 | int i, j; | |
2bd0ea18 | 673 | |
56b2de80 DC |
674 | pag = xfs_perag_get(mp, agno); |
675 | ||
a2ceac1f DC |
676 | ASSERT(pag->pagi_init); |
677 | ASSERT(pag->pagi_inodeok); | |
678 | ASSERT(pag->pagi_freecount > 0); | |
679 | ||
56b2de80 | 680 | restart_pagno: |
70eb7337 | 681 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
2bd0ea18 NS |
682 | /* |
683 | * If pagino is 0 (this is the root inode allocation) use newino. | |
684 | * This must work because we've just allocated some. | |
685 | */ | |
686 | if (!pagino) | |
6e3140c7 | 687 | pagino = be32_to_cpu(agi->agi_newino); |
2bd0ea18 | 688 | |
56b2de80 DC |
689 | error = xfs_check_agi_freecount(cur, agi); |
690 | if (error) | |
691 | goto error0; | |
2bd0ea18 | 692 | |
2bd0ea18 | 693 | /* |
56b2de80 | 694 | * If in the same AG as the parent, try to get near the parent. |
2bd0ea18 NS |
695 | */ |
696 | if (pagno == agno) { | |
56b2de80 DC |
697 | int doneleft; /* done, to the left */ |
698 | int doneright; /* done, to the right */ | |
699 | int searchdistance = 10; | |
700 | ||
701 | error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i); | |
702 | if (error) | |
2bd0ea18 | 703 | goto error0; |
56b2de80 DC |
704 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
705 | ||
706 | error = xfs_inobt_get_rec(cur, &rec, &j); | |
707 | if (error) | |
708 | goto error0; | |
e6d77a21 | 709 | XFS_WANT_CORRUPTED_GOTO(j == 1, error0); |
56b2de80 DC |
710 | |
711 | if (rec.ir_freecount > 0) { | |
2bd0ea18 NS |
712 | /* |
713 | * Found a free inode in the same chunk | |
56b2de80 | 714 | * as the parent, done. |
2bd0ea18 | 715 | */ |
56b2de80 | 716 | goto alloc_inode; |
2bd0ea18 | 717 | } |
56b2de80 DC |
718 | |
719 | ||
720 | /* | |
721 | * In the same AG as parent, but parent's chunk is full. | |
722 | */ | |
723 | ||
724 | /* duplicate the cursor, search left & right simultaneously */ | |
725 | error = xfs_btree_dup_cursor(cur, &tcur); | |
726 | if (error) | |
727 | goto error0; | |
728 | ||
2bd0ea18 | 729 | /* |
56b2de80 | 730 | * Skip to last blocks looked up if same parent inode. |
2bd0ea18 | 731 | */ |
56b2de80 DC |
732 | if (pagino != NULLAGINO && |
733 | pag->pagl_pagino == pagino && | |
734 | pag->pagl_leftrec != NULLAGINO && | |
735 | pag->pagl_rightrec != NULLAGINO) { | |
736 | error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec, | |
3439d03a | 737 | &trec, &doneleft); |
56b2de80 DC |
738 | if (error) |
739 | goto error1; | |
2bd0ea18 | 740 | |
56b2de80 | 741 | error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec, |
3439d03a | 742 | &rec, &doneright); |
2bd0ea18 | 743 | if (error) |
2bd0ea18 | 744 | goto error1; |
56b2de80 DC |
745 | } else { |
746 | /* search left with tcur, back up 1 record */ | |
747 | error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1); | |
748 | if (error) | |
2bd0ea18 | 749 | goto error1; |
2bd0ea18 | 750 | |
56b2de80 DC |
751 | /* search right with cur, go forward 1 record. */ |
752 | error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0); | |
753 | if (error) | |
754 | goto error1; | |
755 | } | |
756 | ||
757 | /* | |
758 | * Loop until we find an inode chunk with a free inode. | |
759 | */ | |
760 | while (!doneleft || !doneright) { | |
761 | int useleft; /* using left inode chunk this time */ | |
762 | ||
763 | if (!--searchdistance) { | |
2bd0ea18 | 764 | /* |
56b2de80 DC |
765 | * Not in range - save last search |
766 | * location and allocate a new inode | |
2bd0ea18 | 767 | */ |
56b2de80 DC |
768 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
769 | pag->pagl_leftrec = trec.ir_startino; | |
770 | pag->pagl_rightrec = rec.ir_startino; | |
771 | pag->pagl_pagino = pagino; | |
772 | goto newino; | |
2bd0ea18 | 773 | } |
56b2de80 DC |
774 | |
775 | /* figure out the closer block if both are valid. */ | |
776 | if (!doneleft && !doneright) { | |
777 | useleft = pagino - | |
778 | (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) < | |
779 | rec.ir_startino - pagino; | |
780 | } else { | |
781 | useleft = !doneleft; | |
782 | } | |
783 | ||
784 | /* free inodes to the left? */ | |
785 | if (useleft && trec.ir_freecount) { | |
786 | rec = trec; | |
787 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
788 | cur = tcur; | |
789 | ||
790 | pag->pagl_leftrec = trec.ir_startino; | |
791 | pag->pagl_rightrec = rec.ir_startino; | |
792 | pag->pagl_pagino = pagino; | |
793 | goto alloc_inode; | |
794 | } | |
795 | ||
796 | /* free inodes to the right? */ | |
797 | if (!useleft && rec.ir_freecount) { | |
798 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); | |
799 | ||
800 | pag->pagl_leftrec = trec.ir_startino; | |
801 | pag->pagl_rightrec = rec.ir_startino; | |
802 | pag->pagl_pagino = pagino; | |
803 | goto alloc_inode; | |
804 | } | |
805 | ||
806 | /* get next record to check */ | |
807 | if (useleft) { | |
808 | error = xfs_ialloc_next_rec(tcur, &trec, | |
809 | &doneleft, 1); | |
810 | } else { | |
811 | error = xfs_ialloc_next_rec(cur, &rec, | |
812 | &doneright, 0); | |
813 | } | |
814 | if (error) | |
815 | goto error1; | |
2bd0ea18 | 816 | } |
56b2de80 DC |
817 | |
818 | /* | |
819 | * We've reached the end of the btree. because | |
820 | * we are only searching a small chunk of the | |
821 | * btree each search, there is obviously free | |
822 | * inodes closer to the parent inode than we | |
823 | * are now. restart the search again. | |
824 | */ | |
825 | pag->pagl_pagino = NULLAGINO; | |
826 | pag->pagl_leftrec = NULLAGINO; | |
827 | pag->pagl_rightrec = NULLAGINO; | |
828 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); | |
829 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
830 | goto restart_pagno; | |
2bd0ea18 | 831 | } |
56b2de80 | 832 | |
2bd0ea18 | 833 | /* |
56b2de80 | 834 | * In a different AG from the parent. |
2bd0ea18 NS |
835 | * See if the most recently allocated block has any free. |
836 | */ | |
56b2de80 | 837 | newino: |
a2ceac1f | 838 | if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { |
56b2de80 DC |
839 | error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), |
840 | XFS_LOOKUP_EQ, &i); | |
841 | if (error) | |
2bd0ea18 | 842 | goto error0; |
56b2de80 DC |
843 | |
844 | if (i == 1) { | |
845 | error = xfs_inobt_get_rec(cur, &rec, &j); | |
2bd0ea18 NS |
846 | if (error) |
847 | goto error0; | |
56b2de80 DC |
848 | |
849 | if (j == 1 && rec.ir_freecount > 0) { | |
850 | /* | |
851 | * The last chunk allocated in the group | |
852 | * still has a free inode. | |
853 | */ | |
854 | goto alloc_inode; | |
2bd0ea18 NS |
855 | } |
856 | } | |
857 | } | |
56b2de80 DC |
858 | |
859 | /* | |
860 | * None left in the last group, search the whole AG | |
861 | */ | |
862 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); | |
863 | if (error) | |
864 | goto error0; | |
865 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
866 | ||
867 | for (;;) { | |
868 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
869 | if (error) | |
870 | goto error0; | |
871 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
872 | if (rec.ir_freecount > 0) | |
873 | break; | |
874 | error = xfs_btree_increment(cur, 0, &i); | |
875 | if (error) | |
876 | goto error0; | |
877 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
878 | } | |
879 | ||
880 | alloc_inode: | |
a2ceac1f | 881 | offset = xfs_lowbit64(rec.ir_free); |
2bd0ea18 NS |
882 | ASSERT(offset >= 0); |
883 | ASSERT(offset < XFS_INODES_PER_CHUNK); | |
884 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % | |
885 | XFS_INODES_PER_CHUNK) == 0); | |
886 | ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); | |
56b2de80 | 887 | rec.ir_free &= ~XFS_INOBT_MASK(offset); |
2bd0ea18 | 888 | rec.ir_freecount--; |
56b2de80 DC |
889 | error = xfs_inobt_update(cur, &rec); |
890 | if (error) | |
2bd0ea18 | 891 | goto error0; |
5e656dbb | 892 | be32_add_cpu(&agi->agi_freecount, -1); |
2bd0ea18 | 893 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
56b2de80 DC |
894 | pag->pagi_freecount--; |
895 | ||
896 | error = xfs_check_agi_freecount(cur, agi); | |
897 | if (error) | |
898 | goto error0; | |
2bd0ea18 | 899 | |
2bd0ea18 NS |
900 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
901 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); | |
56b2de80 | 902 | xfs_perag_put(pag); |
2bd0ea18 NS |
903 | *inop = ino; |
904 | return 0; | |
905 | error1: | |
906 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); | |
907 | error0: | |
908 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
56b2de80 | 909 | xfs_perag_put(pag); |
2bd0ea18 NS |
910 | return error; |
911 | } | |
912 | ||
a2ceac1f DC |
913 | /* |
914 | * Allocate an inode on disk. | |
915 | * | |
916 | * Mode is used to tell whether the new inode will need space, and whether it | |
917 | * is a directory. | |
918 | * | |
919 | * This function is designed to be called twice if it has to do an allocation | |
920 | * to make more free inodes. On the first call, *IO_agbp should be set to NULL. | |
921 | * If an inode is available without having to performn an allocation, an inode | |
922 | * number is returned. In this case, *IO_agbp is set to NULL. If an allocation | |
923 | * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp. | |
924 | * The caller should then commit the current transaction, allocate a | |
925 | * new transaction, and call xfs_dialloc() again, passing in the previous value | |
926 | * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI | |
927 | * buffer is locked across the two calls, the second call is guaranteed to have | |
928 | * a free inode available. | |
929 | * | |
930 | * Once we successfully pick an inode its number is returned and the on-disk | |
931 | * data structures are updated. The inode itself is not read in, since doing so | |
932 | * would break ordering constraints with xfs_reclaim. | |
933 | */ | |
934 | int | |
935 | xfs_dialloc( | |
936 | struct xfs_trans *tp, | |
937 | xfs_ino_t parent, | |
938 | umode_t mode, | |
939 | int okalloc, | |
940 | struct xfs_buf **IO_agbp, | |
941 | xfs_ino_t *inop) | |
942 | { | |
943 | struct xfs_mount *mp = tp->t_mountp; | |
944 | struct xfs_buf *agbp; | |
945 | xfs_agnumber_t agno; | |
946 | int error; | |
947 | int ialloced; | |
948 | int noroom = 0; | |
949 | xfs_agnumber_t start_agno; | |
950 | struct xfs_perag *pag; | |
951 | ||
952 | if (*IO_agbp) { | |
953 | /* | |
954 | * If the caller passes in a pointer to the AGI buffer, | |
955 | * continue where we left off before. In this case, we | |
956 | * know that the allocation group has free inodes. | |
957 | */ | |
958 | agbp = *IO_agbp; | |
959 | goto out_alloc; | |
960 | } | |
961 | ||
962 | /* | |
963 | * We do not have an agbp, so select an initial allocation | |
964 | * group for inode allocation. | |
965 | */ | |
966 | start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc); | |
967 | if (start_agno == NULLAGNUMBER) { | |
968 | *inop = NULLFSINO; | |
969 | return 0; | |
970 | } | |
971 | ||
972 | /* | |
973 | * If we have already hit the ceiling of inode blocks then clear | |
974 | * okalloc so we scan all available agi structures for a free | |
975 | * inode. | |
976 | */ | |
977 | if (mp->m_maxicount && | |
978 | mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) { | |
979 | noroom = 1; | |
980 | okalloc = 0; | |
981 | } | |
982 | ||
983 | /* | |
984 | * Loop until we find an allocation group that either has free inodes | |
985 | * or in which we can allocate some inodes. Iterate through the | |
986 | * allocation groups upward, wrapping at the end. | |
987 | */ | |
988 | agno = start_agno; | |
989 | for (;;) { | |
990 | pag = xfs_perag_get(mp, agno); | |
991 | if (!pag->pagi_inodeok) { | |
992 | xfs_ialloc_next_ag(mp); | |
993 | goto nextag; | |
994 | } | |
995 | ||
996 | if (!pag->pagi_init) { | |
997 | error = xfs_ialloc_pagi_init(mp, tp, agno); | |
998 | if (error) | |
999 | goto out_error; | |
1000 | } | |
1001 | ||
1002 | /* | |
1003 | * Do a first racy fast path check if this AG is usable. | |
1004 | */ | |
1005 | if (!pag->pagi_freecount && !okalloc) | |
1006 | goto nextag; | |
1007 | ||
1008 | /* | |
1009 | * Then read in the AGI buffer and recheck with the AGI buffer | |
1010 | * lock held. | |
1011 | */ | |
1012 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); | |
1013 | if (error) | |
1014 | goto out_error; | |
1015 | ||
1016 | if (pag->pagi_freecount) { | |
1017 | xfs_perag_put(pag); | |
1018 | goto out_alloc; | |
1019 | } | |
1020 | ||
1021 | if (!okalloc) | |
1022 | goto nextag_relse_buffer; | |
1023 | ||
1024 | ||
1025 | error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced); | |
1026 | if (error) { | |
1027 | xfs_trans_brelse(tp, agbp); | |
1028 | ||
1029 | if (error != ENOSPC) | |
1030 | goto out_error; | |
1031 | ||
1032 | xfs_perag_put(pag); | |
1033 | *inop = NULLFSINO; | |
1034 | return 0; | |
1035 | } | |
1036 | ||
1037 | if (ialloced) { | |
1038 | /* | |
1039 | * We successfully allocated some inodes, return | |
1040 | * the current context to the caller so that it | |
1041 | * can commit the current transaction and call | |
1042 | * us again where we left off. | |
1043 | */ | |
1044 | ASSERT(pag->pagi_freecount > 0); | |
1045 | xfs_perag_put(pag); | |
1046 | ||
1047 | *IO_agbp = agbp; | |
1048 | *inop = NULLFSINO; | |
1049 | return 0; | |
1050 | } | |
1051 | ||
1052 | nextag_relse_buffer: | |
1053 | xfs_trans_brelse(tp, agbp); | |
1054 | nextag: | |
1055 | xfs_perag_put(pag); | |
1056 | if (++agno == mp->m_sb.sb_agcount) | |
1057 | agno = 0; | |
1058 | if (agno == start_agno) { | |
1059 | *inop = NULLFSINO; | |
1060 | return noroom ? ENOSPC : 0; | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | out_alloc: | |
1065 | *IO_agbp = NULL; | |
1066 | return xfs_dialloc_ag(tp, agbp, parent, inop); | |
1067 | out_error: | |
1068 | xfs_perag_put(pag); | |
1069 | return XFS_ERROR(error); | |
1070 | } | |
1071 | ||
3439d03a DC |
1072 | /* |
1073 | * Free disk inode. Carefully avoids touching the incore inode, all | |
1074 | * manipulations incore are the caller's responsibility. | |
1075 | * The on-disk inode is not changed by this operation, only the | |
1076 | * btree (free inode mask) is changed. | |
1077 | */ | |
1078 | int | |
1079 | xfs_difree( | |
1080 | xfs_trans_t *tp, /* transaction pointer */ | |
1081 | xfs_ino_t inode, /* inode to be freed */ | |
1082 | xfs_bmap_free_t *flist, /* extents to free */ | |
66260204 | 1083 | int *deleted, /* set if inode cluster was deleted */ |
3439d03a DC |
1084 | xfs_ino_t *first_ino) /* first inode in deleted cluster */ |
1085 | { | |
1086 | /* REFERENCED */ | |
1087 | xfs_agblock_t agbno; /* block number containing inode */ | |
1088 | xfs_buf_t *agbp; /* buffer containing allocation group header */ | |
1089 | xfs_agino_t agino; /* inode number relative to allocation group */ | |
1090 | xfs_agnumber_t agno; /* allocation group number */ | |
1091 | xfs_agi_t *agi; /* allocation group header */ | |
1092 | xfs_btree_cur_t *cur; /* inode btree cursor */ | |
1093 | int error; /* error return value */ | |
1094 | int i; /* result code */ | |
1095 | int ilen; /* inodes in an inode cluster */ | |
1096 | xfs_mount_t *mp; /* mount structure for filesystem */ | |
1097 | int off; /* offset of inode in inode chunk */ | |
1098 | xfs_inobt_rec_incore_t rec; /* btree record */ | |
1099 | struct xfs_perag *pag; | |
1100 | ||
1101 | mp = tp->t_mountp; | |
1102 | ||
1103 | /* | |
1104 | * Break up inode number into its components. | |
1105 | */ | |
1106 | agno = XFS_INO_TO_AGNO(mp, inode); | |
1107 | if (agno >= mp->m_sb.sb_agcount) { | |
1108 | xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).", | |
1109 | __func__, agno, mp->m_sb.sb_agcount); | |
1110 | ASSERT(0); | |
1111 | return XFS_ERROR(EINVAL); | |
1112 | } | |
1113 | agino = XFS_INO_TO_AGINO(mp, inode); | |
1114 | if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) { | |
1115 | xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).", | |
1116 | __func__, (unsigned long long)inode, | |
1117 | (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino)); | |
1118 | ASSERT(0); | |
1119 | return XFS_ERROR(EINVAL); | |
1120 | } | |
1121 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); | |
1122 | if (agbno >= mp->m_sb.sb_agblocks) { | |
1123 | xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).", | |
1124 | __func__, agbno, mp->m_sb.sb_agblocks); | |
1125 | ASSERT(0); | |
1126 | return XFS_ERROR(EINVAL); | |
1127 | } | |
1128 | /* | |
1129 | * Get the allocation group header. | |
1130 | */ | |
1131 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); | |
1132 | if (error) { | |
1133 | xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.", | |
1134 | __func__, error); | |
1135 | return error; | |
1136 | } | |
1137 | agi = XFS_BUF_TO_AGI(agbp); | |
1138 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); | |
1139 | ASSERT(agbno < be32_to_cpu(agi->agi_length)); | |
1140 | /* | |
1141 | * Initialize the cursor. | |
1142 | */ | |
70eb7337 | 1143 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
3439d03a DC |
1144 | |
1145 | error = xfs_check_agi_freecount(cur, agi); | |
1146 | if (error) | |
1147 | goto error0; | |
1148 | ||
1149 | /* | |
1150 | * Look for the entry describing this inode. | |
1151 | */ | |
1152 | if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) { | |
1153 | xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.", | |
1154 | __func__, error); | |
1155 | goto error0; | |
1156 | } | |
1157 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
1158 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
1159 | if (error) { | |
1160 | xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.", | |
1161 | __func__, error); | |
1162 | goto error0; | |
1163 | } | |
1164 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
1165 | /* | |
1166 | * Get the offset in the inode chunk. | |
1167 | */ | |
1168 | off = agino - rec.ir_startino; | |
1169 | ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); | |
1170 | ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); | |
1171 | /* | |
1172 | * Mark the inode free & increment the count. | |
1173 | */ | |
1174 | rec.ir_free |= XFS_INOBT_MASK(off); | |
1175 | rec.ir_freecount++; | |
1176 | ||
1177 | /* | |
1178 | * When an inode cluster is free, it becomes eligible for removal | |
1179 | */ | |
1180 | if (!(mp->m_flags & XFS_MOUNT_IKEEP) && | |
1181 | (rec.ir_freecount == XFS_IALLOC_INODES(mp))) { | |
1182 | ||
66260204 | 1183 | *deleted = 1; |
3439d03a DC |
1184 | *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino); |
1185 | ||
1186 | /* | |
1187 | * Remove the inode cluster from the AGI B+Tree, adjust the | |
1188 | * AGI and Superblock inode counts, and mark the disk space | |
1189 | * to be freed when the transaction is committed. | |
1190 | */ | |
1191 | ilen = XFS_IALLOC_INODES(mp); | |
1192 | be32_add_cpu(&agi->agi_count, -ilen); | |
1193 | be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); | |
1194 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); | |
1195 | pag = xfs_perag_get(mp, agno); | |
1196 | pag->pagi_freecount -= ilen - 1; | |
1197 | xfs_perag_put(pag); | |
1198 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); | |
1199 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); | |
1200 | ||
1201 | if ((error = xfs_btree_delete(cur, &i))) { | |
1202 | xfs_warn(mp, "%s: xfs_btree_delete returned error %d.", | |
1203 | __func__, error); | |
1204 | goto error0; | |
1205 | } | |
1206 | ||
1207 | xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, | |
1208 | agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)), | |
1209 | XFS_IALLOC_BLOCKS(mp), flist, mp); | |
1210 | } else { | |
66260204 | 1211 | *deleted = 0; |
3439d03a DC |
1212 | |
1213 | error = xfs_inobt_update(cur, &rec); | |
1214 | if (error) { | |
1215 | xfs_warn(mp, "%s: xfs_inobt_update returned error %d.", | |
1216 | __func__, error); | |
1217 | goto error0; | |
1218 | } | |
1219 | ||
1220 | /* | |
1221 | * Change the inode free counts and log the ag/sb changes. | |
1222 | */ | |
1223 | be32_add_cpu(&agi->agi_freecount, 1); | |
1224 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); | |
1225 | pag = xfs_perag_get(mp, agno); | |
1226 | pag->pagi_freecount++; | |
1227 | xfs_perag_put(pag); | |
1228 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); | |
1229 | } | |
1230 | ||
1231 | error = xfs_check_agi_freecount(cur, agi); | |
1232 | if (error) | |
1233 | goto error0; | |
1234 | ||
1235 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
1236 | return 0; | |
1237 | ||
1238 | error0: | |
1239 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
1240 | return error; | |
1241 | } | |
1242 | ||
56b2de80 DC |
1243 | STATIC int |
1244 | xfs_imap_lookup( | |
1245 | struct xfs_mount *mp, | |
1246 | struct xfs_trans *tp, | |
1247 | xfs_agnumber_t agno, | |
1248 | xfs_agino_t agino, | |
1249 | xfs_agblock_t agbno, | |
1250 | xfs_agblock_t *chunk_agbno, | |
1251 | xfs_agblock_t *offset_agbno, | |
1252 | int flags) | |
1253 | { | |
1254 | struct xfs_inobt_rec_incore rec; | |
1255 | struct xfs_btree_cur *cur; | |
1256 | struct xfs_buf *agbp; | |
1257 | int error; | |
1258 | int i; | |
1259 | ||
1260 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); | |
1261 | if (error) { | |
a2ceac1f DC |
1262 | xfs_alert(mp, |
1263 | "%s: xfs_ialloc_read_agi() returned error %d, agno %d", | |
1264 | __func__, error, agno); | |
56b2de80 DC |
1265 | return error; |
1266 | } | |
1267 | ||
1268 | /* | |
1269 | * Lookup the inode record for the given agino. If the record cannot be | |
1270 | * found, then it's an invalid inode number and we should abort. Once | |
1271 | * we have a record, we need to ensure it contains the inode number | |
1272 | * we are looking up. | |
1273 | */ | |
70eb7337 | 1274 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
56b2de80 DC |
1275 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i); |
1276 | if (!error) { | |
1277 | if (i) | |
1278 | error = xfs_inobt_get_rec(cur, &rec, &i); | |
1279 | if (!error && i == 0) | |
1280 | error = EINVAL; | |
1281 | } | |
1282 | ||
1283 | xfs_trans_brelse(tp, agbp); | |
1284 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
1285 | if (error) | |
1286 | return error; | |
1287 | ||
1288 | /* check that the returned record contains the required inode */ | |
1289 | if (rec.ir_startino > agino || | |
1290 | rec.ir_startino + XFS_IALLOC_INODES(mp) <= agino) | |
1291 | return EINVAL; | |
1292 | ||
1293 | /* for untrusted inodes check it is allocated first */ | |
1294 | if ((flags & XFS_IGET_UNTRUSTED) && | |
1295 | (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))) | |
1296 | return EINVAL; | |
1297 | ||
1298 | *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino); | |
1299 | *offset_agbno = agbno - *chunk_agbno; | |
1300 | return 0; | |
1301 | } | |
2bd0ea18 NS |
1302 | |
1303 | /* | |
56b2de80 | 1304 | * Return the location of the inode in imap, for mapping it into a buffer. |
2bd0ea18 | 1305 | */ |
2bd0ea18 | 1306 | int |
56b2de80 DC |
1307 | xfs_imap( |
1308 | xfs_mount_t *mp, /* file system mount structure */ | |
1309 | xfs_trans_t *tp, /* transaction pointer */ | |
2bd0ea18 | 1310 | xfs_ino_t ino, /* inode to locate */ |
56b2de80 DC |
1311 | struct xfs_imap *imap, /* location map structure */ |
1312 | uint flags) /* flags for inode btree lookup */ | |
2bd0ea18 NS |
1313 | { |
1314 | xfs_agblock_t agbno; /* block number of inode in the alloc group */ | |
2bd0ea18 NS |
1315 | xfs_agino_t agino; /* inode number within alloc group */ |
1316 | xfs_agnumber_t agno; /* allocation group number */ | |
1317 | int blks_per_cluster; /* num blocks per inode cluster */ | |
1318 | xfs_agblock_t chunk_agbno; /* first block in inode chunk */ | |
2bd0ea18 | 1319 | xfs_agblock_t cluster_agbno; /* first block in inode cluster */ |
2bd0ea18 | 1320 | int error; /* error code */ |
dfc130f3 | 1321 | int offset; /* index of inode in its buffer */ |
6bddecbc | 1322 | xfs_agblock_t offset_agbno; /* blks from chunk start to inode */ |
2bd0ea18 NS |
1323 | |
1324 | ASSERT(ino != NULLFSINO); | |
56b2de80 | 1325 | |
2bd0ea18 NS |
1326 | /* |
1327 | * Split up the inode number into its parts. | |
1328 | */ | |
1329 | agno = XFS_INO_TO_AGNO(mp, ino); | |
1330 | agino = XFS_INO_TO_AGINO(mp, ino); | |
1331 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); | |
1332 | if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || | |
63518810 NS |
1333 | ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
1334 | #ifdef DEBUG | |
56b2de80 DC |
1335 | /* |
1336 | * Don't output diagnostic information for untrusted inodes | |
1337 | * as they can be invalid without implying corruption. | |
1338 | */ | |
1339 | if (flags & XFS_IGET_UNTRUSTED) | |
5e656dbb | 1340 | return XFS_ERROR(EINVAL); |
5000d01d | 1341 | if (agno >= mp->m_sb.sb_agcount) { |
a2ceac1f DC |
1342 | xfs_alert(mp, |
1343 | "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)", | |
1344 | __func__, agno, mp->m_sb.sb_agcount); | |
63518810 NS |
1345 | } |
1346 | if (agbno >= mp->m_sb.sb_agblocks) { | |
a2ceac1f DC |
1347 | xfs_alert(mp, |
1348 | "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)", | |
1349 | __func__, (unsigned long long)agbno, | |
1350 | (unsigned long)mp->m_sb.sb_agblocks); | |
63518810 NS |
1351 | } |
1352 | if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) { | |
a2ceac1f DC |
1353 | xfs_alert(mp, |
1354 | "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)", | |
1355 | __func__, ino, | |
1356 | XFS_AGINO_TO_INO(mp, agno, agino)); | |
63518810 | 1357 | } |
5e656dbb | 1358 | xfs_stack_trace(); |
63518810 | 1359 | #endif /* DEBUG */ |
2bd0ea18 | 1360 | return XFS_ERROR(EINVAL); |
63518810 | 1361 | } |
56b2de80 | 1362 | |
2bd0ea18 | 1363 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog; |
56b2de80 DC |
1364 | |
1365 | /* | |
1366 | * For bulkstat and handle lookups, we have an untrusted inode number | |
1367 | * that we have to verify is valid. We cannot do this just by reading | |
1368 | * the inode buffer as it may have been unlinked and removed leaving | |
1369 | * inodes in stale state on disk. Hence we have to do a btree lookup | |
1370 | * in all cases where an untrusted inode number is passed. | |
1371 | */ | |
1372 | if (flags & XFS_IGET_UNTRUSTED) { | |
1373 | error = xfs_imap_lookup(mp, tp, agno, agino, agbno, | |
1374 | &chunk_agbno, &offset_agbno, flags); | |
1375 | if (error) | |
1376 | return error; | |
1377 | goto out_map; | |
1378 | } | |
1379 | ||
1380 | /* | |
1381 | * If the inode cluster size is the same as the blocksize or | |
1382 | * smaller we get to the buffer by simple arithmetics. | |
1383 | */ | |
1384 | if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) { | |
2bd0ea18 NS |
1385 | offset = XFS_INO_TO_OFFSET(mp, ino); |
1386 | ASSERT(offset < mp->m_sb.sb_inopblock); | |
56b2de80 DC |
1387 | |
1388 | imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno); | |
1389 | imap->im_len = XFS_FSB_TO_BB(mp, 1); | |
1390 | imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); | |
2bd0ea18 NS |
1391 | return 0; |
1392 | } | |
56b2de80 DC |
1393 | |
1394 | /* | |
1395 | * If the inode chunks are aligned then use simple maths to | |
1396 | * find the location. Otherwise we have to do a btree | |
1397 | * lookup to find the location. | |
1398 | */ | |
2bd0ea18 NS |
1399 | if (mp->m_inoalign_mask) { |
1400 | offset_agbno = agbno & mp->m_inoalign_mask; | |
1401 | chunk_agbno = agbno - offset_agbno; | |
1402 | } else { | |
56b2de80 DC |
1403 | error = xfs_imap_lookup(mp, tp, agno, agino, agbno, |
1404 | &chunk_agbno, &offset_agbno, flags); | |
2bd0ea18 NS |
1405 | if (error) |
1406 | return error; | |
2bd0ea18 | 1407 | } |
56b2de80 DC |
1408 | |
1409 | out_map: | |
2bd0ea18 NS |
1410 | ASSERT(agbno >= chunk_agbno); |
1411 | cluster_agbno = chunk_agbno + | |
1412 | ((offset_agbno / blks_per_cluster) * blks_per_cluster); | |
1413 | offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + | |
1414 | XFS_INO_TO_OFFSET(mp, ino); | |
56b2de80 DC |
1415 | |
1416 | imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno); | |
1417 | imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster); | |
1418 | imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); | |
1419 | ||
1420 | /* | |
1421 | * If the inode number maps to a block outside the bounds | |
1422 | * of the file system then return NULL rather than calling | |
1423 | * read_buf and panicing when we get an error from the | |
1424 | * driver. | |
1425 | */ | |
1426 | if ((imap->im_blkno + imap->im_len) > | |
1427 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { | |
a2ceac1f DC |
1428 | xfs_alert(mp, |
1429 | "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)", | |
1430 | __func__, (unsigned long long) imap->im_blkno, | |
56b2de80 DC |
1431 | (unsigned long long) imap->im_len, |
1432 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); | |
1433 | return XFS_ERROR(EINVAL); | |
1434 | } | |
2bd0ea18 | 1435 | return 0; |
2bd0ea18 NS |
1436 | } |
1437 | ||
1438 | /* | |
1439 | * Compute and fill in value of m_in_maxlevels. | |
1440 | */ | |
1441 | void | |
1442 | xfs_ialloc_compute_maxlevels( | |
1443 | xfs_mount_t *mp) /* file system mount structure */ | |
1444 | { | |
1445 | int level; | |
1446 | uint maxblocks; | |
1447 | uint maxleafents; | |
1448 | int minleafrecs; | |
1449 | int minnoderecs; | |
1450 | ||
1451 | maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >> | |
1452 | XFS_INODES_PER_CHUNK_LOG; | |
1453 | minleafrecs = mp->m_alloc_mnr[0]; | |
1454 | minnoderecs = mp->m_alloc_mnr[1]; | |
1455 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; | |
1456 | for (level = 1; maxblocks > 1; level++) | |
1457 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; | |
1458 | mp->m_in_maxlevels = level; | |
1459 | } | |
1460 | ||
1461 | /* | |
1462 | * Log specified fields for the ag hdr (inode section) | |
1463 | */ | |
1464 | void | |
1465 | xfs_ialloc_log_agi( | |
1466 | xfs_trans_t *tp, /* transaction pointer */ | |
1467 | xfs_buf_t *bp, /* allocation group header buffer */ | |
1468 | int fields) /* bitmask of fields to log */ | |
1469 | { | |
1470 | int first; /* first byte number */ | |
1471 | int last; /* last byte number */ | |
1472 | static const short offsets[] = { /* field starting offsets */ | |
1473 | /* keep in sync with bit definitions */ | |
1474 | offsetof(xfs_agi_t, agi_magicnum), | |
1475 | offsetof(xfs_agi_t, agi_versionnum), | |
1476 | offsetof(xfs_agi_t, agi_seqno), | |
1477 | offsetof(xfs_agi_t, agi_length), | |
1478 | offsetof(xfs_agi_t, agi_count), | |
1479 | offsetof(xfs_agi_t, agi_root), | |
1480 | offsetof(xfs_agi_t, agi_level), | |
1481 | offsetof(xfs_agi_t, agi_freecount), | |
1482 | offsetof(xfs_agi_t, agi_newino), | |
1483 | offsetof(xfs_agi_t, agi_dirino), | |
1484 | offsetof(xfs_agi_t, agi_unlinked), | |
c0a4c227 BF |
1485 | offsetof(xfs_agi_t, agi_free_root), |
1486 | offsetof(xfs_agi_t, agi_free_level), | |
2bd0ea18 NS |
1487 | sizeof(xfs_agi_t) |
1488 | }; | |
1489 | #ifdef DEBUG | |
1490 | xfs_agi_t *agi; /* allocation group header */ | |
1491 | ||
1492 | agi = XFS_BUF_TO_AGI(bp); | |
a2ceac1f | 1493 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); |
2bd0ea18 NS |
1494 | #endif |
1495 | /* | |
c0a4c227 BF |
1496 | * The growth of the agi buffer over time now requires that we interpret |
1497 | * the buffer as two logical regions delineated at the end of the unlinked | |
1498 | * list. This is due to the size of the hash table and its location in the | |
1499 | * middle of the agi. | |
1500 | * | |
1501 | * For example, a request to log a field before agi_unlinked and a field | |
1502 | * after agi_unlinked could cause us to log the entire hash table and use | |
1503 | * an excessive amount of log space. To avoid this behavior, log the | |
1504 | * region up through agi_unlinked in one call and the region after | |
1505 | * agi_unlinked through the end of the structure in another. | |
2bd0ea18 | 1506 | */ |
c0a4c227 BF |
1507 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF); |
1508 | ||
2bd0ea18 | 1509 | /* |
c0a4c227 BF |
1510 | * Compute byte offsets for the first and last fields in the first |
1511 | * region and log agi buffer. This only logs up through agi_unlinked. | |
2bd0ea18 | 1512 | */ |
c0a4c227 BF |
1513 | if (fields & XFS_AGI_ALL_BITS_R1) { |
1514 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1, | |
1515 | &first, &last); | |
1516 | xfs_trans_log_buf(tp, bp, first, last); | |
1517 | } | |
1518 | ||
1519 | /* | |
1520 | * Mask off the bits in the first region and calculate the first and last | |
1521 | * field offsets for any bits in the second region. | |
1522 | */ | |
1523 | fields &= ~XFS_AGI_ALL_BITS_R1; | |
1524 | if (fields) { | |
1525 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2, | |
1526 | &first, &last); | |
1527 | xfs_trans_log_buf(tp, bp, first, last); | |
1528 | } | |
2bd0ea18 NS |
1529 | } |
1530 | ||
56b2de80 DC |
1531 | #ifdef DEBUG |
1532 | STATIC void | |
1533 | xfs_check_agi_unlinked( | |
1534 | struct xfs_agi *agi) | |
1535 | { | |
1536 | int i; | |
1537 | ||
1538 | for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) | |
1539 | ASSERT(agi->agi_unlinked[i]); | |
1540 | } | |
1541 | #else | |
1542 | #define xfs_check_agi_unlinked(agi) | |
1543 | #endif | |
1544 | ||
dd5b876e | 1545 | static bool |
a2ceac1f DC |
1546 | xfs_agi_verify( |
1547 | struct xfs_buf *bp) | |
1548 | { | |
1549 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
1550 | struct xfs_agi *agi = XFS_BUF_TO_AGI(bp); | |
a2ceac1f | 1551 | |
dd5b876e DC |
1552 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
1553 | !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid)) | |
1554 | return false; | |
a2ceac1f DC |
1555 | /* |
1556 | * Validate the magic number of the agi block. | |
1557 | */ | |
dd5b876e DC |
1558 | if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC)) |
1559 | return false; | |
1560 | if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum))) | |
1561 | return false; | |
a2ceac1f DC |
1562 | |
1563 | /* | |
1564 | * during growfs operations, the perag is not fully initialised, | |
1565 | * so we can't use it for any useful checking. growfs ensures we can't | |
1566 | * use it by using uncached buffers that don't have the perag attached | |
1567 | * so we can detect and avoid this problem. | |
1568 | */ | |
dd5b876e DC |
1569 | if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno) |
1570 | return false; | |
a2ceac1f | 1571 | |
a2ceac1f | 1572 | xfs_check_agi_unlinked(agi); |
dd5b876e | 1573 | return true; |
a2ceac1f DC |
1574 | } |
1575 | ||
1576 | static void | |
1577 | xfs_agi_read_verify( | |
1578 | struct xfs_buf *bp) | |
1579 | { | |
dd5b876e | 1580 | struct xfs_mount *mp = bp->b_target->bt_mount; |
dd5b876e | 1581 | |
45922933 DC |
1582 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
1583 | !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF)) | |
1584 | xfs_buf_ioerror(bp, EFSBADCRC); | |
1585 | else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp, | |
1586 | XFS_ERRTAG_IALLOC_READ_AGI, | |
1587 | XFS_RANDOM_IALLOC_READ_AGI)) | |
dd5b876e | 1588 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
45922933 DC |
1589 | |
1590 | if (bp->b_error) | |
1591 | xfs_verifier_error(bp); | |
a2ceac1f DC |
1592 | } |
1593 | ||
1594 | static void | |
1595 | xfs_agi_write_verify( | |
1596 | struct xfs_buf *bp) | |
1597 | { | |
dd5b876e DC |
1598 | struct xfs_mount *mp = bp->b_target->bt_mount; |
1599 | struct xfs_buf_log_item *bip = bp->b_fspriv; | |
1600 | ||
1601 | if (!xfs_agi_verify(bp)) { | |
dd5b876e | 1602 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
45922933 | 1603 | xfs_verifier_error(bp); |
dd5b876e DC |
1604 | return; |
1605 | } | |
1606 | ||
1607 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
1608 | return; | |
1609 | ||
1610 | if (bip) | |
1611 | XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
43b5aeed | 1612 | xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF); |
a2ceac1f DC |
1613 | } |
1614 | ||
1615 | const struct xfs_buf_ops xfs_agi_buf_ops = { | |
1616 | .verify_read = xfs_agi_read_verify, | |
1617 | .verify_write = xfs_agi_write_verify, | |
1618 | }; | |
1619 | ||
2bd0ea18 NS |
1620 | /* |
1621 | * Read in the allocation group header (inode allocation section) | |
1622 | */ | |
1623 | int | |
56b2de80 DC |
1624 | xfs_read_agi( |
1625 | struct xfs_mount *mp, /* file system mount structure */ | |
1626 | struct xfs_trans *tp, /* transaction pointer */ | |
1627 | xfs_agnumber_t agno, /* allocation group number */ | |
1628 | struct xfs_buf **bpp) /* allocation group hdr buf */ | |
2bd0ea18 | 1629 | { |
56b2de80 | 1630 | int error; |
2bd0ea18 NS |
1631 | |
1632 | ASSERT(agno != NULLAGNUMBER); | |
56b2de80 DC |
1633 | |
1634 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, | |
9440d84d | 1635 | XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), |
a2ceac1f | 1636 | XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops); |
9440d84d | 1637 | if (error) |
2bd0ea18 | 1638 | return error; |
56b2de80 | 1639 | |
a2ceac1f DC |
1640 | ASSERT(!xfs_buf_geterror(*bpp)); |
1641 | xfs_buf_set_ref(*bpp, XFS_AGI_REF); | |
56b2de80 DC |
1642 | return 0; |
1643 | } | |
1644 | ||
1645 | int | |
1646 | xfs_ialloc_read_agi( | |
1647 | struct xfs_mount *mp, /* file system mount structure */ | |
1648 | struct xfs_trans *tp, /* transaction pointer */ | |
1649 | xfs_agnumber_t agno, /* allocation group number */ | |
1650 | struct xfs_buf **bpp) /* allocation group hdr buf */ | |
1651 | { | |
1652 | struct xfs_agi *agi; /* allocation group header */ | |
1653 | struct xfs_perag *pag; /* per allocation group data */ | |
1654 | int error; | |
1655 | ||
1656 | error = xfs_read_agi(mp, tp, agno, bpp); | |
1657 | if (error) | |
1658 | return error; | |
1659 | ||
1660 | agi = XFS_BUF_TO_AGI(*bpp); | |
1661 | pag = xfs_perag_get(mp, agno); | |
2bd0ea18 | 1662 | if (!pag->pagi_init) { |
6e3140c7 | 1663 | pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); |
cdded3d8 | 1664 | pag->pagi_count = be32_to_cpu(agi->agi_count); |
2bd0ea18 | 1665 | pag->pagi_init = 1; |
9440d84d | 1666 | } |
9440d84d | 1667 | |
56b2de80 DC |
1668 | /* |
1669 | * It's possible for these to be out of sync if | |
1670 | * we are in the middle of a forced shutdown. | |
1671 | */ | |
1672 | ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || | |
1673 | XFS_FORCED_SHUTDOWN(mp)); | |
1674 | xfs_perag_put(pag); | |
2bd0ea18 NS |
1675 | return 0; |
1676 | } | |
cdded3d8 DC |
1677 | |
1678 | /* | |
1679 | * Read in the agi to initialise the per-ag data in the mount structure | |
1680 | */ | |
1681 | int | |
1682 | xfs_ialloc_pagi_init( | |
1683 | xfs_mount_t *mp, /* file system mount structure */ | |
1684 | xfs_trans_t *tp, /* transaction pointer */ | |
1685 | xfs_agnumber_t agno) /* allocation group number */ | |
1686 | { | |
1687 | xfs_buf_t *bp = NULL; | |
1688 | int error; | |
1689 | ||
5e656dbb BN |
1690 | error = xfs_ialloc_read_agi(mp, tp, agno, &bp); |
1691 | if (error) | |
cdded3d8 DC |
1692 | return error; |
1693 | if (bp) | |
1694 | xfs_trans_brelse(tp, bp); | |
1695 | return 0; | |
1696 | } |