]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
da23017d NS |
2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
5000d01d | 4 | * |
da23017d 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 | |
2bd0ea18 | 7 | * published by the Free Software Foundation. |
5000d01d | 8 | * |
da23017d 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. | |
5000d01d | 13 | * |
da23017d 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 | |
2bd0ea18 NS |
17 | */ |
18 | ||
19 | #include <xfs.h> | |
20 | ||
21 | /* | |
22 | * Internal functions. | |
23 | */ | |
24 | ||
25 | /* | |
26 | * Log specified fields for the inode given by bp and off. | |
27 | */ | |
28 | STATIC void | |
29 | xfs_ialloc_log_di( | |
30 | xfs_trans_t *tp, /* transaction pointer */ | |
31 | xfs_buf_t *bp, /* inode buffer */ | |
32 | int off, /* index of inode in buffer */ | |
33 | int fields) /* bitmask of fields to log */ | |
34 | { | |
35 | int first; /* first byte number */ | |
36 | int ioffset; /* off in bytes */ | |
37 | int last; /* last byte number */ | |
38 | xfs_mount_t *mp; /* mount point structure */ | |
39 | static const short offsets[] = { /* field offsets */ | |
40 | /* keep in sync with bits */ | |
41 | offsetof(xfs_dinode_core_t, di_magic), | |
42 | offsetof(xfs_dinode_core_t, di_mode), | |
43 | offsetof(xfs_dinode_core_t, di_version), | |
44 | offsetof(xfs_dinode_core_t, di_format), | |
45 | offsetof(xfs_dinode_core_t, di_onlink), | |
46 | offsetof(xfs_dinode_core_t, di_uid), | |
47 | offsetof(xfs_dinode_core_t, di_gid), | |
48 | offsetof(xfs_dinode_core_t, di_nlink), | |
49 | offsetof(xfs_dinode_core_t, di_projid), | |
50 | offsetof(xfs_dinode_core_t, di_pad), | |
51 | offsetof(xfs_dinode_core_t, di_atime), | |
52 | offsetof(xfs_dinode_core_t, di_mtime), | |
53 | offsetof(xfs_dinode_core_t, di_ctime), | |
54 | offsetof(xfs_dinode_core_t, di_size), | |
55 | offsetof(xfs_dinode_core_t, di_nblocks), | |
56 | offsetof(xfs_dinode_core_t, di_extsize), | |
57 | offsetof(xfs_dinode_core_t, di_nextents), | |
58 | offsetof(xfs_dinode_core_t, di_anextents), | |
59 | offsetof(xfs_dinode_core_t, di_forkoff), | |
60 | offsetof(xfs_dinode_core_t, di_aformat), | |
61 | offsetof(xfs_dinode_core_t, di_dmevmask), | |
62 | offsetof(xfs_dinode_core_t, di_dmstate), | |
63 | offsetof(xfs_dinode_core_t, di_flags), | |
64 | offsetof(xfs_dinode_core_t, di_gen), | |
65 | offsetof(xfs_dinode_t, di_next_unlinked), | |
66 | offsetof(xfs_dinode_t, di_u), | |
67 | offsetof(xfs_dinode_t, di_a), | |
68 | sizeof(xfs_dinode_t) | |
69 | }; | |
5000d01d | 70 | |
2bd0ea18 NS |
71 | |
72 | ASSERT(offsetof(xfs_dinode_t, di_core) == 0); | |
73 | ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0); | |
74 | mp = tp->t_mountp; | |
75 | /* | |
76 | * Get the inode-relative first and last bytes for these fields | |
77 | */ | |
78 | xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last); | |
79 | /* | |
80 | * Convert to buffer offsets and log it. | |
81 | */ | |
82 | ioffset = off << mp->m_sb.sb_inodelog; | |
83 | first += ioffset; | |
84 | last += ioffset; | |
85 | xfs_trans_log_buf(tp, bp, first, last); | |
86 | } | |
87 | ||
88 | /* | |
89 | * Allocation group level functions. | |
90 | */ | |
91 | ||
92 | /* | |
93 | * Allocate new inodes in the allocation group specified by agbp. | |
94 | * Return 0 for success, else error code. | |
95 | */ | |
96 | STATIC int /* error code or 0 */ | |
97 | xfs_ialloc_ag_alloc( | |
98 | xfs_trans_t *tp, /* transaction pointer */ | |
99 | xfs_buf_t *agbp, /* alloc group buffer */ | |
100 | int *alloc) | |
101 | { | |
102 | xfs_agi_t *agi; /* allocation group header */ | |
dfc130f3 | 103 | xfs_alloc_arg_t args; /* allocation argument structure */ |
2bd0ea18 | 104 | int blks_per_cluster; /* fs blocks per inode cluster */ |
dfc130f3 | 105 | xfs_btree_cur_t *cur; /* inode btree cursor */ |
0e266570 | 106 | xfs_daddr_t d; /* disk addr of buffer */ |
2bd0ea18 NS |
107 | int error; |
108 | xfs_buf_t *fbuf; /* new free inodes' buffer */ | |
109 | xfs_dinode_t *free; /* new free inode structure */ | |
110 | int i; /* inode counter */ | |
111 | int j; /* block counter */ | |
112 | int nbufs; /* num bufs of new inodes */ | |
113 | xfs_agino_t newino; /* new first inode's number */ | |
114 | xfs_agino_t newlen; /* new number of inodes */ | |
115 | int ninodes; /* num inodes per buf */ | |
116 | xfs_agino_t thisino; /* current inode number, for loop */ | |
117 | int version; /* inode version number to use */ | |
2bd0ea18 NS |
118 | int isaligned; /* inode allocation at stripe unit */ |
119 | /* boundary */ | |
dfc130f3 | 120 | xfs_dinode_core_t dic; /* a dinode_core to copy to new */ |
5000d01d SL |
121 | /* inodes */ |
122 | ||
2bd0ea18 NS |
123 | args.tp = tp; |
124 | args.mp = tp->t_mountp; | |
125 | ||
126 | /* | |
127 | * Locking will ensure that we don't have two callers in here | |
128 | * at one time. | |
129 | */ | |
130 | newlen = XFS_IALLOC_INODES(args.mp); | |
131 | if (args.mp->m_maxicount && | |
132 | args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount) | |
133 | return XFS_ERROR(ENOSPC); | |
134 | args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp); | |
135 | /* | |
136 | * Set the alignment for the allocation. | |
137 | * If stripe alignment is turned on then align at stripe unit | |
138 | * boundary. | |
5000d01d | 139 | * If the cluster size is smaller than a filesystem block |
2bd0ea18 NS |
140 | * then we're doing I/O for inodes in filesystem block size pieces, |
141 | * so don't need alignment anyway. | |
142 | */ | |
143 | isaligned = 0; | |
144 | if (args.mp->m_sinoalign) { | |
145 | ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN)); | |
146 | args.alignment = args.mp->m_dalign; | |
147 | isaligned = 1; | |
148 | } else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) && | |
5000d01d | 149 | args.mp->m_sb.sb_inoalignmt >= |
2bd0ea18 NS |
150 | XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp))) |
151 | args.alignment = args.mp->m_sb.sb_inoalignmt; | |
152 | else | |
153 | args.alignment = 1; | |
154 | agi = XFS_BUF_TO_AGI(agbp); | |
155 | /* | |
156 | * Need to figure out where to allocate the inode blocks. | |
157 | * Ideally they should be spaced out through the a.g. | |
158 | * For now, just allocate blocks up front. | |
159 | */ | |
6e3140c7 NS |
160 | args.agbno = be32_to_cpu(agi->agi_root); |
161 | args.fsbno = XFS_AGB_TO_FSB(args.mp, be32_to_cpu(agi->agi_seqno), | |
2bd0ea18 NS |
162 | args.agbno); |
163 | /* | |
164 | * Allocate a fixed-size extent of inodes. | |
165 | */ | |
166 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
5000d01d | 167 | args.mod = args.total = args.wasdel = args.isfl = args.userdata = |
2bd0ea18 NS |
168 | args.minalignslop = 0; |
169 | args.prod = 1; | |
170 | /* | |
171 | * Allow space for the inode btree to split. | |
172 | */ | |
173 | args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1; | |
0e266570 | 174 | if ((error = xfs_alloc_vextent(&args))) |
2bd0ea18 NS |
175 | return error; |
176 | ||
177 | /* | |
178 | * If stripe alignment is turned on, then try again with cluster | |
179 | * alignment. | |
180 | */ | |
181 | if (isaligned && args.fsbno == NULLFSBLOCK) { | |
182 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
6e3140c7 | 183 | args.agbno = be32_to_cpu(agi->agi_root); |
2bd0ea18 | 184 | args.fsbno = XFS_AGB_TO_FSB(args.mp, |
6e3140c7 | 185 | be32_to_cpu(agi->agi_seqno), args.agbno); |
2bd0ea18 | 186 | if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) && |
5000d01d SL |
187 | args.mp->m_sb.sb_inoalignmt >= |
188 | XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp))) | |
2bd0ea18 NS |
189 | args.alignment = args.mp->m_sb.sb_inoalignmt; |
190 | else | |
191 | args.alignment = 1; | |
0e266570 | 192 | if ((error = xfs_alloc_vextent(&args))) |
dfc130f3 | 193 | return error; |
2bd0ea18 | 194 | } |
5000d01d | 195 | |
2bd0ea18 NS |
196 | if (args.fsbno == NULLFSBLOCK) { |
197 | *alloc = 0; | |
198 | return 0; | |
199 | } | |
200 | ASSERT(args.len == args.minlen); | |
201 | /* | |
202 | * Convert the results. | |
203 | */ | |
204 | newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0); | |
205 | /* | |
206 | * Loop over the new block(s), filling in the inodes. | |
207 | * For small block sizes, manipulate the inodes in buffers | |
208 | * which are multiples of the blocks size. | |
209 | */ | |
210 | if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) { | |
211 | blks_per_cluster = 1; | |
212 | nbufs = (int)args.len; | |
213 | ninodes = args.mp->m_sb.sb_inopblock; | |
214 | } else { | |
215 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) / | |
5000d01d | 216 | args.mp->m_sb.sb_blocksize; |
2bd0ea18 NS |
217 | nbufs = (int)args.len / blks_per_cluster; |
218 | ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock; | |
219 | } | |
220 | /* | |
221 | * Figure out what version number to use in the inodes we create. | |
222 | * If the superblock version has caught up to the one that supports | |
223 | * the new inode format, then use the new inode version. Otherwise | |
224 | * use the old version so that old kernels will continue to be | |
225 | * able to use the file system. | |
226 | */ | |
227 | if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb)) | |
228 | version = XFS_DINODE_VERSION_2; | |
229 | else | |
230 | version = XFS_DINODE_VERSION_1; | |
a562a63b NS |
231 | |
232 | memset(&dic, 0, sizeof(xfs_dinode_core_t)); | |
233 | INT_SET(dic.di_magic, ARCH_CONVERT, XFS_DINODE_MAGIC); | |
234 | INT_SET(dic.di_version, ARCH_CONVERT, version); | |
235 | ||
2bd0ea18 NS |
236 | for (j = 0; j < nbufs; j++) { |
237 | /* | |
238 | * Get the block. | |
239 | */ | |
6e3140c7 | 240 | d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno), |
2bd0ea18 NS |
241 | args.agbno + (j * blks_per_cluster)); |
242 | fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d, | |
243 | args.mp->m_bsize * blks_per_cluster, | |
244 | XFS_BUF_LOCK); | |
245 | ASSERT(fbuf); | |
5000d01d | 246 | ASSERT(!XFS_BUF_GETERROR(fbuf)); |
2bd0ea18 NS |
247 | /* |
248 | * Loop over the inodes in this buffer. | |
249 | */ | |
5000d01d | 250 | |
2bd0ea18 NS |
251 | for (i = 0; i < ninodes; i++) { |
252 | free = XFS_MAKE_IPTR(args.mp, fbuf, i); | |
32181a02 | 253 | memcpy(&(free->di_core), &dic, sizeof(xfs_dinode_core_t)); |
5000d01d | 254 | INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO); |
2bd0ea18 NS |
255 | xfs_ialloc_log_di(tp, fbuf, i, |
256 | XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED); | |
257 | } | |
258 | xfs_trans_inode_alloc_buf(tp, fbuf); | |
259 | } | |
6e3140c7 NS |
260 | be32_add(&agi->agi_count, newlen); |
261 | be32_add(&agi->agi_freecount, newlen); | |
e56fcdce | 262 | down_read(&args.mp->m_peraglock); |
6e3140c7 | 263 | args.mp->m_perag[be32_to_cpu(agi->agi_seqno)].pagi_freecount += newlen; |
e56fcdce | 264 | up_read(&args.mp->m_peraglock); |
6e3140c7 | 265 | agi->agi_newino = cpu_to_be32(newino); |
2bd0ea18 NS |
266 | /* |
267 | * Insert records describing the new inode chunk into the btree. | |
268 | */ | |
6e3140c7 NS |
269 | cur = xfs_btree_init_cursor(args.mp, tp, agbp, |
270 | be32_to_cpu(agi->agi_seqno), | |
2bd0ea18 NS |
271 | XFS_BTNUM_INO, (xfs_inode_t *)0, 0); |
272 | for (thisino = newino; | |
273 | thisino < newino + newlen; | |
274 | thisino += XFS_INODES_PER_CHUNK) { | |
0e266570 NS |
275 | if ((error = xfs_inobt_lookup_eq(cur, thisino, |
276 | XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) { | |
2bd0ea18 NS |
277 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
278 | return error; | |
279 | } | |
280 | ASSERT(i == 0); | |
0e266570 | 281 | if ((error = xfs_inobt_insert(cur, &i))) { |
2bd0ea18 NS |
282 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
283 | return error; | |
284 | } | |
285 | ASSERT(i == 1); | |
286 | } | |
287 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
288 | /* | |
289 | * Log allocation group header fields | |
290 | */ | |
291 | xfs_ialloc_log_agi(tp, agbp, | |
292 | XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); | |
293 | /* | |
294 | * Modify/log superblock values for inode count and inode free count. | |
295 | */ | |
296 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); | |
297 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); | |
298 | *alloc = 1; | |
299 | return 0; | |
300 | } | |
301 | ||
321717ae NS |
302 | STATIC __inline xfs_agnumber_t |
303 | xfs_ialloc_next_ag( | |
304 | xfs_mount_t *mp) | |
305 | { | |
306 | xfs_agnumber_t agno; | |
307 | ||
308 | spin_lock(&mp->m_agirotor_lock); | |
309 | agno = mp->m_agirotor; | |
310 | if (++mp->m_agirotor == mp->m_maxagi) | |
311 | mp->m_agirotor = 0; | |
312 | spin_unlock(&mp->m_agirotor_lock); | |
313 | ||
314 | return agno; | |
315 | } | |
316 | ||
2bd0ea18 NS |
317 | /* |
318 | * Select an allocation group to look for a free inode in, based on the parent | |
dfc130f3 | 319 | * inode and then mode. Return the allocation group buffer. |
2bd0ea18 NS |
320 | */ |
321 | STATIC xfs_buf_t * /* allocation group buffer */ | |
322 | xfs_ialloc_ag_select( | |
323 | xfs_trans_t *tp, /* transaction pointer */ | |
324 | xfs_ino_t parent, /* parent directory inode number */ | |
325 | mode_t mode, /* bits set to indicate file type */ | |
326 | int okalloc) /* ok to allocate more space */ | |
327 | { | |
328 | xfs_buf_t *agbp; /* allocation group header buffer */ | |
329 | xfs_agnumber_t agcount; /* number of ag's in the filesystem */ | |
330 | xfs_agnumber_t agno; /* current ag number */ | |
331 | int flags; /* alloc buffer locking flags */ | |
332 | xfs_extlen_t ineed; /* blocks needed for inode allocation */ | |
275ae71f | 333 | xfs_extlen_t longest = 0; /* longest extent available */ |
2bd0ea18 NS |
334 | xfs_mount_t *mp; /* mount point structure */ |
335 | int needspace; /* file mode implies space allocated */ | |
336 | xfs_perag_t *pag; /* per allocation group data */ | |
337 | xfs_agnumber_t pagno; /* parent (starting) ag number */ | |
338 | ||
339 | /* | |
340 | * Files of these types need at least one block if length > 0 | |
341 | * (and they won't fit in the inode, but that's hard to figure out). | |
342 | */ | |
343 | needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); | |
344 | mp = tp->t_mountp; | |
34317449 | 345 | agcount = mp->m_maxagi; |
2bd0ea18 | 346 | if (S_ISDIR(mode)) |
321717ae | 347 | pagno = xfs_ialloc_next_ag(mp); |
5ce1d1f7 | 348 | else { |
2bd0ea18 | 349 | pagno = XFS_INO_TO_AGNO(mp, parent); |
5ce1d1f7 NS |
350 | if (pagno >= agcount) |
351 | pagno = 0; | |
352 | } | |
2bd0ea18 NS |
353 | ASSERT(pagno < agcount); |
354 | /* | |
355 | * Loop through allocation groups, looking for one with a little | |
356 | * free space in it. Note we don't look for free inodes, exactly. | |
357 | * Instead, we include whether there is a need to allocate inodes | |
5000d01d | 358 | * to mean that blocks must be allocated for them, |
2bd0ea18 NS |
359 | * if none are currently free. |
360 | */ | |
361 | agno = pagno; | |
362 | flags = XFS_ALLOC_FLAG_TRYLOCK; | |
e56fcdce | 363 | down_read(&mp->m_peraglock); |
2bd0ea18 | 364 | for (;;) { |
2bd0ea18 NS |
365 | pag = &mp->m_perag[agno]; |
366 | if (!pag->pagi_init) { | |
367 | if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { | |
368 | agbp = NULL; | |
2bd0ea18 NS |
369 | goto nextag; |
370 | } | |
371 | } else | |
372 | agbp = NULL; | |
34317449 NS |
373 | |
374 | if (!pag->pagi_inodeok) { | |
321717ae | 375 | xfs_ialloc_next_ag(mp); |
34317449 NS |
376 | goto unlock_nextag; |
377 | } | |
378 | ||
2bd0ea18 NS |
379 | /* |
380 | * Is there enough free space for the file plus a block | |
381 | * of inodes (if we need to allocate some)? | |
382 | */ | |
383 | ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp); | |
384 | if (ineed && !pag->pagf_init) { | |
385 | if (agbp == NULL && | |
386 | xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { | |
387 | agbp = NULL; | |
2bd0ea18 NS |
388 | goto nextag; |
389 | } | |
390 | (void)xfs_alloc_pagf_init(mp, tp, agno, flags); | |
391 | } | |
392 | if (!ineed || pag->pagf_init) { | |
393 | if (ineed && !(longest = pag->pagf_longest)) | |
394 | longest = pag->pagf_flcount > 0; | |
395 | if (!ineed || | |
396 | (pag->pagf_freeblks >= needspace + ineed && | |
397 | longest >= ineed && | |
398 | okalloc)) { | |
399 | if (agbp == NULL && | |
400 | xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { | |
401 | agbp = NULL; | |
2bd0ea18 NS |
402 | goto nextag; |
403 | } | |
e56fcdce | 404 | up_read(&mp->m_peraglock); |
2bd0ea18 NS |
405 | return agbp; |
406 | } | |
407 | } | |
34317449 | 408 | unlock_nextag: |
2bd0ea18 NS |
409 | if (agbp) |
410 | xfs_trans_brelse(tp, agbp); | |
5000d01d SL |
411 | nextag: |
412 | /* | |
2bd0ea18 NS |
413 | * No point in iterating over the rest, if we're shutting |
414 | * down. | |
415 | */ | |
99ec8857 | 416 | if (XFS_FORCED_SHUTDOWN(mp)) { |
e56fcdce | 417 | up_read(&mp->m_peraglock); |
2bd0ea18 | 418 | return (xfs_buf_t *)0; |
99ec8857 | 419 | } |
2bd0ea18 | 420 | agno++; |
5ce1d1f7 | 421 | if (agno >= agcount) |
2bd0ea18 NS |
422 | agno = 0; |
423 | if (agno == pagno) { | |
99ec8857 | 424 | if (flags == 0) { |
e56fcdce | 425 | up_read(&mp->m_peraglock); |
2bd0ea18 | 426 | return (xfs_buf_t *)0; |
99ec8857 | 427 | } |
2bd0ea18 NS |
428 | flags = 0; |
429 | } | |
430 | } | |
431 | } | |
432 | ||
5000d01d | 433 | /* |
2bd0ea18 NS |
434 | * Visible inode allocation functions. |
435 | */ | |
436 | ||
437 | /* | |
438 | * Allocate an inode on disk. | |
439 | * Mode is used to tell whether the new inode will need space, and whether | |
440 | * it is a directory. | |
441 | * | |
442 | * The arguments IO_agbp and alloc_done are defined to work within | |
443 | * the constraint of one allocation per transaction. | |
444 | * xfs_dialloc() is designed to be called twice if it has to do an | |
dfc130f3 | 445 | * allocation to make more free inodes. On the first call, |
2bd0ea18 NS |
446 | * IO_agbp should be set to NULL. If an inode is available, |
447 | * i.e., xfs_dialloc() did not need to do an allocation, an inode | |
dfc130f3 | 448 | * number is returned. In this case, IO_agbp would be set to the |
2bd0ea18 NS |
449 | * current ag_buf and alloc_done set to false. |
450 | * If an allocation needed to be done, xfs_dialloc would return | |
451 | * the current ag_buf in IO_agbp and set alloc_done to true. | |
452 | * The caller should then commit the current transaction, allocate a new | |
453 | * transaction, and call xfs_dialloc() again, passing in the previous | |
454 | * value of IO_agbp. IO_agbp should be held across the transactions. | |
455 | * Since the agbp is locked across the two calls, the second call is | |
456 | * guaranteed to have a free inode available. | |
457 | * | |
458 | * Once we successfully pick an inode its number is returned and the | |
dfc130f3 | 459 | * on-disk data structures are updated. The inode itself is not read |
2bd0ea18 NS |
460 | * in, since doing so would break ordering constraints with xfs_reclaim. |
461 | */ | |
462 | int | |
463 | xfs_dialloc( | |
464 | xfs_trans_t *tp, /* transaction pointer */ | |
465 | xfs_ino_t parent, /* parent inode (directory) */ | |
466 | mode_t mode, /* mode bits for new inode */ | |
467 | int okalloc, /* ok to allocate more space */ | |
468 | xfs_buf_t **IO_agbp, /* in/out ag header's buffer */ | |
469 | boolean_t *alloc_done, /* true if we needed to replenish | |
470 | inode freelist */ | |
471 | xfs_ino_t *inop) /* inode number allocated */ | |
472 | { | |
473 | xfs_agnumber_t agcount; /* number of allocation groups */ | |
474 | xfs_buf_t *agbp; /* allocation group header's buffer */ | |
475 | xfs_agnumber_t agno; /* allocation group number */ | |
476 | xfs_agi_t *agi; /* allocation group header structure */ | |
dfc130f3 | 477 | xfs_btree_cur_t *cur; /* inode allocation btree cursor */ |
2bd0ea18 NS |
478 | int error; /* error return value */ |
479 | int i; /* result code */ | |
480 | int ialloced; /* inode allocation status */ | |
481 | int noroom = 0; /* no space for inode blk allocation */ | |
482 | xfs_ino_t ino; /* fs-relative inode to be returned */ | |
483 | /* REFERENCED */ | |
484 | int j; /* result code */ | |
485 | xfs_mount_t *mp; /* file system mount structure */ | |
486 | int offset; /* index of inode in chunk */ | |
487 | xfs_agino_t pagino; /* parent's a.g. relative inode # */ | |
488 | xfs_agnumber_t pagno; /* parent's allocation group number */ | |
dfc130f3 | 489 | xfs_inobt_rec_t rec; /* inode allocation record */ |
2bd0ea18 | 490 | xfs_agnumber_t tagno; /* testing allocation group number */ |
dfc130f3 RC |
491 | xfs_btree_cur_t *tcur; /* temp cursor */ |
492 | xfs_inobt_rec_t trec; /* temp inode allocation record */ | |
2bd0ea18 NS |
493 | |
494 | ||
495 | if (*IO_agbp == NULL) { | |
496 | /* | |
497 | * We do not have an agbp, so select an initial allocation | |
498 | * group for inode allocation. | |
499 | */ | |
500 | agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc); | |
501 | /* | |
5000d01d | 502 | * Couldn't find an allocation group satisfying the |
2bd0ea18 NS |
503 | * criteria, give up. |
504 | */ | |
505 | if (!agbp) { | |
506 | *inop = NULLFSINO; | |
507 | return 0; | |
508 | } | |
509 | agi = XFS_BUF_TO_AGI(agbp); | |
6e3140c7 | 510 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
2bd0ea18 NS |
511 | } else { |
512 | /* | |
5000d01d | 513 | * Continue where we left off before. In this case, we |
2bd0ea18 NS |
514 | * know that the allocation group has free inodes. |
515 | */ | |
516 | agbp = *IO_agbp; | |
517 | agi = XFS_BUF_TO_AGI(agbp); | |
6e3140c7 NS |
518 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
519 | ASSERT(be32_to_cpu(agi->agi_freecount) > 0); | |
2bd0ea18 NS |
520 | } |
521 | mp = tp->t_mountp; | |
522 | agcount = mp->m_sb.sb_agcount; | |
6e3140c7 | 523 | agno = be32_to_cpu(agi->agi_seqno); |
2bd0ea18 NS |
524 | tagno = agno; |
525 | pagno = XFS_INO_TO_AGNO(mp, parent); | |
526 | pagino = XFS_INO_TO_AGINO(mp, parent); | |
527 | ||
528 | /* | |
529 | * If we have already hit the ceiling of inode blocks then clear | |
530 | * okalloc so we scan all available agi structures for a free | |
531 | * inode. | |
532 | */ | |
533 | ||
534 | if (mp->m_maxicount && | |
535 | mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) { | |
536 | noroom = 1; | |
537 | okalloc = 0; | |
538 | } | |
539 | ||
540 | /* | |
541 | * Loop until we find an allocation group that either has free inodes | |
542 | * or in which we can allocate some inodes. Iterate through the | |
543 | * allocation groups upward, wrapping at the end. | |
544 | */ | |
545 | *alloc_done = B_FALSE; | |
46eca962 | 546 | while (!agi->agi_freecount) { |
5000d01d | 547 | /* |
2bd0ea18 NS |
548 | * Don't do anything if we're not supposed to allocate |
549 | * any blocks, just go on to the next ag. | |
550 | */ | |
551 | if (okalloc) { | |
552 | /* | |
553 | * Try to allocate some new inodes in the allocation | |
554 | * group. | |
555 | */ | |
0e266570 | 556 | if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) { |
2bd0ea18 NS |
557 | xfs_trans_brelse(tp, agbp); |
558 | if (error == ENOSPC) { | |
559 | *inop = NULLFSINO; | |
560 | return 0; | |
561 | } else | |
562 | return error; | |
563 | } | |
564 | if (ialloced) { | |
565 | /* | |
566 | * We successfully allocated some inodes, return | |
567 | * the current context to the caller so that it | |
568 | * can commit the current transaction and call | |
569 | * us again where we left off. | |
570 | */ | |
6e3140c7 | 571 | ASSERT(be32_to_cpu(agi->agi_freecount) > 0); |
2bd0ea18 NS |
572 | *alloc_done = B_TRUE; |
573 | *IO_agbp = agbp; | |
574 | *inop = NULLFSINO; | |
575 | return 0; | |
576 | } | |
577 | } | |
578 | /* | |
579 | * If it failed, give up on this ag. | |
580 | */ | |
581 | xfs_trans_brelse(tp, agbp); | |
582 | /* | |
583 | * Go on to the next ag: get its ag header. | |
584 | */ | |
585 | nextag: | |
586 | if (++tagno == agcount) | |
587 | tagno = 0; | |
588 | if (tagno == agno) { | |
589 | *inop = NULLFSINO; | |
590 | return noroom ? ENOSPC : 0; | |
591 | } | |
e56fcdce | 592 | down_read(&mp->m_peraglock); |
34317449 | 593 | if (mp->m_perag[tagno].pagi_inodeok == 0) { |
e56fcdce | 594 | up_read(&mp->m_peraglock); |
34317449 NS |
595 | goto nextag; |
596 | } | |
2bd0ea18 | 597 | error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp); |
e56fcdce | 598 | up_read(&mp->m_peraglock); |
2bd0ea18 NS |
599 | if (error) |
600 | goto nextag; | |
601 | agi = XFS_BUF_TO_AGI(agbp); | |
6e3140c7 | 602 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
2bd0ea18 NS |
603 | } |
604 | /* | |
605 | * Here with an allocation group that has a free inode. | |
606 | * Reset agno since we may have chosen a new ag in the | |
607 | * loop above. | |
608 | */ | |
609 | agno = tagno; | |
610 | *IO_agbp = NULL; | |
6e3140c7 | 611 | cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno), |
2bd0ea18 NS |
612 | XFS_BTNUM_INO, (xfs_inode_t *)0, 0); |
613 | /* | |
614 | * If pagino is 0 (this is the root inode allocation) use newino. | |
615 | * This must work because we've just allocated some. | |
616 | */ | |
617 | if (!pagino) | |
6e3140c7 | 618 | pagino = be32_to_cpu(agi->agi_newino); |
2bd0ea18 NS |
619 | #ifdef DEBUG |
620 | if (cur->bc_nlevels == 1) { | |
621 | int freecount = 0; | |
622 | ||
0e266570 | 623 | if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) |
2bd0ea18 NS |
624 | goto error0; |
625 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
626 | do { | |
0e266570 | 627 | if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, |
46eca962 | 628 | &rec.ir_freecount, &rec.ir_free, &i))) |
2bd0ea18 NS |
629 | goto error0; |
630 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
631 | freecount += rec.ir_freecount; | |
0e266570 | 632 | if ((error = xfs_inobt_increment(cur, 0, &i))) |
2bd0ea18 NS |
633 | goto error0; |
634 | } while (i == 1); | |
635 | ||
6e3140c7 | 636 | ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || |
2bd0ea18 NS |
637 | XFS_FORCED_SHUTDOWN(mp)); |
638 | } | |
639 | #endif | |
640 | /* | |
641 | * If in the same a.g. as the parent, try to get near the parent. | |
642 | */ | |
643 | if (pagno == agno) { | |
0e266570 | 644 | if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i))) |
2bd0ea18 NS |
645 | goto error0; |
646 | if (i != 0 && | |
647 | (error = xfs_inobt_get_rec(cur, &rec.ir_startino, | |
46eca962 | 648 | &rec.ir_freecount, &rec.ir_free, &j)) == 0 && |
2bd0ea18 NS |
649 | j == 1 && |
650 | rec.ir_freecount > 0) { | |
651 | /* | |
652 | * Found a free inode in the same chunk | |
653 | * as parent, done. | |
654 | */ | |
655 | } | |
656 | /* | |
657 | * In the same a.g. as parent, but parent's chunk is full. | |
658 | */ | |
659 | else { | |
660 | int doneleft; /* done, to the left */ | |
661 | int doneright; /* done, to the right */ | |
662 | ||
663 | if (error) | |
664 | goto error0; | |
665 | ASSERT(i == 1); | |
666 | ASSERT(j == 1); | |
667 | /* | |
668 | * Duplicate the cursor, search left & right | |
669 | * simultaneously. | |
670 | */ | |
0e266570 | 671 | if ((error = xfs_btree_dup_cursor(cur, &tcur))) |
2bd0ea18 NS |
672 | goto error0; |
673 | /* | |
674 | * Search left with tcur, back up 1 record. | |
675 | */ | |
0e266570 | 676 | if ((error = xfs_inobt_decrement(tcur, 0, &i))) |
2bd0ea18 NS |
677 | goto error1; |
678 | doneleft = !i; | |
679 | if (!doneleft) { | |
0e266570 | 680 | if ((error = xfs_inobt_get_rec(tcur, |
2bd0ea18 NS |
681 | &trec.ir_startino, |
682 | &trec.ir_freecount, | |
46eca962 | 683 | &trec.ir_free, &i))) |
2bd0ea18 NS |
684 | goto error1; |
685 | XFS_WANT_CORRUPTED_GOTO(i == 1, error1); | |
686 | } | |
5000d01d | 687 | /* |
2bd0ea18 NS |
688 | * Search right with cur, go forward 1 record. |
689 | */ | |
0e266570 | 690 | if ((error = xfs_inobt_increment(cur, 0, &i))) |
2bd0ea18 NS |
691 | goto error1; |
692 | doneright = !i; | |
693 | if (!doneright) { | |
0e266570 | 694 | if ((error = xfs_inobt_get_rec(cur, |
2bd0ea18 NS |
695 | &rec.ir_startino, |
696 | &rec.ir_freecount, | |
46eca962 | 697 | &rec.ir_free, &i))) |
2bd0ea18 NS |
698 | goto error1; |
699 | XFS_WANT_CORRUPTED_GOTO(i == 1, error1); | |
700 | } | |
701 | /* | |
702 | * Loop until we find the closest inode chunk | |
703 | * with a free one. | |
704 | */ | |
705 | while (!doneleft || !doneright) { | |
706 | int useleft; /* using left inode | |
707 | chunk this time */ | |
708 | ||
709 | /* | |
710 | * Figure out which block is closer, | |
711 | * if both are valid. | |
712 | */ | |
713 | if (!doneleft && !doneright) | |
714 | useleft = | |
715 | pagino - | |
716 | (trec.ir_startino + | |
717 | XFS_INODES_PER_CHUNK - 1) < | |
718 | rec.ir_startino - pagino; | |
719 | else | |
720 | useleft = !doneleft; | |
721 | /* | |
722 | * If checking the left, does it have | |
723 | * free inodes? | |
724 | */ | |
725 | if (useleft && trec.ir_freecount) { | |
726 | /* | |
727 | * Yes, set it up as the chunk to use. | |
728 | */ | |
729 | rec = trec; | |
730 | xfs_btree_del_cursor(cur, | |
731 | XFS_BTREE_NOERROR); | |
732 | cur = tcur; | |
733 | break; | |
734 | } | |
735 | /* | |
736 | * If checking the right, does it have | |
737 | * free inodes? | |
738 | */ | |
739 | if (!useleft && rec.ir_freecount) { | |
740 | /* | |
741 | * Yes, it's already set up. | |
742 | */ | |
743 | xfs_btree_del_cursor(tcur, | |
744 | XFS_BTREE_NOERROR); | |
745 | break; | |
746 | } | |
747 | /* | |
748 | * If used the left, get another one | |
749 | * further left. | |
750 | */ | |
751 | if (useleft) { | |
0e266570 NS |
752 | if ((error = xfs_inobt_decrement(tcur, 0, |
753 | &i))) | |
2bd0ea18 NS |
754 | goto error1; |
755 | doneleft = !i; | |
756 | if (!doneleft) { | |
0e266570 | 757 | if ((error = xfs_inobt_get_rec( |
2bd0ea18 NS |
758 | tcur, |
759 | &trec.ir_startino, | |
760 | &trec.ir_freecount, | |
46eca962 | 761 | &trec.ir_free, &i))) |
2bd0ea18 NS |
762 | goto error1; |
763 | XFS_WANT_CORRUPTED_GOTO(i == 1, | |
764 | error1); | |
765 | } | |
766 | } | |
767 | /* | |
768 | * If used the right, get another one | |
769 | * further right. | |
770 | */ | |
771 | else { | |
0e266570 NS |
772 | if ((error = xfs_inobt_increment(cur, 0, |
773 | &i))) | |
2bd0ea18 NS |
774 | goto error1; |
775 | doneright = !i; | |
776 | if (!doneright) { | |
0e266570 | 777 | if ((error = xfs_inobt_get_rec( |
2bd0ea18 NS |
778 | cur, |
779 | &rec.ir_startino, | |
780 | &rec.ir_freecount, | |
46eca962 | 781 | &rec.ir_free, &i))) |
2bd0ea18 NS |
782 | goto error1; |
783 | XFS_WANT_CORRUPTED_GOTO(i == 1, | |
784 | error1); | |
785 | } | |
786 | } | |
787 | } | |
788 | ASSERT(!doneleft || !doneright); | |
789 | } | |
790 | } | |
791 | /* | |
792 | * In a different a.g. from the parent. | |
793 | * See if the most recently allocated block has any free. | |
794 | */ | |
6e3140c7 | 795 | else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) { |
0e266570 | 796 | if ((error = xfs_inobt_lookup_eq(cur, |
6e3140c7 | 797 | be32_to_cpu(agi->agi_newino), 0, 0, &i))) |
2bd0ea18 NS |
798 | goto error0; |
799 | if (i == 1 && | |
800 | (error = xfs_inobt_get_rec(cur, &rec.ir_startino, | |
46eca962 | 801 | &rec.ir_freecount, &rec.ir_free, &j)) == 0 && |
2bd0ea18 NS |
802 | j == 1 && |
803 | rec.ir_freecount > 0) { | |
804 | /* | |
805 | * The last chunk allocated in the group still has | |
806 | * a free inode. | |
807 | */ | |
808 | } | |
809 | /* | |
810 | * None left in the last group, search the whole a.g. | |
811 | */ | |
812 | else { | |
813 | if (error) | |
814 | goto error0; | |
0e266570 | 815 | if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) |
2bd0ea18 NS |
816 | goto error0; |
817 | ASSERT(i == 1); | |
818 | for (;;) { | |
0e266570 | 819 | if ((error = xfs_inobt_get_rec(cur, |
2bd0ea18 NS |
820 | &rec.ir_startino, |
821 | &rec.ir_freecount, &rec.ir_free, | |
46eca962 | 822 | &i))) |
2bd0ea18 NS |
823 | goto error0; |
824 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
825 | if (rec.ir_freecount > 0) | |
826 | break; | |
0e266570 | 827 | if ((error = xfs_inobt_increment(cur, 0, &i))) |
2bd0ea18 NS |
828 | goto error0; |
829 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
830 | } | |
831 | } | |
832 | } | |
833 | offset = XFS_IALLOC_FIND_FREE(&rec.ir_free); | |
834 | ASSERT(offset >= 0); | |
835 | ASSERT(offset < XFS_INODES_PER_CHUNK); | |
836 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % | |
837 | XFS_INODES_PER_CHUNK) == 0); | |
838 | ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); | |
b34acbba | 839 | XFS_INOBT_CLR_FREE(&rec, offset); |
2bd0ea18 | 840 | rec.ir_freecount--; |
0e266570 NS |
841 | if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, |
842 | rec.ir_free))) | |
2bd0ea18 | 843 | goto error0; |
6e3140c7 | 844 | be32_add(&agi->agi_freecount, -1); |
2bd0ea18 | 845 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
e56fcdce | 846 | down_read(&mp->m_peraglock); |
2bd0ea18 | 847 | mp->m_perag[tagno].pagi_freecount--; |
e56fcdce | 848 | up_read(&mp->m_peraglock); |
2bd0ea18 NS |
849 | #ifdef DEBUG |
850 | if (cur->bc_nlevels == 1) { | |
851 | int freecount = 0; | |
852 | ||
0e266570 | 853 | if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) |
2bd0ea18 NS |
854 | goto error0; |
855 | do { | |
0e266570 | 856 | if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, |
46eca962 | 857 | &rec.ir_freecount, &rec.ir_free, &i))) |
2bd0ea18 NS |
858 | goto error0; |
859 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
860 | freecount += rec.ir_freecount; | |
0e266570 | 861 | if ((error = xfs_inobt_increment(cur, 0, &i))) |
2bd0ea18 NS |
862 | goto error0; |
863 | } while (i == 1); | |
6e3140c7 | 864 | ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || |
2bd0ea18 NS |
865 | XFS_FORCED_SHUTDOWN(mp)); |
866 | } | |
867 | #endif | |
868 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
869 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); | |
870 | *inop = ino; | |
871 | return 0; | |
872 | error1: | |
873 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); | |
874 | error0: | |
875 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
876 | return error; | |
877 | } | |
878 | ||
879 | ||
880 | /* | |
881 | * Return the location of the inode in bno/off, for mapping it into a buffer. | |
882 | */ | |
883 | /*ARGSUSED*/ | |
884 | int | |
885 | xfs_dilocate( | |
886 | xfs_mount_t *mp, /* file system mount structure */ | |
887 | xfs_trans_t *tp, /* transaction pointer */ | |
888 | xfs_ino_t ino, /* inode to locate */ | |
889 | xfs_fsblock_t *bno, /* output: block containing inode */ | |
890 | int *len, /* output: num blocks in inode cluster */ | |
891 | int *off, /* output: index in block of inode */ | |
5000d01d | 892 | uint flags) /* flags concerning inode lookup */ |
2bd0ea18 NS |
893 | { |
894 | xfs_agblock_t agbno; /* block number of inode in the alloc group */ | |
895 | xfs_buf_t *agbp; /* agi buffer */ | |
896 | xfs_agino_t agino; /* inode number within alloc group */ | |
897 | xfs_agnumber_t agno; /* allocation group number */ | |
898 | int blks_per_cluster; /* num blocks per inode cluster */ | |
899 | xfs_agblock_t chunk_agbno; /* first block in inode chunk */ | |
900 | xfs_agino_t chunk_agino; /* first agino in inode chunk */ | |
901 | __int32_t chunk_cnt; /* count of free inodes in chunk */ | |
902 | xfs_inofree_t chunk_free; /* mask of free inodes in chunk */ | |
903 | xfs_agblock_t cluster_agbno; /* first block in inode cluster */ | |
dfc130f3 | 904 | xfs_btree_cur_t *cur; /* inode btree cursor */ |
2bd0ea18 NS |
905 | int error; /* error code */ |
906 | int i; /* temp state */ | |
dfc130f3 | 907 | int offset; /* index of inode in its buffer */ |
2bd0ea18 NS |
908 | int offset_agbno; /* blks from chunk start to inode */ |
909 | ||
910 | ASSERT(ino != NULLFSINO); | |
911 | /* | |
912 | * Split up the inode number into its parts. | |
913 | */ | |
914 | agno = XFS_INO_TO_AGNO(mp, ino); | |
915 | agino = XFS_INO_TO_AGINO(mp, ino); | |
916 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); | |
917 | if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || | |
63518810 NS |
918 | ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
919 | #ifdef DEBUG | |
5000d01d | 920 | if (agno >= mp->m_sb.sb_agcount) { |
63518810 NS |
921 | xfs_fs_cmn_err(CE_ALERT, mp, |
922 | "xfs_dilocate: agno (%d) >= " | |
923 | "mp->m_sb.sb_agcount (%d)", | |
924 | agno, mp->m_sb.sb_agcount); | |
925 | } | |
926 | if (agbno >= mp->m_sb.sb_agblocks) { | |
927 | xfs_fs_cmn_err(CE_ALERT, mp, | |
928 | "xfs_dilocate: agbno (0x%llx) >= " | |
3ded4129 NS |
929 | "mp->m_sb.sb_agblocks (0x%lx)", |
930 | (unsigned long long) agbno, | |
931 | (unsigned long) mp->m_sb.sb_agblocks); | |
63518810 NS |
932 | } |
933 | if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) { | |
934 | xfs_fs_cmn_err(CE_ALERT, mp, | |
935 | "xfs_dilocate: ino (0x%llx) != " | |
936 | "XFS_AGINO_TO_INO(mp, agno, agino) " | |
937 | "(0x%llx)", | |
938 | ino, XFS_AGINO_TO_INO(mp, agno, agino)); | |
939 | } | |
940 | #endif /* DEBUG */ | |
2bd0ea18 | 941 | return XFS_ERROR(EINVAL); |
63518810 | 942 | } |
2bd0ea18 NS |
943 | if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) || |
944 | !(flags & XFS_IMAP_LOOKUP)) { | |
945 | offset = XFS_INO_TO_OFFSET(mp, ino); | |
946 | ASSERT(offset < mp->m_sb.sb_inopblock); | |
947 | *bno = XFS_AGB_TO_FSB(mp, agno, agbno); | |
948 | *off = offset; | |
949 | *len = 1; | |
950 | return 0; | |
951 | } | |
952 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog; | |
953 | if (*bno != NULLFSBLOCK) { | |
954 | offset = XFS_INO_TO_OFFSET(mp, ino); | |
955 | ASSERT(offset < mp->m_sb.sb_inopblock); | |
956 | cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno); | |
957 | *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + | |
958 | offset; | |
959 | *len = blks_per_cluster; | |
960 | return 0; | |
961 | } | |
962 | if (mp->m_inoalign_mask) { | |
963 | offset_agbno = agbno & mp->m_inoalign_mask; | |
964 | chunk_agbno = agbno - offset_agbno; | |
965 | } else { | |
e56fcdce | 966 | down_read(&mp->m_peraglock); |
2bd0ea18 | 967 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); |
e56fcdce | 968 | up_read(&mp->m_peraglock); |
63518810 NS |
969 | if (error) { |
970 | #ifdef DEBUG | |
971 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " | |
972 | "xfs_ialloc_read_agi() returned " | |
973 | "error %d, agno %d", | |
974 | error, agno); | |
975 | #endif /* DEBUG */ | |
2bd0ea18 | 976 | return error; |
63518810 | 977 | } |
2bd0ea18 NS |
978 | cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO, |
979 | (xfs_inode_t *)0, 0); | |
63518810 NS |
980 | if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) { |
981 | #ifdef DEBUG | |
982 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " | |
983 | "xfs_inobt_lookup_le() failed"); | |
984 | #endif /* DEBUG */ | |
2bd0ea18 | 985 | goto error0; |
63518810 | 986 | } |
0e266570 | 987 | if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt, |
46eca962 | 988 | &chunk_free, &i))) { |
63518810 NS |
989 | #ifdef DEBUG |
990 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " | |
991 | "xfs_inobt_get_rec() failed"); | |
992 | #endif /* DEBUG */ | |
2bd0ea18 | 993 | goto error0; |
63518810 NS |
994 | } |
995 | if (i == 0) { | |
996 | #ifdef DEBUG | |
997 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " | |
998 | "xfs_inobt_get_rec() failed"); | |
999 | #endif /* DEBUG */ | |
2bd0ea18 | 1000 | error = XFS_ERROR(EINVAL); |
63518810 | 1001 | } |
2bd0ea18 | 1002 | xfs_trans_brelse(tp, agbp); |
5000d01d | 1003 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
2bd0ea18 NS |
1004 | if (error) |
1005 | return error; | |
1006 | chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino); | |
1007 | offset_agbno = agbno - chunk_agbno; | |
1008 | } | |
1009 | ASSERT(agbno >= chunk_agbno); | |
1010 | cluster_agbno = chunk_agbno + | |
1011 | ((offset_agbno / blks_per_cluster) * blks_per_cluster); | |
1012 | offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + | |
1013 | XFS_INO_TO_OFFSET(mp, ino); | |
1014 | *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno); | |
1015 | *off = offset; | |
1016 | *len = blks_per_cluster; | |
1017 | return 0; | |
1018 | error0: | |
1019 | xfs_trans_brelse(tp, agbp); | |
1020 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
1021 | return error; | |
1022 | } | |
1023 | ||
1024 | /* | |
1025 | * Compute and fill in value of m_in_maxlevels. | |
1026 | */ | |
1027 | void | |
1028 | xfs_ialloc_compute_maxlevels( | |
1029 | xfs_mount_t *mp) /* file system mount structure */ | |
1030 | { | |
1031 | int level; | |
1032 | uint maxblocks; | |
1033 | uint maxleafents; | |
1034 | int minleafrecs; | |
1035 | int minnoderecs; | |
1036 | ||
1037 | maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >> | |
1038 | XFS_INODES_PER_CHUNK_LOG; | |
1039 | minleafrecs = mp->m_alloc_mnr[0]; | |
1040 | minnoderecs = mp->m_alloc_mnr[1]; | |
1041 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; | |
1042 | for (level = 1; maxblocks > 1; level++) | |
1043 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; | |
1044 | mp->m_in_maxlevels = level; | |
1045 | } | |
1046 | ||
1047 | /* | |
1048 | * Log specified fields for the ag hdr (inode section) | |
1049 | */ | |
1050 | void | |
1051 | xfs_ialloc_log_agi( | |
1052 | xfs_trans_t *tp, /* transaction pointer */ | |
1053 | xfs_buf_t *bp, /* allocation group header buffer */ | |
1054 | int fields) /* bitmask of fields to log */ | |
1055 | { | |
1056 | int first; /* first byte number */ | |
1057 | int last; /* last byte number */ | |
1058 | static const short offsets[] = { /* field starting offsets */ | |
1059 | /* keep in sync with bit definitions */ | |
1060 | offsetof(xfs_agi_t, agi_magicnum), | |
1061 | offsetof(xfs_agi_t, agi_versionnum), | |
1062 | offsetof(xfs_agi_t, agi_seqno), | |
1063 | offsetof(xfs_agi_t, agi_length), | |
1064 | offsetof(xfs_agi_t, agi_count), | |
1065 | offsetof(xfs_agi_t, agi_root), | |
1066 | offsetof(xfs_agi_t, agi_level), | |
1067 | offsetof(xfs_agi_t, agi_freecount), | |
1068 | offsetof(xfs_agi_t, agi_newino), | |
1069 | offsetof(xfs_agi_t, agi_dirino), | |
1070 | offsetof(xfs_agi_t, agi_unlinked), | |
1071 | sizeof(xfs_agi_t) | |
1072 | }; | |
1073 | #ifdef DEBUG | |
1074 | xfs_agi_t *agi; /* allocation group header */ | |
1075 | ||
1076 | agi = XFS_BUF_TO_AGI(bp); | |
6e3140c7 | 1077 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
2bd0ea18 NS |
1078 | #endif |
1079 | /* | |
1080 | * Compute byte offsets for the first and last fields. | |
1081 | */ | |
1082 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last); | |
1083 | /* | |
1084 | * Log the allocation group inode header buffer. | |
1085 | */ | |
1086 | xfs_trans_log_buf(tp, bp, first, last); | |
1087 | } | |
1088 | ||
1089 | /* | |
1090 | * Read in the allocation group header (inode allocation section) | |
1091 | */ | |
1092 | int | |
1093 | xfs_ialloc_read_agi( | |
1094 | xfs_mount_t *mp, /* file system mount structure */ | |
1095 | xfs_trans_t *tp, /* transaction pointer */ | |
1096 | xfs_agnumber_t agno, /* allocation group number */ | |
1097 | xfs_buf_t **bpp) /* allocation group hdr buf */ | |
1098 | { | |
1099 | xfs_agi_t *agi; /* allocation group header */ | |
1100 | int agi_ok; /* agi is consistent */ | |
1101 | xfs_buf_t *bp; /* allocation group hdr buf */ | |
2bd0ea18 | 1102 | xfs_perag_t *pag; /* per allocation group data */ |
9440d84d | 1103 | int error; |
2bd0ea18 NS |
1104 | |
1105 | ASSERT(agno != NULLAGNUMBER); | |
9440d84d NS |
1106 | error = xfs_trans_read_buf( |
1107 | mp, tp, mp->m_ddev_targp, | |
1108 | XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), | |
1109 | XFS_FSS_TO_BB(mp, 1), 0, &bp); | |
1110 | if (error) | |
2bd0ea18 NS |
1111 | return error; |
1112 | ASSERT(bp && !XFS_BUF_GETERROR(bp)); | |
9440d84d | 1113 | |
2bd0ea18 NS |
1114 | /* |
1115 | * Validate the magic number of the agi block. | |
1116 | */ | |
1117 | agi = XFS_BUF_TO_AGI(bp); | |
1118 | agi_ok = | |
6e3140c7 NS |
1119 | be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC && |
1120 | XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)); | |
4ca431fc NS |
1121 | if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI, |
1122 | XFS_RANDOM_IALLOC_READ_AGI))) { | |
1123 | XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW, | |
1124 | mp, agi); | |
2bd0ea18 NS |
1125 | xfs_trans_brelse(tp, bp); |
1126 | return XFS_ERROR(EFSCORRUPTED); | |
1127 | } | |
1128 | pag = &mp->m_perag[agno]; | |
1129 | if (!pag->pagi_init) { | |
6e3140c7 | 1130 | pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); |
cdded3d8 | 1131 | pag->pagi_count = be32_to_cpu(agi->agi_count); |
2bd0ea18 NS |
1132 | pag->pagi_init = 1; |
1133 | } else { | |
1134 | /* | |
1135 | * It's possible for these to be out of sync if | |
1136 | * we are in the middle of a forced shutdown. | |
1137 | */ | |
6e3140c7 | 1138 | ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || |
cdded3d8 | 1139 | pag->pagi_count == be32_to_cpu(agi->agi_count) || |
6e3140c7 | 1140 | XFS_FORCED_SHUTDOWN(mp)); |
2bd0ea18 | 1141 | } |
9440d84d | 1142 | |
2bd0ea18 | 1143 | #ifdef DEBUG |
9440d84d NS |
1144 | { |
1145 | int i; | |
1146 | ||
1147 | for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) | |
46eca962 | 1148 | ASSERT(agi->agi_unlinked[i]); |
9440d84d | 1149 | } |
2bd0ea18 | 1150 | #endif |
9440d84d | 1151 | |
2bd0ea18 NS |
1152 | XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF); |
1153 | *bpp = bp; | |
1154 | return 0; | |
1155 | } | |
cdded3d8 DC |
1156 | |
1157 | /* | |
1158 | * Read in the agi to initialise the per-ag data in the mount structure | |
1159 | */ | |
1160 | int | |
1161 | xfs_ialloc_pagi_init( | |
1162 | xfs_mount_t *mp, /* file system mount structure */ | |
1163 | xfs_trans_t *tp, /* transaction pointer */ | |
1164 | xfs_agnumber_t agno) /* allocation group number */ | |
1165 | { | |
1166 | xfs_buf_t *bp = NULL; | |
1167 | int error; | |
1168 | ||
1169 | if ((error = xfs_ialloc_read_agi(mp, tp, agno, &bp))) | |
1170 | return error; | |
1171 | if (bp) | |
1172 | xfs_trans_brelse(tp, bp); | |
1173 | return 0; | |
1174 | } |