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