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