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