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