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