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