]>
Commit | Line | Data |
---|---|---|
2bd0ea18 NS |
1 | /* |
2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. | |
3 | * | |
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. | |
7 | * | |
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. | |
11 | * | |
12 | * Further, this software is distributed without any warranty that it is | |
13 | * free of the rightful claim of any third person regarding infringement | |
14 | * or the like. Any license provided herein, whether implied or | |
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. | |
18 | * | |
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. | |
22 | * | |
23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, | |
24 | * Mountain View, CA 94043, or: | |
25 | * | |
26 | * http://www.sgi.com | |
27 | * | |
28 | * For further information regarding this notice, see: | |
29 | * | |
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 | }; | |
84 | ||
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 */ | |
117 | xfs_alloc_arg_t args; /* allocation argument structure */ | |
118 | int blks_per_cluster; /* fs blocks per inode cluster */ | |
119 | xfs_btree_cur_t *cur; /* inode btree cursor */ | |
120 | xfs_daddr_t d; /* disk addr of buffer */ | |
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 */ | |
135 | xfs_dinode_core_t dic; /* a dinode_core to copy to new */ | |
136 | /* inodes */ | |
137 | ||
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. | |
154 | * If the cluster size is smaller than a filesystem block | |
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) && | |
164 | args.mp->m_sb.sb_inoalignmt >= | |
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; | |
182 | args.mod = args.total = args.wasdel = args.isfl = args.userdata = | |
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; | |
189 | if (error = xfs_alloc_vextent(&args)) | |
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) && | |
202 | args.mp->m_sb.sb_inoalignmt >= | |
203 | XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp))) | |
204 | args.alignment = args.mp->m_sb.sb_inoalignmt; | |
205 | else | |
206 | args.alignment = 1; | |
207 | if (error = xfs_alloc_vextent(&args)) | |
208 | return error; | |
209 | } | |
210 | ||
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) / | |
231 | args.mp->m_sb.sb_blocksize; | |
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); | |
256 | ASSERT(!XFS_BUF_GETERROR(fbuf)); | |
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); | |
269 | bzero(&(dic.di_pad[0]),sizeof(dic.di_pad)); | |
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); | |
272 | ||
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); | |
275 | ||
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); | |
278 | ||
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); | |
290 | ||
291 | for (i = 0; i < ninodes; i++) { | |
292 | free = XFS_MAKE_IPTR(args.mp, fbuf, i); | |
293 | bcopy (&dic, &(free->di_core), sizeof(xfs_dinode_core_t)); | |
294 | INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO); | |
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); | |
302 | mraccess(&args.mp->m_peraglock); | |
303 | args.mp->m_perag[INT_GET(agi->agi_seqno, ARCH_CONVERT)].pagi_freecount += newlen; | |
304 | mraccunlock(&args.mp->m_peraglock); | |
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) { | |
315 | if (error = xfs_inobt_lookup_eq(cur, thisino, | |
316 | XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i)) { | |
317 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
318 | return error; | |
319 | } | |
320 | ASSERT(i == 0); | |
321 | if (error = xfs_inobt_insert(cur, &i)) { | |
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 | ||
342 | /* | |
343 | * Select an allocation group to look for a free inode in, based on the parent | |
344 | * inode and then mode. Return the allocation group buffer. | |
345 | */ | |
346 | STATIC xfs_buf_t * /* allocation group buffer */ | |
347 | xfs_ialloc_ag_select( | |
348 | xfs_trans_t *tp, /* transaction pointer */ | |
349 | xfs_ino_t parent, /* parent directory inode number */ | |
350 | mode_t mode, /* bits set to indicate file type */ | |
351 | int okalloc) /* ok to allocate more space */ | |
352 | { | |
353 | xfs_buf_t *agbp; /* allocation group header buffer */ | |
354 | xfs_agnumber_t agcount; /* number of ag's in the filesystem */ | |
355 | xfs_agnumber_t agno; /* current ag number */ | |
356 | int flags; /* alloc buffer locking flags */ | |
357 | xfs_extlen_t ineed; /* blocks needed for inode allocation */ | |
358 | xfs_extlen_t longest; /* longest extent available */ | |
359 | xfs_mount_t *mp; /* mount point structure */ | |
360 | int needspace; /* file mode implies space allocated */ | |
361 | xfs_perag_t *pag; /* per allocation group data */ | |
362 | xfs_agnumber_t pagno; /* parent (starting) ag number */ | |
363 | ||
364 | /* | |
365 | * Files of these types need at least one block if length > 0 | |
366 | * (and they won't fit in the inode, but that's hard to figure out). | |
367 | */ | |
368 | needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); | |
369 | mp = tp->t_mountp; | |
370 | agcount = mp->m_sb.sb_agcount; | |
371 | if (S_ISDIR(mode)) | |
372 | pagno = atomicIncWithWrap((int *)&mp->m_agirotor, agcount); | |
373 | else | |
374 | pagno = XFS_INO_TO_AGNO(mp, parent); | |
375 | ASSERT(pagno < agcount); | |
376 | /* | |
377 | * Loop through allocation groups, looking for one with a little | |
378 | * free space in it. Note we don't look for free inodes, exactly. | |
379 | * Instead, we include whether there is a need to allocate inodes | |
380 | * to mean that blocks must be allocated for them, | |
381 | * if none are currently free. | |
382 | */ | |
383 | agno = pagno; | |
384 | flags = XFS_ALLOC_FLAG_TRYLOCK; | |
385 | for (;;) { | |
386 | mraccess(&mp->m_peraglock); | |
387 | pag = &mp->m_perag[agno]; | |
388 | if (!pag->pagi_init) { | |
389 | if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { | |
390 | agbp = NULL; | |
391 | mraccunlock(&mp->m_peraglock); | |
392 | goto nextag; | |
393 | } | |
394 | } else | |
395 | agbp = NULL; | |
396 | /* | |
397 | * Is there enough free space for the file plus a block | |
398 | * of inodes (if we need to allocate some)? | |
399 | */ | |
400 | ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp); | |
401 | if (ineed && !pag->pagf_init) { | |
402 | if (agbp == NULL && | |
403 | xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { | |
404 | agbp = NULL; | |
405 | mraccunlock(&mp->m_peraglock); | |
406 | goto nextag; | |
407 | } | |
408 | (void)xfs_alloc_pagf_init(mp, tp, agno, flags); | |
409 | } | |
410 | if (!ineed || pag->pagf_init) { | |
411 | if (ineed && !(longest = pag->pagf_longest)) | |
412 | longest = pag->pagf_flcount > 0; | |
413 | if (!ineed || | |
414 | (pag->pagf_freeblks >= needspace + ineed && | |
415 | longest >= ineed && | |
416 | okalloc)) { | |
417 | if (agbp == NULL && | |
418 | xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { | |
419 | agbp = NULL; | |
420 | mraccunlock(&mp->m_peraglock); | |
421 | goto nextag; | |
422 | } | |
423 | mraccunlock(&mp->m_peraglock); | |
424 | return agbp; | |
425 | } | |
426 | } | |
427 | mraccunlock(&mp->m_peraglock); | |
428 | if (agbp) | |
429 | xfs_trans_brelse(tp, agbp); | |
430 | nextag: | |
431 | /* | |
432 | * No point in iterating over the rest, if we're shutting | |
433 | * down. | |
434 | */ | |
435 | if (XFS_FORCED_SHUTDOWN(mp)) | |
436 | return (xfs_buf_t *)0; | |
437 | agno++; | |
438 | if (agno == agcount) | |
439 | agno = 0; | |
440 | if (agno == pagno) { | |
441 | if (flags == 0) | |
442 | return (xfs_buf_t *)0; | |
443 | flags = 0; | |
444 | } | |
445 | } | |
446 | } | |
447 | ||
448 | /* | |
449 | * Visible inode allocation functions. | |
450 | */ | |
451 | ||
452 | /* | |
453 | * Allocate an inode on disk. | |
454 | * Mode is used to tell whether the new inode will need space, and whether | |
455 | * it is a directory. | |
456 | * | |
457 | * The arguments IO_agbp and alloc_done are defined to work within | |
458 | * the constraint of one allocation per transaction. | |
459 | * xfs_dialloc() is designed to be called twice if it has to do an | |
460 | * allocation to make more free inodes. On the first call, | |
461 | * IO_agbp should be set to NULL. If an inode is available, | |
462 | * i.e., xfs_dialloc() did not need to do an allocation, an inode | |
463 | * number is returned. In this case, IO_agbp would be set to the | |
464 | * current ag_buf and alloc_done set to false. | |
465 | * If an allocation needed to be done, xfs_dialloc would return | |
466 | * the current ag_buf in IO_agbp and set alloc_done to true. | |
467 | * The caller should then commit the current transaction, allocate a new | |
468 | * transaction, and call xfs_dialloc() again, passing in the previous | |
469 | * value of IO_agbp. IO_agbp should be held across the transactions. | |
470 | * Since the agbp is locked across the two calls, the second call is | |
471 | * guaranteed to have a free inode available. | |
472 | * | |
473 | * Once we successfully pick an inode its number is returned and the | |
474 | * on-disk data structures are updated. The inode itself is not read | |
475 | * in, since doing so would break ordering constraints with xfs_reclaim. | |
476 | */ | |
477 | int | |
478 | xfs_dialloc( | |
479 | xfs_trans_t *tp, /* transaction pointer */ | |
480 | xfs_ino_t parent, /* parent inode (directory) */ | |
481 | mode_t mode, /* mode bits for new inode */ | |
482 | int okalloc, /* ok to allocate more space */ | |
483 | xfs_buf_t **IO_agbp, /* in/out ag header's buffer */ | |
484 | boolean_t *alloc_done, /* true if we needed to replenish | |
485 | inode freelist */ | |
486 | xfs_ino_t *inop) /* inode number allocated */ | |
487 | { | |
488 | xfs_agnumber_t agcount; /* number of allocation groups */ | |
489 | xfs_buf_t *agbp; /* allocation group header's buffer */ | |
490 | xfs_agnumber_t agno; /* allocation group number */ | |
491 | xfs_agi_t *agi; /* allocation group header structure */ | |
492 | xfs_btree_cur_t *cur; /* inode allocation btree cursor */ | |
493 | int error; /* error return value */ | |
494 | int i; /* result code */ | |
495 | int ialloced; /* inode allocation status */ | |
496 | int noroom = 0; /* no space for inode blk allocation */ | |
497 | xfs_ino_t ino; /* fs-relative inode to be returned */ | |
498 | /* REFERENCED */ | |
499 | int j; /* result code */ | |
500 | xfs_mount_t *mp; /* file system mount structure */ | |
501 | int offset; /* index of inode in chunk */ | |
502 | xfs_agino_t pagino; /* parent's a.g. relative inode # */ | |
503 | xfs_agnumber_t pagno; /* parent's allocation group number */ | |
504 | xfs_inobt_rec_t rec; /* inode allocation record */ | |
505 | xfs_agnumber_t tagno; /* testing allocation group number */ | |
506 | xfs_btree_cur_t *tcur; /* temp cursor */ | |
507 | xfs_inobt_rec_t trec; /* temp inode allocation record */ | |
508 | ||
509 | ||
510 | if (*IO_agbp == NULL) { | |
511 | /* | |
512 | * We do not have an agbp, so select an initial allocation | |
513 | * group for inode allocation. | |
514 | */ | |
515 | agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc); | |
516 | /* | |
517 | * Couldn't find an allocation group satisfying the | |
518 | * criteria, give up. | |
519 | */ | |
520 | if (!agbp) { | |
521 | *inop = NULLFSINO; | |
522 | return 0; | |
523 | } | |
524 | agi = XFS_BUF_TO_AGI(agbp); | |
525 | ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC); | |
526 | } else { | |
527 | /* | |
528 | * Continue where we left off before. In this case, we | |
529 | * know that the allocation group has free inodes. | |
530 | */ | |
531 | agbp = *IO_agbp; | |
532 | agi = XFS_BUF_TO_AGI(agbp); | |
533 | ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC); | |
534 | ASSERT(INT_GET(agi->agi_freecount, ARCH_CONVERT) > 0); | |
535 | } | |
536 | mp = tp->t_mountp; | |
537 | agcount = mp->m_sb.sb_agcount; | |
538 | agno = INT_GET(agi->agi_seqno, ARCH_CONVERT); | |
539 | tagno = agno; | |
540 | pagno = XFS_INO_TO_AGNO(mp, parent); | |
541 | pagino = XFS_INO_TO_AGINO(mp, parent); | |
542 | ||
543 | /* | |
544 | * If we have already hit the ceiling of inode blocks then clear | |
545 | * okalloc so we scan all available agi structures for a free | |
546 | * inode. | |
547 | */ | |
548 | ||
549 | if (mp->m_maxicount && | |
550 | mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) { | |
551 | noroom = 1; | |
552 | okalloc = 0; | |
553 | } | |
554 | ||
555 | /* | |
556 | * Loop until we find an allocation group that either has free inodes | |
557 | * or in which we can allocate some inodes. Iterate through the | |
558 | * allocation groups upward, wrapping at the end. | |
559 | */ | |
560 | *alloc_done = B_FALSE; | |
561 | while (INT_GET(agi->agi_freecount, ARCH_CONVERT) == 0) { | |
562 | /* | |
563 | * Don't do anything if we're not supposed to allocate | |
564 | * any blocks, just go on to the next ag. | |
565 | */ | |
566 | if (okalloc) { | |
567 | /* | |
568 | * Try to allocate some new inodes in the allocation | |
569 | * group. | |
570 | */ | |
571 | if (error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced)) { | |
572 | xfs_trans_brelse(tp, agbp); | |
573 | if (error == ENOSPC) { | |
574 | *inop = NULLFSINO; | |
575 | return 0; | |
576 | } else | |
577 | return error; | |
578 | } | |
579 | if (ialloced) { | |
580 | /* | |
581 | * We successfully allocated some inodes, return | |
582 | * the current context to the caller so that it | |
583 | * can commit the current transaction and call | |
584 | * us again where we left off. | |
585 | */ | |
586 | ASSERT(INT_GET(agi->agi_freecount, ARCH_CONVERT) > 0); | |
587 | *alloc_done = B_TRUE; | |
588 | *IO_agbp = agbp; | |
589 | *inop = NULLFSINO; | |
590 | return 0; | |
591 | } | |
592 | } | |
593 | /* | |
594 | * If it failed, give up on this ag. | |
595 | */ | |
596 | xfs_trans_brelse(tp, agbp); | |
597 | /* | |
598 | * Go on to the next ag: get its ag header. | |
599 | */ | |
600 | nextag: | |
601 | if (++tagno == agcount) | |
602 | tagno = 0; | |
603 | if (tagno == agno) { | |
604 | *inop = NULLFSINO; | |
605 | return noroom ? ENOSPC : 0; | |
606 | } | |
607 | mraccess(&mp->m_peraglock); | |
608 | error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp); | |
609 | mraccunlock(&mp->m_peraglock); | |
610 | if (error) | |
611 | goto nextag; | |
612 | agi = XFS_BUF_TO_AGI(agbp); | |
613 | ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC); | |
614 | } | |
615 | /* | |
616 | * Here with an allocation group that has a free inode. | |
617 | * Reset agno since we may have chosen a new ag in the | |
618 | * loop above. | |
619 | */ | |
620 | agno = tagno; | |
621 | *IO_agbp = NULL; | |
622 | cur = xfs_btree_init_cursor(mp, tp, agbp, INT_GET(agi->agi_seqno, ARCH_CONVERT), | |
623 | XFS_BTNUM_INO, (xfs_inode_t *)0, 0); | |
624 | /* | |
625 | * If pagino is 0 (this is the root inode allocation) use newino. | |
626 | * This must work because we've just allocated some. | |
627 | */ | |
628 | if (!pagino) | |
629 | pagino = INT_GET(agi->agi_newino, ARCH_CONVERT); | |
630 | #ifdef DEBUG | |
631 | if (cur->bc_nlevels == 1) { | |
632 | int freecount = 0; | |
633 | ||
634 | if (error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)) | |
635 | goto error0; | |
636 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
637 | do { | |
638 | if (error = xfs_inobt_get_rec(cur, &rec.ir_startino, | |
639 | &rec.ir_freecount, &rec.ir_free, &i, ARCH_NOCONVERT)) | |
640 | goto error0; | |
641 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
642 | freecount += rec.ir_freecount; | |
643 | if (error = xfs_inobt_increment(cur, 0, &i)) | |
644 | goto error0; | |
645 | } while (i == 1); | |
646 | ||
647 | ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) || | |
648 | XFS_FORCED_SHUTDOWN(mp)); | |
649 | } | |
650 | #endif | |
651 | /* | |
652 | * If in the same a.g. as the parent, try to get near the parent. | |
653 | */ | |
654 | if (pagno == agno) { | |
655 | if (error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)) | |
656 | goto error0; | |
657 | if (i != 0 && | |
658 | (error = xfs_inobt_get_rec(cur, &rec.ir_startino, | |
659 | &rec.ir_freecount, &rec.ir_free, &j, ARCH_NOCONVERT)) == 0 && | |
660 | j == 1 && | |
661 | rec.ir_freecount > 0) { | |
662 | /* | |
663 | * Found a free inode in the same chunk | |
664 | * as parent, done. | |
665 | */ | |
666 | } | |
667 | /* | |
668 | * In the same a.g. as parent, but parent's chunk is full. | |
669 | */ | |
670 | else { | |
671 | int doneleft; /* done, to the left */ | |
672 | int doneright; /* done, to the right */ | |
673 | ||
674 | if (error) | |
675 | goto error0; | |
676 | ASSERT(i == 1); | |
677 | ASSERT(j == 1); | |
678 | /* | |
679 | * Duplicate the cursor, search left & right | |
680 | * simultaneously. | |
681 | */ | |
682 | if (error = xfs_btree_dup_cursor(cur, &tcur)) | |
683 | goto error0; | |
684 | /* | |
685 | * Search left with tcur, back up 1 record. | |
686 | */ | |
687 | if (error = xfs_inobt_decrement(tcur, 0, &i)) | |
688 | goto error1; | |
689 | doneleft = !i; | |
690 | if (!doneleft) { | |
691 | if (error = xfs_inobt_get_rec(tcur, | |
692 | &trec.ir_startino, | |
693 | &trec.ir_freecount, | |
694 | &trec.ir_free, &i, ARCH_NOCONVERT)) | |
695 | goto error1; | |
696 | XFS_WANT_CORRUPTED_GOTO(i == 1, error1); | |
697 | } | |
698 | /* | |
699 | * Search right with cur, go forward 1 record. | |
700 | */ | |
701 | if (error = xfs_inobt_increment(cur, 0, &i)) | |
702 | goto error1; | |
703 | doneright = !i; | |
704 | if (!doneright) { | |
705 | if (error = xfs_inobt_get_rec(cur, | |
706 | &rec.ir_startino, | |
707 | &rec.ir_freecount, | |
708 | &rec.ir_free, &i, ARCH_NOCONVERT)) | |
709 | goto error1; | |
710 | XFS_WANT_CORRUPTED_GOTO(i == 1, error1); | |
711 | } | |
712 | /* | |
713 | * Loop until we find the closest inode chunk | |
714 | * with a free one. | |
715 | */ | |
716 | while (!doneleft || !doneright) { | |
717 | int useleft; /* using left inode | |
718 | chunk this time */ | |
719 | ||
720 | /* | |
721 | * Figure out which block is closer, | |
722 | * if both are valid. | |
723 | */ | |
724 | if (!doneleft && !doneright) | |
725 | useleft = | |
726 | pagino - | |
727 | (trec.ir_startino + | |
728 | XFS_INODES_PER_CHUNK - 1) < | |
729 | rec.ir_startino - pagino; | |
730 | else | |
731 | useleft = !doneleft; | |
732 | /* | |
733 | * If checking the left, does it have | |
734 | * free inodes? | |
735 | */ | |
736 | if (useleft && trec.ir_freecount) { | |
737 | /* | |
738 | * Yes, set it up as the chunk to use. | |
739 | */ | |
740 | rec = trec; | |
741 | xfs_btree_del_cursor(cur, | |
742 | XFS_BTREE_NOERROR); | |
743 | cur = tcur; | |
744 | break; | |
745 | } | |
746 | /* | |
747 | * If checking the right, does it have | |
748 | * free inodes? | |
749 | */ | |
750 | if (!useleft && rec.ir_freecount) { | |
751 | /* | |
752 | * Yes, it's already set up. | |
753 | */ | |
754 | xfs_btree_del_cursor(tcur, | |
755 | XFS_BTREE_NOERROR); | |
756 | break; | |
757 | } | |
758 | /* | |
759 | * If used the left, get another one | |
760 | * further left. | |
761 | */ | |
762 | if (useleft) { | |
763 | if (error = xfs_inobt_decrement(tcur, 0, | |
764 | &i)) | |
765 | goto error1; | |
766 | doneleft = !i; | |
767 | if (!doneleft) { | |
768 | if (error = xfs_inobt_get_rec( | |
769 | tcur, | |
770 | &trec.ir_startino, | |
771 | &trec.ir_freecount, | |
772 | &trec.ir_free, &i, ARCH_NOCONVERT)) | |
773 | goto error1; | |
774 | XFS_WANT_CORRUPTED_GOTO(i == 1, | |
775 | error1); | |
776 | } | |
777 | } | |
778 | /* | |
779 | * If used the right, get another one | |
780 | * further right. | |
781 | */ | |
782 | else { | |
783 | if (error = xfs_inobt_increment(cur, 0, | |
784 | &i)) | |
785 | goto error1; | |
786 | doneright = !i; | |
787 | if (!doneright) { | |
788 | if (error = xfs_inobt_get_rec( | |
789 | cur, | |
790 | &rec.ir_startino, | |
791 | &rec.ir_freecount, | |
792 | &rec.ir_free, &i, ARCH_NOCONVERT)) | |
793 | goto error1; | |
794 | XFS_WANT_CORRUPTED_GOTO(i == 1, | |
795 | error1); | |
796 | } | |
797 | } | |
798 | } | |
799 | ASSERT(!doneleft || !doneright); | |
800 | } | |
801 | } | |
802 | /* | |
803 | * In a different a.g. from the parent. | |
804 | * See if the most recently allocated block has any free. | |
805 | */ | |
806 | else if (INT_GET(agi->agi_newino, ARCH_CONVERT) != NULLAGINO) { | |
807 | if (error = xfs_inobt_lookup_eq(cur, | |
808 | INT_GET(agi->agi_newino, ARCH_CONVERT), 0, 0, &i)) | |
809 | goto error0; | |
810 | if (i == 1 && | |
811 | (error = xfs_inobt_get_rec(cur, &rec.ir_startino, | |
812 | &rec.ir_freecount, &rec.ir_free, &j, ARCH_NOCONVERT)) == 0 && | |
813 | j == 1 && | |
814 | rec.ir_freecount > 0) { | |
815 | /* | |
816 | * The last chunk allocated in the group still has | |
817 | * a free inode. | |
818 | */ | |
819 | } | |
820 | /* | |
821 | * None left in the last group, search the whole a.g. | |
822 | */ | |
823 | else { | |
824 | if (error) | |
825 | goto error0; | |
826 | if (error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)) | |
827 | goto error0; | |
828 | ASSERT(i == 1); | |
829 | for (;;) { | |
830 | if (error = xfs_inobt_get_rec(cur, | |
831 | &rec.ir_startino, | |
832 | &rec.ir_freecount, &rec.ir_free, | |
833 | &i, ARCH_NOCONVERT)) | |
834 | goto error0; | |
835 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
836 | if (rec.ir_freecount > 0) | |
837 | break; | |
838 | if (error = xfs_inobt_increment(cur, 0, &i)) | |
839 | goto error0; | |
840 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
841 | } | |
842 | } | |
843 | } | |
844 | offset = XFS_IALLOC_FIND_FREE(&rec.ir_free); | |
845 | ASSERT(offset >= 0); | |
846 | ASSERT(offset < XFS_INODES_PER_CHUNK); | |
847 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % | |
848 | XFS_INODES_PER_CHUNK) == 0); | |
849 | ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); | |
850 | XFS_INOBT_CLR_FREE(&rec, offset, ARCH_NOCONVERT); | |
851 | rec.ir_freecount--; | |
852 | if (error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, | |
853 | rec.ir_free)) | |
854 | goto error0; | |
855 | INT_MOD(agi->agi_freecount, ARCH_CONVERT, -1); | |
856 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); | |
857 | mraccess(&mp->m_peraglock); | |
858 | mp->m_perag[tagno].pagi_freecount--; | |
859 | mraccunlock(&mp->m_peraglock); | |
860 | #ifdef DEBUG | |
861 | if (cur->bc_nlevels == 1) { | |
862 | int freecount = 0; | |
863 | ||
864 | if (error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)) | |
865 | goto error0; | |
866 | do { | |
867 | if (error = xfs_inobt_get_rec(cur, &rec.ir_startino, | |
868 | &rec.ir_freecount, &rec.ir_free, &i, ARCH_NOCONVERT)) | |
869 | goto error0; | |
870 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
871 | freecount += rec.ir_freecount; | |
872 | if (error = xfs_inobt_increment(cur, 0, &i)) | |
873 | goto error0; | |
874 | } while (i == 1); | |
875 | ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) || | |
876 | XFS_FORCED_SHUTDOWN(mp)); | |
877 | } | |
878 | #endif | |
879 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
880 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); | |
881 | *inop = ino; | |
882 | return 0; | |
883 | error1: | |
884 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); | |
885 | error0: | |
886 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
887 | return error; | |
888 | } | |
889 | ||
890 | ||
891 | /* | |
892 | * Return the location of the inode in bno/off, for mapping it into a buffer. | |
893 | */ | |
894 | /*ARGSUSED*/ | |
895 | int | |
896 | xfs_dilocate( | |
897 | xfs_mount_t *mp, /* file system mount structure */ | |
898 | xfs_trans_t *tp, /* transaction pointer */ | |
899 | xfs_ino_t ino, /* inode to locate */ | |
900 | xfs_fsblock_t *bno, /* output: block containing inode */ | |
901 | int *len, /* output: num blocks in inode cluster */ | |
902 | int *off, /* output: index in block of inode */ | |
903 | uint flags) /* flags concerning inode lookup */ | |
904 | { | |
905 | xfs_agblock_t agbno; /* block number of inode in the alloc group */ | |
906 | xfs_buf_t *agbp; /* agi buffer */ | |
907 | xfs_agino_t agino; /* inode number within alloc group */ | |
908 | xfs_agnumber_t agno; /* allocation group number */ | |
909 | int blks_per_cluster; /* num blocks per inode cluster */ | |
910 | xfs_agblock_t chunk_agbno; /* first block in inode chunk */ | |
911 | xfs_agino_t chunk_agino; /* first agino in inode chunk */ | |
912 | __int32_t chunk_cnt; /* count of free inodes in chunk */ | |
913 | xfs_inofree_t chunk_free; /* mask of free inodes in chunk */ | |
914 | xfs_agblock_t cluster_agbno; /* first block in inode cluster */ | |
915 | xfs_btree_cur_t *cur; /* inode btree cursor */ | |
916 | int error; /* error code */ | |
917 | int i; /* temp state */ | |
918 | int offset; /* index of inode in its buffer */ | |
919 | int offset_agbno; /* blks from chunk start to inode */ | |
920 | ||
921 | ASSERT(ino != NULLFSINO); | |
922 | /* | |
923 | * Split up the inode number into its parts. | |
924 | */ | |
925 | agno = XFS_INO_TO_AGNO(mp, ino); | |
926 | agino = XFS_INO_TO_AGINO(mp, ino); | |
927 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); | |
928 | if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || | |
929 | ino != XFS_AGINO_TO_INO(mp, agno, agino)) | |
930 | return XFS_ERROR(EINVAL); | |
931 | if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) || | |
932 | !(flags & XFS_IMAP_LOOKUP)) { | |
933 | offset = XFS_INO_TO_OFFSET(mp, ino); | |
934 | ASSERT(offset < mp->m_sb.sb_inopblock); | |
935 | *bno = XFS_AGB_TO_FSB(mp, agno, agbno); | |
936 | *off = offset; | |
937 | *len = 1; | |
938 | return 0; | |
939 | } | |
940 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog; | |
941 | if (*bno != NULLFSBLOCK) { | |
942 | offset = XFS_INO_TO_OFFSET(mp, ino); | |
943 | ASSERT(offset < mp->m_sb.sb_inopblock); | |
944 | cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno); | |
945 | *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + | |
946 | offset; | |
947 | *len = blks_per_cluster; | |
948 | return 0; | |
949 | } | |
950 | if (mp->m_inoalign_mask) { | |
951 | offset_agbno = agbno & mp->m_inoalign_mask; | |
952 | chunk_agbno = agbno - offset_agbno; | |
953 | } else { | |
954 | mraccess(&mp->m_peraglock); | |
955 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); | |
956 | mraccunlock(&mp->m_peraglock); | |
957 | if (error) | |
958 | return error; | |
959 | cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO, | |
960 | (xfs_inode_t *)0, 0); | |
961 | if (error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i)) | |
962 | goto error0; | |
963 | if (error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt, | |
964 | &chunk_free, &i, ARCH_NOCONVERT)) | |
965 | goto error0; | |
966 | if (i == 0) | |
967 | error = XFS_ERROR(EINVAL); | |
968 | xfs_trans_brelse(tp, agbp); | |
969 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
970 | if (error) | |
971 | return error; | |
972 | chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino); | |
973 | offset_agbno = agbno - chunk_agbno; | |
974 | } | |
975 | ASSERT(agbno >= chunk_agbno); | |
976 | cluster_agbno = chunk_agbno + | |
977 | ((offset_agbno / blks_per_cluster) * blks_per_cluster); | |
978 | offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + | |
979 | XFS_INO_TO_OFFSET(mp, ino); | |
980 | *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno); | |
981 | *off = offset; | |
982 | *len = blks_per_cluster; | |
983 | return 0; | |
984 | error0: | |
985 | xfs_trans_brelse(tp, agbp); | |
986 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
987 | return error; | |
988 | } | |
989 | ||
990 | /* | |
991 | * Compute and fill in value of m_in_maxlevels. | |
992 | */ | |
993 | void | |
994 | xfs_ialloc_compute_maxlevels( | |
995 | xfs_mount_t *mp) /* file system mount structure */ | |
996 | { | |
997 | int level; | |
998 | uint maxblocks; | |
999 | uint maxleafents; | |
1000 | int minleafrecs; | |
1001 | int minnoderecs; | |
1002 | ||
1003 | maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >> | |
1004 | XFS_INODES_PER_CHUNK_LOG; | |
1005 | minleafrecs = mp->m_alloc_mnr[0]; | |
1006 | minnoderecs = mp->m_alloc_mnr[1]; | |
1007 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; | |
1008 | for (level = 1; maxblocks > 1; level++) | |
1009 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; | |
1010 | mp->m_in_maxlevels = level; | |
1011 | } | |
1012 | ||
1013 | /* | |
1014 | * Log specified fields for the ag hdr (inode section) | |
1015 | */ | |
1016 | void | |
1017 | xfs_ialloc_log_agi( | |
1018 | xfs_trans_t *tp, /* transaction pointer */ | |
1019 | xfs_buf_t *bp, /* allocation group header buffer */ | |
1020 | int fields) /* bitmask of fields to log */ | |
1021 | { | |
1022 | int first; /* first byte number */ | |
1023 | int last; /* last byte number */ | |
1024 | static const short offsets[] = { /* field starting offsets */ | |
1025 | /* keep in sync with bit definitions */ | |
1026 | offsetof(xfs_agi_t, agi_magicnum), | |
1027 | offsetof(xfs_agi_t, agi_versionnum), | |
1028 | offsetof(xfs_agi_t, agi_seqno), | |
1029 | offsetof(xfs_agi_t, agi_length), | |
1030 | offsetof(xfs_agi_t, agi_count), | |
1031 | offsetof(xfs_agi_t, agi_root), | |
1032 | offsetof(xfs_agi_t, agi_level), | |
1033 | offsetof(xfs_agi_t, agi_freecount), | |
1034 | offsetof(xfs_agi_t, agi_newino), | |
1035 | offsetof(xfs_agi_t, agi_dirino), | |
1036 | offsetof(xfs_agi_t, agi_unlinked), | |
1037 | sizeof(xfs_agi_t) | |
1038 | }; | |
1039 | #ifdef DEBUG | |
1040 | xfs_agi_t *agi; /* allocation group header */ | |
1041 | ||
1042 | agi = XFS_BUF_TO_AGI(bp); | |
1043 | ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == | |
1044 | XFS_AGI_MAGIC); | |
1045 | #endif | |
1046 | /* | |
1047 | * Compute byte offsets for the first and last fields. | |
1048 | */ | |
1049 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last); | |
1050 | /* | |
1051 | * Log the allocation group inode header buffer. | |
1052 | */ | |
1053 | xfs_trans_log_buf(tp, bp, first, last); | |
1054 | } | |
1055 | ||
1056 | /* | |
1057 | * Read in the allocation group header (inode allocation section) | |
1058 | */ | |
1059 | int | |
1060 | xfs_ialloc_read_agi( | |
1061 | xfs_mount_t *mp, /* file system mount structure */ | |
1062 | xfs_trans_t *tp, /* transaction pointer */ | |
1063 | xfs_agnumber_t agno, /* allocation group number */ | |
1064 | xfs_buf_t **bpp) /* allocation group hdr buf */ | |
1065 | { | |
1066 | xfs_agi_t *agi; /* allocation group header */ | |
1067 | int agi_ok; /* agi is consistent */ | |
1068 | xfs_buf_t *bp; /* allocation group hdr buf */ | |
1069 | xfs_daddr_t d; /* disk block address */ | |
1070 | int error; | |
1071 | #ifdef DEBUG | |
1072 | int i; | |
1073 | #endif | |
1074 | xfs_perag_t *pag; /* per allocation group data */ | |
1075 | ||
1076 | ||
1077 | ASSERT(agno != NULLAGNUMBER); | |
1078 | d = XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR); | |
1079 | if (error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d, 1, 0, &bp)) | |
1080 | return error; | |
1081 | ASSERT(bp && !XFS_BUF_GETERROR(bp)); | |
1082 | /* | |
1083 | * Validate the magic number of the agi block. | |
1084 | */ | |
1085 | agi = XFS_BUF_TO_AGI(bp); | |
1086 | agi_ok = | |
1087 | INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC && | |
1088 | XFS_AGI_GOOD_VERSION(INT_GET(agi->agi_versionnum, ARCH_CONVERT)); | |
1089 | if (XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI, | |
1090 | XFS_RANDOM_IALLOC_READ_AGI)) { | |
1091 | xfs_trans_brelse(tp, bp); | |
1092 | return XFS_ERROR(EFSCORRUPTED); | |
1093 | } | |
1094 | pag = &mp->m_perag[agno]; | |
1095 | if (!pag->pagi_init) { | |
1096 | pag->pagi_freecount = INT_GET(agi->agi_freecount, ARCH_CONVERT); | |
1097 | pag->pagi_init = 1; | |
1098 | } else { | |
1099 | /* | |
1100 | * It's possible for these to be out of sync if | |
1101 | * we are in the middle of a forced shutdown. | |
1102 | */ | |
1103 | ASSERT(pag->pagi_freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) | |
1104 | || XFS_FORCED_SHUTDOWN(mp)); | |
1105 | } | |
1106 | #ifdef DEBUG | |
1107 | for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) | |
1108 | ASSERT(INT_GET(agi->agi_unlinked[i], ARCH_CONVERT) != 0); | |
1109 | #endif | |
1110 | XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF); | |
1111 | *bpp = bp; | |
1112 | return 0; | |
1113 | } |