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