]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 | 2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
f5f3d9b0 | 3 | * Copyright (c) 2013 Red Hat, Inc. |
7b718769 | 4 | * All Rights Reserved. |
1da177e4 | 5 | * |
7b718769 NS |
6 | * This program is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
8 | * published by the Free Software Foundation. |
9 | * | |
7b718769 NS |
10 | * This program is distributed in the hope that it would be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
1da177e4 | 14 | * |
7b718769 NS |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, write the Free Software Foundation, | |
17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 18 | */ |
1da177e4 | 19 | #include "xfs.h" |
a844f451 | 20 | #include "xfs_fs.h" |
1da177e4 | 21 | #include "xfs_types.h" |
1da177e4 LT |
22 | #include "xfs_log.h" |
23 | #include "xfs_trans.h" | |
24 | #include "xfs_sb.h" | |
da353b0d | 25 | #include "xfs_ag.h" |
1da177e4 | 26 | #include "xfs_mount.h" |
a844f451 | 27 | #include "xfs_da_btree.h" |
1da177e4 | 28 | #include "xfs_bmap_btree.h" |
1da177e4 LT |
29 | #include "xfs_dinode.h" |
30 | #include "xfs_inode.h" | |
57926640 | 31 | #include "xfs_dir2_format.h" |
2b9ab5ab | 32 | #include "xfs_dir2.h" |
57926640 | 33 | #include "xfs_dir2_priv.h" |
1da177e4 | 34 | #include "xfs_error.h" |
33363fee DC |
35 | #include "xfs_buf_item.h" |
36 | #include "xfs_cksum.h" | |
1da177e4 | 37 | |
1da177e4 LT |
38 | /* |
39 | * Check the consistency of the data block. | |
40 | * The input can also be a block-format directory. | |
82025d7f | 41 | * Return 0 is the buffer is good, otherwise an error. |
1da177e4 | 42 | */ |
82025d7f | 43 | int |
33363fee | 44 | __xfs_dir3_data_check( |
1d9025e5 DC |
45 | struct xfs_inode *dp, /* incore inode pointer */ |
46 | struct xfs_buf *bp) /* data block's buffer */ | |
1da177e4 LT |
47 | { |
48 | xfs_dir2_dataptr_t addr; /* addr for leaf lookup */ | |
49 | xfs_dir2_data_free_t *bf; /* bestfree table */ | |
50 | xfs_dir2_block_tail_t *btp=NULL; /* block tail */ | |
51 | int count; /* count of entries found */ | |
c2066e26 | 52 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
1da177e4 LT |
53 | xfs_dir2_data_entry_t *dep; /* data entry */ |
54 | xfs_dir2_data_free_t *dfp; /* bestfree entry */ | |
55 | xfs_dir2_data_unused_t *dup; /* unused entry */ | |
56 | char *endp; /* end of useful data */ | |
57 | int freeseen; /* mask of bestfrees seen */ | |
58 | xfs_dahash_t hash; /* hash of current name */ | |
59 | int i; /* leaf index */ | |
60 | int lastfree; /* last entry was unused */ | |
61 | xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */ | |
62 | xfs_mount_t *mp; /* filesystem mount point */ | |
63 | char *p; /* current data position */ | |
64 | int stale; /* count of stale leaves */ | |
5163f95a | 65 | struct xfs_name name; |
1da177e4 | 66 | |
82025d7f | 67 | mp = bp->b_target->bt_mount; |
1d9025e5 | 68 | hdr = bp->b_addr; |
f5f3d9b0 DC |
69 | bf = xfs_dir3_data_bestfree_p(hdr); |
70 | p = (char *)xfs_dir3_data_entry_p(hdr); | |
c2066e26 | 71 | |
82025d7f | 72 | switch (hdr->magic) { |
f5f3d9b0 | 73 | case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC): |
82025d7f | 74 | case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC): |
c2066e26 | 75 | btp = xfs_dir2_block_tail_p(mp, hdr); |
bbaaf538 | 76 | lep = xfs_dir2_block_leaf_p(btp); |
1da177e4 | 77 | endp = (char *)lep; |
82025d7f | 78 | break; |
33363fee | 79 | case cpu_to_be32(XFS_DIR3_DATA_MAGIC): |
82025d7f | 80 | case cpu_to_be32(XFS_DIR2_DATA_MAGIC): |
c2066e26 | 81 | endp = (char *)hdr + mp->m_dirblksize; |
82025d7f DC |
82 | break; |
83 | default: | |
84 | XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp); | |
85 | return EFSCORRUPTED; | |
c2066e26 CH |
86 | } |
87 | ||
1da177e4 LT |
88 | count = lastfree = freeseen = 0; |
89 | /* | |
90 | * Account for zero bestfree entries. | |
91 | */ | |
92 | if (!bf[0].length) { | |
82025d7f | 93 | XFS_WANT_CORRUPTED_RETURN(!bf[0].offset); |
1da177e4 LT |
94 | freeseen |= 1 << 0; |
95 | } | |
96 | if (!bf[1].length) { | |
82025d7f | 97 | XFS_WANT_CORRUPTED_RETURN(!bf[1].offset); |
1da177e4 LT |
98 | freeseen |= 1 << 1; |
99 | } | |
100 | if (!bf[2].length) { | |
82025d7f | 101 | XFS_WANT_CORRUPTED_RETURN(!bf[2].offset); |
1da177e4 LT |
102 | freeseen |= 1 << 2; |
103 | } | |
82025d7f DC |
104 | |
105 | XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >= | |
106 | be16_to_cpu(bf[1].length)); | |
107 | XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >= | |
108 | be16_to_cpu(bf[2].length)); | |
1da177e4 LT |
109 | /* |
110 | * Loop over the data/unused entries. | |
111 | */ | |
112 | while (p < endp) { | |
113 | dup = (xfs_dir2_data_unused_t *)p; | |
114 | /* | |
115 | * If it's unused, look for the space in the bestfree table. | |
116 | * If we find it, account for that, else make sure it | |
117 | * doesn't need to be there. | |
118 | */ | |
ad354eb3 | 119 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
82025d7f DC |
120 | XFS_WANT_CORRUPTED_RETURN(lastfree == 0); |
121 | XFS_WANT_CORRUPTED_RETURN( | |
122 | be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) == | |
123 | (char *)dup - (char *)hdr); | |
c2066e26 | 124 | dfp = xfs_dir2_data_freefind(hdr, dup); |
1da177e4 LT |
125 | if (dfp) { |
126 | i = (int)(dfp - bf); | |
82025d7f DC |
127 | XFS_WANT_CORRUPTED_RETURN( |
128 | (freeseen & (1 << i)) == 0); | |
1da177e4 | 129 | freeseen |= 1 << i; |
70e73f59 | 130 | } else { |
82025d7f DC |
131 | XFS_WANT_CORRUPTED_RETURN( |
132 | be16_to_cpu(dup->length) <= | |
133 | be16_to_cpu(bf[2].length)); | |
70e73f59 | 134 | } |
ad354eb3 | 135 | p += be16_to_cpu(dup->length); |
1da177e4 LT |
136 | lastfree = 1; |
137 | continue; | |
138 | } | |
139 | /* | |
140 | * It's a real entry. Validate the fields. | |
141 | * If this is a block directory then make sure it's | |
142 | * in the leaf section of the block. | |
143 | * The linear search is crude but this is DEBUG code. | |
144 | */ | |
145 | dep = (xfs_dir2_data_entry_t *)p; | |
82025d7f DC |
146 | XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0); |
147 | XFS_WANT_CORRUPTED_RETURN( | |
148 | !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber))); | |
149 | XFS_WANT_CORRUPTED_RETURN( | |
0cb97766 | 150 | be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep)) == |
82025d7f | 151 | (char *)dep - (char *)hdr); |
1c55cece DC |
152 | XFS_WANT_CORRUPTED_RETURN( |
153 | xfs_dir3_dirent_get_ftype(mp, dep) < XFS_DIR3_FT_MAX); | |
1da177e4 LT |
154 | count++; |
155 | lastfree = 0; | |
f5f3d9b0 DC |
156 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
157 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) { | |
bbaaf538 | 158 | addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk, |
1da177e4 | 159 | (xfs_dir2_data_aoff_t) |
c2066e26 | 160 | ((char *)dep - (char *)hdr)); |
5163f95a BN |
161 | name.name = dep->name; |
162 | name.len = dep->namelen; | |
163 | hash = mp->m_dirnameops->hashname(&name); | |
e922fffa | 164 | for (i = 0; i < be32_to_cpu(btp->count); i++) { |
3c1f9c15 NS |
165 | if (be32_to_cpu(lep[i].address) == addr && |
166 | be32_to_cpu(lep[i].hashval) == hash) | |
1da177e4 LT |
167 | break; |
168 | } | |
82025d7f | 169 | XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count)); |
1da177e4 | 170 | } |
0cb97766 | 171 | p += xfs_dir3_data_entsize(mp, dep->namelen); |
1da177e4 LT |
172 | } |
173 | /* | |
174 | * Need to have seen all the entries and all the bestfree slots. | |
175 | */ | |
82025d7f | 176 | XFS_WANT_CORRUPTED_RETURN(freeseen == 7); |
f5f3d9b0 DC |
177 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
178 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) { | |
e922fffa | 179 | for (i = stale = 0; i < be32_to_cpu(btp->count); i++) { |
69ef921b CH |
180 | if (lep[i].address == |
181 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) | |
1da177e4 LT |
182 | stale++; |
183 | if (i > 0) | |
82025d7f DC |
184 | XFS_WANT_CORRUPTED_RETURN( |
185 | be32_to_cpu(lep[i].hashval) >= | |
186 | be32_to_cpu(lep[i - 1].hashval)); | |
1da177e4 | 187 | } |
82025d7f DC |
188 | XFS_WANT_CORRUPTED_RETURN(count == |
189 | be32_to_cpu(btp->count) - be32_to_cpu(btp->stale)); | |
190 | XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale)); | |
1da177e4 | 191 | } |
82025d7f | 192 | return 0; |
1da177e4 | 193 | } |
1da177e4 | 194 | |
33363fee DC |
195 | static bool |
196 | xfs_dir3_data_verify( | |
e4813572 DC |
197 | struct xfs_buf *bp) |
198 | { | |
199 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
33363fee | 200 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
e4813572 | 201 | |
33363fee DC |
202 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
203 | if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC)) | |
204 | return false; | |
205 | if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid)) | |
206 | return false; | |
207 | if (be64_to_cpu(hdr3->blkno) != bp->b_bn) | |
208 | return false; | |
209 | } else { | |
210 | if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC)) | |
211 | return false; | |
e4813572 | 212 | } |
33363fee DC |
213 | if (__xfs_dir3_data_check(NULL, bp)) |
214 | return false; | |
215 | return true; | |
612cfbfe DC |
216 | } |
217 | ||
1813dd64 DC |
218 | /* |
219 | * Readahead of the first block of the directory when it is opened is completely | |
220 | * oblivious to the format of the directory. Hence we can either get a block | |
221 | * format buffer or a data format buffer on readahead. | |
222 | */ | |
223 | static void | |
33363fee | 224 | xfs_dir3_data_reada_verify( |
1813dd64 DC |
225 | struct xfs_buf *bp) |
226 | { | |
227 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
228 | struct xfs_dir2_data_hdr *hdr = bp->b_addr; | |
229 | ||
230 | switch (hdr->magic) { | |
231 | case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC): | |
f5f3d9b0 DC |
232 | case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC): |
233 | bp->b_ops = &xfs_dir3_block_buf_ops; | |
1813dd64 DC |
234 | bp->b_ops->verify_read(bp); |
235 | return; | |
236 | case cpu_to_be32(XFS_DIR2_DATA_MAGIC): | |
33363fee DC |
237 | case cpu_to_be32(XFS_DIR3_DATA_MAGIC): |
238 | xfs_dir3_data_verify(bp); | |
1813dd64 DC |
239 | return; |
240 | default: | |
241 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr); | |
242 | xfs_buf_ioerror(bp, EFSCORRUPTED); | |
243 | break; | |
244 | } | |
245 | } | |
246 | ||
247 | static void | |
33363fee | 248 | xfs_dir3_data_read_verify( |
612cfbfe DC |
249 | struct xfs_buf *bp) |
250 | { | |
33363fee DC |
251 | struct xfs_mount *mp = bp->b_target->bt_mount; |
252 | ||
253 | if ((xfs_sb_version_hascrc(&mp->m_sb) && | |
254 | !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length), | |
255 | XFS_DIR3_DATA_CRC_OFF)) || | |
256 | !xfs_dir3_data_verify(bp)) { | |
257 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); | |
258 | xfs_buf_ioerror(bp, EFSCORRUPTED); | |
259 | } | |
612cfbfe | 260 | } |
e4813572 | 261 | |
b0f539de | 262 | static void |
33363fee | 263 | xfs_dir3_data_write_verify( |
612cfbfe DC |
264 | struct xfs_buf *bp) |
265 | { | |
33363fee DC |
266 | struct xfs_mount *mp = bp->b_target->bt_mount; |
267 | struct xfs_buf_log_item *bip = bp->b_fspriv; | |
268 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; | |
269 | ||
270 | if (!xfs_dir3_data_verify(bp)) { | |
271 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); | |
272 | xfs_buf_ioerror(bp, EFSCORRUPTED); | |
273 | return; | |
274 | } | |
275 | ||
276 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
277 | return; | |
278 | ||
279 | if (bip) | |
280 | hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
281 | ||
282 | xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF); | |
e4813572 DC |
283 | } |
284 | ||
33363fee DC |
285 | const struct xfs_buf_ops xfs_dir3_data_buf_ops = { |
286 | .verify_read = xfs_dir3_data_read_verify, | |
287 | .verify_write = xfs_dir3_data_write_verify, | |
1813dd64 DC |
288 | }; |
289 | ||
33363fee DC |
290 | static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = { |
291 | .verify_read = xfs_dir3_data_reada_verify, | |
292 | .verify_write = xfs_dir3_data_write_verify, | |
1813dd64 DC |
293 | }; |
294 | ||
612cfbfe | 295 | |
e4813572 | 296 | int |
33363fee | 297 | xfs_dir3_data_read( |
e4813572 DC |
298 | struct xfs_trans *tp, |
299 | struct xfs_inode *dp, | |
300 | xfs_dablk_t bno, | |
301 | xfs_daddr_t mapped_bno, | |
302 | struct xfs_buf **bpp) | |
303 | { | |
d75afeb3 DC |
304 | int err; |
305 | ||
306 | err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp, | |
33363fee | 307 | XFS_DATA_FORK, &xfs_dir3_data_buf_ops); |
d75afeb3 | 308 | if (!err && tp) |
61fe135c | 309 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF); |
d75afeb3 | 310 | return err; |
e4813572 DC |
311 | } |
312 | ||
da6958c8 | 313 | int |
33363fee | 314 | xfs_dir3_data_readahead( |
da6958c8 DC |
315 | struct xfs_trans *tp, |
316 | struct xfs_inode *dp, | |
317 | xfs_dablk_t bno, | |
318 | xfs_daddr_t mapped_bno) | |
319 | { | |
320 | return xfs_da_reada_buf(tp, dp, bno, mapped_bno, | |
33363fee | 321 | XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops); |
da6958c8 DC |
322 | } |
323 | ||
1da177e4 LT |
324 | /* |
325 | * Given a data block and an unused entry from that block, | |
326 | * return the bestfree entry if any that corresponds to it. | |
327 | */ | |
2b9ab5ab | 328 | xfs_dir2_data_free_t * |
1da177e4 | 329 | xfs_dir2_data_freefind( |
c2066e26 | 330 | xfs_dir2_data_hdr_t *hdr, /* data block */ |
1da177e4 LT |
331 | xfs_dir2_data_unused_t *dup) /* data unused entry */ |
332 | { | |
333 | xfs_dir2_data_free_t *dfp; /* bestfree entry */ | |
334 | xfs_dir2_data_aoff_t off; /* offset value needed */ | |
f5f3d9b0 | 335 | struct xfs_dir2_data_free *bf; |
b49a0c18 | 336 | #ifdef DEBUG |
1da177e4 LT |
337 | int matched; /* matched the value */ |
338 | int seenzero; /* saw a 0 bestfree entry */ | |
339 | #endif | |
340 | ||
c2066e26 | 341 | off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr); |
f5f3d9b0 DC |
342 | bf = xfs_dir3_data_bestfree_p(hdr); |
343 | ||
b49a0c18 | 344 | #ifdef DEBUG |
1da177e4 LT |
345 | /* |
346 | * Validate some consistency in the bestfree table. | |
347 | * Check order, non-overlapping entries, and if we find the | |
348 | * one we're looking for it has to be exact. | |
349 | */ | |
69ef921b | 350 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
33363fee | 351 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
f5f3d9b0 DC |
352 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
353 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
354 | for (dfp = &bf[0], seenzero = matched = 0; | |
355 | dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; | |
1da177e4 LT |
356 | dfp++) { |
357 | if (!dfp->offset) { | |
358 | ASSERT(!dfp->length); | |
359 | seenzero = 1; | |
360 | continue; | |
361 | } | |
362 | ASSERT(seenzero == 0); | |
70e73f59 | 363 | if (be16_to_cpu(dfp->offset) == off) { |
1da177e4 | 364 | matched = 1; |
ad354eb3 | 365 | ASSERT(dfp->length == dup->length); |
70e73f59 | 366 | } else if (off < be16_to_cpu(dfp->offset)) |
ad354eb3 | 367 | ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset)); |
1da177e4 | 368 | else |
70e73f59 | 369 | ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off); |
ad354eb3 | 370 | ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length)); |
f5f3d9b0 | 371 | if (dfp > &bf[0]) |
70e73f59 | 372 | ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length)); |
1da177e4 LT |
373 | } |
374 | #endif | |
375 | /* | |
376 | * If this is smaller than the smallest bestfree entry, | |
377 | * it can't be there since they're sorted. | |
378 | */ | |
ad354eb3 | 379 | if (be16_to_cpu(dup->length) < |
f5f3d9b0 | 380 | be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length)) |
1da177e4 LT |
381 | return NULL; |
382 | /* | |
383 | * Look at the three bestfree entries for our guy. | |
384 | */ | |
f5f3d9b0 | 385 | for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) { |
1da177e4 LT |
386 | if (!dfp->offset) |
387 | return NULL; | |
70e73f59 | 388 | if (be16_to_cpu(dfp->offset) == off) |
1da177e4 LT |
389 | return dfp; |
390 | } | |
391 | /* | |
392 | * Didn't find it. This only happens if there are duplicate lengths. | |
393 | */ | |
394 | return NULL; | |
395 | } | |
396 | ||
397 | /* | |
398 | * Insert an unused-space entry into the bestfree table. | |
399 | */ | |
400 | xfs_dir2_data_free_t * /* entry inserted */ | |
401 | xfs_dir2_data_freeinsert( | |
c2066e26 | 402 | xfs_dir2_data_hdr_t *hdr, /* data block pointer */ |
1da177e4 LT |
403 | xfs_dir2_data_unused_t *dup, /* unused space */ |
404 | int *loghead) /* log the data header (out) */ | |
405 | { | |
406 | xfs_dir2_data_free_t *dfp; /* bestfree table pointer */ | |
407 | xfs_dir2_data_free_t new; /* new bestfree entry */ | |
408 | ||
69ef921b | 409 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
f5f3d9b0 DC |
410 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
411 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || | |
412 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
413 | ||
414 | dfp = xfs_dir3_data_bestfree_p(hdr); | |
70e73f59 | 415 | new.length = dup->length; |
c2066e26 CH |
416 | new.offset = cpu_to_be16((char *)dup - (char *)hdr); |
417 | ||
1da177e4 LT |
418 | /* |
419 | * Insert at position 0, 1, or 2; or not at all. | |
420 | */ | |
70e73f59 | 421 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) { |
1da177e4 LT |
422 | dfp[2] = dfp[1]; |
423 | dfp[1] = dfp[0]; | |
424 | dfp[0] = new; | |
425 | *loghead = 1; | |
426 | return &dfp[0]; | |
427 | } | |
70e73f59 | 428 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) { |
1da177e4 LT |
429 | dfp[2] = dfp[1]; |
430 | dfp[1] = new; | |
431 | *loghead = 1; | |
432 | return &dfp[1]; | |
433 | } | |
70e73f59 | 434 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) { |
1da177e4 LT |
435 | dfp[2] = new; |
436 | *loghead = 1; | |
437 | return &dfp[2]; | |
438 | } | |
439 | return NULL; | |
440 | } | |
441 | ||
442 | /* | |
443 | * Remove a bestfree entry from the table. | |
444 | */ | |
ba0f32d4 | 445 | STATIC void |
1da177e4 | 446 | xfs_dir2_data_freeremove( |
c2066e26 | 447 | xfs_dir2_data_hdr_t *hdr, /* data block header */ |
1da177e4 LT |
448 | xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */ |
449 | int *loghead) /* out: log data header */ | |
450 | { | |
f5f3d9b0 DC |
451 | struct xfs_dir2_data_free *bf; |
452 | ||
69ef921b | 453 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
f5f3d9b0 DC |
454 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
455 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || | |
456 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
457 | ||
1da177e4 LT |
458 | /* |
459 | * It's the first entry, slide the next 2 up. | |
460 | */ | |
f5f3d9b0 DC |
461 | bf = xfs_dir3_data_bestfree_p(hdr); |
462 | if (dfp == &bf[0]) { | |
463 | bf[0] = bf[1]; | |
464 | bf[1] = bf[2]; | |
1da177e4 LT |
465 | } |
466 | /* | |
467 | * It's the second entry, slide the 3rd entry up. | |
468 | */ | |
f5f3d9b0 DC |
469 | else if (dfp == &bf[1]) |
470 | bf[1] = bf[2]; | |
1da177e4 LT |
471 | /* |
472 | * Must be the last entry. | |
473 | */ | |
474 | else | |
f5f3d9b0 | 475 | ASSERT(dfp == &bf[2]); |
1da177e4 LT |
476 | /* |
477 | * Clear the 3rd entry, must be zero now. | |
478 | */ | |
f5f3d9b0 DC |
479 | bf[2].length = 0; |
480 | bf[2].offset = 0; | |
1da177e4 LT |
481 | *loghead = 1; |
482 | } | |
483 | ||
484 | /* | |
485 | * Given a data block, reconstruct its bestfree map. | |
486 | */ | |
487 | void | |
488 | xfs_dir2_data_freescan( | |
489 | xfs_mount_t *mp, /* filesystem mount point */ | |
c2066e26 | 490 | xfs_dir2_data_hdr_t *hdr, /* data block header */ |
ef497f8a | 491 | int *loghead) /* out: log data header */ |
1da177e4 LT |
492 | { |
493 | xfs_dir2_block_tail_t *btp; /* block tail */ | |
494 | xfs_dir2_data_entry_t *dep; /* active data entry */ | |
495 | xfs_dir2_data_unused_t *dup; /* unused data entry */ | |
f5f3d9b0 | 496 | struct xfs_dir2_data_free *bf; |
1da177e4 LT |
497 | char *endp; /* end of block's data */ |
498 | char *p; /* current entry pointer */ | |
499 | ||
69ef921b | 500 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
33363fee | 501 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
f5f3d9b0 DC |
502 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
503 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
504 | ||
1da177e4 LT |
505 | /* |
506 | * Start by clearing the table. | |
507 | */ | |
f5f3d9b0 DC |
508 | bf = xfs_dir3_data_bestfree_p(hdr); |
509 | memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT); | |
1da177e4 LT |
510 | *loghead = 1; |
511 | /* | |
512 | * Set up pointers. | |
513 | */ | |
f5f3d9b0 DC |
514 | p = (char *)xfs_dir3_data_entry_p(hdr); |
515 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || | |
516 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) { | |
c2066e26 | 517 | btp = xfs_dir2_block_tail_p(mp, hdr); |
bbaaf538 | 518 | endp = (char *)xfs_dir2_block_leaf_p(btp); |
1da177e4 | 519 | } else |
c2066e26 | 520 | endp = (char *)hdr + mp->m_dirblksize; |
1da177e4 LT |
521 | /* |
522 | * Loop over the block's entries. | |
523 | */ | |
524 | while (p < endp) { | |
525 | dup = (xfs_dir2_data_unused_t *)p; | |
526 | /* | |
527 | * If it's a free entry, insert it. | |
528 | */ | |
ad354eb3 | 529 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
c2066e26 | 530 | ASSERT((char *)dup - (char *)hdr == |
bbaaf538 | 531 | be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup))); |
c2066e26 | 532 | xfs_dir2_data_freeinsert(hdr, dup, loghead); |
ad354eb3 | 533 | p += be16_to_cpu(dup->length); |
1da177e4 LT |
534 | } |
535 | /* | |
536 | * For active entries, check their tags and skip them. | |
537 | */ | |
538 | else { | |
539 | dep = (xfs_dir2_data_entry_t *)p; | |
c2066e26 | 540 | ASSERT((char *)dep - (char *)hdr == |
0cb97766 DC |
541 | be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep))); |
542 | p += xfs_dir3_data_entsize(mp, dep->namelen); | |
1da177e4 LT |
543 | } |
544 | } | |
545 | } | |
546 | ||
547 | /* | |
548 | * Initialize a data block at the given block number in the directory. | |
549 | * Give back the buffer for the created block. | |
550 | */ | |
551 | int /* error */ | |
f5f3d9b0 | 552 | xfs_dir3_data_init( |
1da177e4 LT |
553 | xfs_da_args_t *args, /* directory operation args */ |
554 | xfs_dir2_db_t blkno, /* logical dir block number */ | |
1d9025e5 | 555 | struct xfs_buf **bpp) /* output block buffer */ |
1da177e4 | 556 | { |
1d9025e5 | 557 | struct xfs_buf *bp; /* block buffer */ |
c2066e26 | 558 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
1da177e4 LT |
559 | xfs_inode_t *dp; /* incore directory inode */ |
560 | xfs_dir2_data_unused_t *dup; /* unused entry pointer */ | |
f5f3d9b0 | 561 | struct xfs_dir2_data_free *bf; |
1da177e4 LT |
562 | int error; /* error return value */ |
563 | int i; /* bestfree index */ | |
564 | xfs_mount_t *mp; /* filesystem mount point */ | |
565 | xfs_trans_t *tp; /* transaction pointer */ | |
566 | int t; /* temp */ | |
567 | ||
568 | dp = args->dp; | |
569 | mp = dp->i_mount; | |
570 | tp = args->trans; | |
571 | /* | |
572 | * Get the buffer set up for the block. | |
573 | */ | |
bbaaf538 | 574 | error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp, |
1da177e4 | 575 | XFS_DATA_FORK); |
b0f539de | 576 | if (error) |
1da177e4 | 577 | return error; |
33363fee | 578 | bp->b_ops = &xfs_dir3_data_buf_ops; |
61fe135c | 579 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF); |
c2066e26 | 580 | |
1da177e4 LT |
581 | /* |
582 | * Initialize the header. | |
583 | */ | |
1d9025e5 | 584 | hdr = bp->b_addr; |
f5f3d9b0 DC |
585 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
586 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; | |
587 | ||
588 | memset(hdr3, 0, sizeof(*hdr3)); | |
589 | hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC); | |
590 | hdr3->blkno = cpu_to_be64(bp->b_bn); | |
591 | hdr3->owner = cpu_to_be64(dp->i_ino); | |
592 | uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid); | |
593 | ||
594 | } else | |
595 | hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC); | |
596 | ||
597 | bf = xfs_dir3_data_bestfree_p(hdr); | |
598 | bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr)); | |
1da177e4 | 599 | for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) { |
f5f3d9b0 DC |
600 | bf[i].length = 0; |
601 | bf[i].offset = 0; | |
1da177e4 | 602 | } |
c2066e26 | 603 | |
1da177e4 LT |
604 | /* |
605 | * Set up an unused entry for the block's body. | |
606 | */ | |
f5f3d9b0 | 607 | dup = xfs_dir3_data_unused_p(hdr); |
ad354eb3 | 608 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
1da177e4 | 609 | |
f5f3d9b0 DC |
610 | t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr); |
611 | bf[0].length = cpu_to_be16(t); | |
ad354eb3 | 612 | dup->length = cpu_to_be16(t); |
c2066e26 | 613 | *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr); |
1da177e4 LT |
614 | /* |
615 | * Log it and return it. | |
616 | */ | |
617 | xfs_dir2_data_log_header(tp, bp); | |
618 | xfs_dir2_data_log_unused(tp, bp, dup); | |
619 | *bpp = bp; | |
620 | return 0; | |
621 | } | |
622 | ||
623 | /* | |
624 | * Log an active data entry from the block. | |
625 | */ | |
626 | void | |
627 | xfs_dir2_data_log_entry( | |
1d9025e5 DC |
628 | struct xfs_trans *tp, |
629 | struct xfs_buf *bp, | |
1da177e4 LT |
630 | xfs_dir2_data_entry_t *dep) /* data entry pointer */ |
631 | { | |
0cb97766 DC |
632 | struct xfs_dir2_data_hdr *hdr = bp->b_addr; |
633 | struct xfs_mount *mp = tp->t_mountp; | |
1da177e4 | 634 | |
69ef921b | 635 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
33363fee | 636 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
f5f3d9b0 DC |
637 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
638 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
c2066e26 | 639 | |
1d9025e5 | 640 | xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr), |
0cb97766 | 641 | (uint)((char *)(xfs_dir3_data_entry_tag_p(mp, dep) + 1) - |
c2066e26 | 642 | (char *)hdr - 1)); |
1da177e4 LT |
643 | } |
644 | ||
645 | /* | |
646 | * Log a data block header. | |
647 | */ | |
648 | void | |
649 | xfs_dir2_data_log_header( | |
1d9025e5 DC |
650 | struct xfs_trans *tp, |
651 | struct xfs_buf *bp) | |
1da177e4 | 652 | { |
1d9025e5 | 653 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
1da177e4 | 654 | |
69ef921b | 655 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
33363fee | 656 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
f5f3d9b0 DC |
657 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
658 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
c2066e26 | 659 | |
f5f3d9b0 | 660 | xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1); |
1da177e4 LT |
661 | } |
662 | ||
663 | /* | |
664 | * Log a data unused entry. | |
665 | */ | |
666 | void | |
667 | xfs_dir2_data_log_unused( | |
1d9025e5 DC |
668 | struct xfs_trans *tp, |
669 | struct xfs_buf *bp, | |
1da177e4 LT |
670 | xfs_dir2_data_unused_t *dup) /* data unused pointer */ |
671 | { | |
1d9025e5 | 672 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
c2066e26 | 673 | |
69ef921b | 674 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
33363fee | 675 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
f5f3d9b0 DC |
676 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
677 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
1da177e4 | 678 | |
1da177e4 LT |
679 | /* |
680 | * Log the first part of the unused entry. | |
681 | */ | |
1d9025e5 | 682 | xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr), |
1da177e4 | 683 | (uint)((char *)&dup->length + sizeof(dup->length) - |
c2066e26 | 684 | 1 - (char *)hdr)); |
1da177e4 LT |
685 | /* |
686 | * Log the end (tag) of the unused entry. | |
687 | */ | |
1d9025e5 | 688 | xfs_trans_log_buf(tp, bp, |
c2066e26 CH |
689 | (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr), |
690 | (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr + | |
1da177e4 LT |
691 | sizeof(xfs_dir2_data_off_t) - 1)); |
692 | } | |
693 | ||
694 | /* | |
695 | * Make a byte range in the data block unused. | |
696 | * Its current contents are unimportant. | |
697 | */ | |
698 | void | |
699 | xfs_dir2_data_make_free( | |
1d9025e5 DC |
700 | struct xfs_trans *tp, |
701 | struct xfs_buf *bp, | |
1da177e4 LT |
702 | xfs_dir2_data_aoff_t offset, /* starting byte offset */ |
703 | xfs_dir2_data_aoff_t len, /* length in bytes */ | |
704 | int *needlogp, /* out: log header */ | |
705 | int *needscanp) /* out: regen bestfree */ | |
706 | { | |
c2066e26 | 707 | xfs_dir2_data_hdr_t *hdr; /* data block pointer */ |
1da177e4 LT |
708 | xfs_dir2_data_free_t *dfp; /* bestfree pointer */ |
709 | char *endptr; /* end of data area */ | |
710 | xfs_mount_t *mp; /* filesystem mount point */ | |
711 | int needscan; /* need to regen bestfree */ | |
712 | xfs_dir2_data_unused_t *newdup; /* new unused entry */ | |
713 | xfs_dir2_data_unused_t *postdup; /* unused entry after us */ | |
714 | xfs_dir2_data_unused_t *prevdup; /* unused entry before us */ | |
f5f3d9b0 | 715 | struct xfs_dir2_data_free *bf; |
1da177e4 LT |
716 | |
717 | mp = tp->t_mountp; | |
1d9025e5 | 718 | hdr = bp->b_addr; |
c2066e26 | 719 | |
1da177e4 LT |
720 | /* |
721 | * Figure out where the end of the data area is. | |
722 | */ | |
33363fee DC |
723 | if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
724 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)) | |
c2066e26 | 725 | endptr = (char *)hdr + mp->m_dirblksize; |
1da177e4 LT |
726 | else { |
727 | xfs_dir2_block_tail_t *btp; /* block tail */ | |
728 | ||
f5f3d9b0 DC |
729 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
730 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
c2066e26 | 731 | btp = xfs_dir2_block_tail_p(mp, hdr); |
bbaaf538 | 732 | endptr = (char *)xfs_dir2_block_leaf_p(btp); |
1da177e4 LT |
733 | } |
734 | /* | |
735 | * If this isn't the start of the block, then back up to | |
736 | * the previous entry and see if it's free. | |
737 | */ | |
f5f3d9b0 | 738 | if (offset > xfs_dir3_data_entry_offset(hdr)) { |
3d693c6e | 739 | __be16 *tagp; /* tag just before us */ |
1da177e4 | 740 | |
c2066e26 CH |
741 | tagp = (__be16 *)((char *)hdr + offset) - 1; |
742 | prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | |
ad354eb3 | 743 | if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
1da177e4 LT |
744 | prevdup = NULL; |
745 | } else | |
746 | prevdup = NULL; | |
747 | /* | |
748 | * If this isn't the end of the block, see if the entry after | |
749 | * us is free. | |
750 | */ | |
c2066e26 | 751 | if ((char *)hdr + offset + len < endptr) { |
1da177e4 | 752 | postdup = |
c2066e26 | 753 | (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
ad354eb3 | 754 | if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
1da177e4 LT |
755 | postdup = NULL; |
756 | } else | |
757 | postdup = NULL; | |
758 | ASSERT(*needscanp == 0); | |
759 | needscan = 0; | |
760 | /* | |
761 | * Previous and following entries are both free, | |
762 | * merge everything into a single free entry. | |
763 | */ | |
f5f3d9b0 | 764 | bf = xfs_dir3_data_bestfree_p(hdr); |
1da177e4 LT |
765 | if (prevdup && postdup) { |
766 | xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */ | |
767 | ||
768 | /* | |
769 | * See if prevdup and/or postdup are in bestfree table. | |
770 | */ | |
c2066e26 CH |
771 | dfp = xfs_dir2_data_freefind(hdr, prevdup); |
772 | dfp2 = xfs_dir2_data_freefind(hdr, postdup); | |
1da177e4 LT |
773 | /* |
774 | * We need a rescan unless there are exactly 2 free entries | |
775 | * namely our two. Then we know what's happening, otherwise | |
776 | * since the third bestfree is there, there might be more | |
777 | * entries. | |
778 | */ | |
f5f3d9b0 | 779 | needscan = (bf[2].length != 0); |
1da177e4 LT |
780 | /* |
781 | * Fix up the new big freespace. | |
782 | */ | |
413d57c9 | 783 | be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length)); |
bbaaf538 | 784 | *xfs_dir2_data_unused_tag_p(prevdup) = |
c2066e26 | 785 | cpu_to_be16((char *)prevdup - (char *)hdr); |
1da177e4 LT |
786 | xfs_dir2_data_log_unused(tp, bp, prevdup); |
787 | if (!needscan) { | |
788 | /* | |
789 | * Has to be the case that entries 0 and 1 are | |
790 | * dfp and dfp2 (don't know which is which), and | |
791 | * entry 2 is empty. | |
792 | * Remove entry 1 first then entry 0. | |
793 | */ | |
794 | ASSERT(dfp && dfp2); | |
f5f3d9b0 DC |
795 | if (dfp == &bf[1]) { |
796 | dfp = &bf[0]; | |
1da177e4 | 797 | ASSERT(dfp2 == dfp); |
f5f3d9b0 | 798 | dfp2 = &bf[1]; |
1da177e4 | 799 | } |
c2066e26 CH |
800 | xfs_dir2_data_freeremove(hdr, dfp2, needlogp); |
801 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); | |
1da177e4 LT |
802 | /* |
803 | * Now insert the new entry. | |
804 | */ | |
c2066e26 | 805 | dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp); |
f5f3d9b0 | 806 | ASSERT(dfp == &bf[0]); |
ad354eb3 | 807 | ASSERT(dfp->length == prevdup->length); |
1da177e4 LT |
808 | ASSERT(!dfp[1].length); |
809 | ASSERT(!dfp[2].length); | |
810 | } | |
811 | } | |
812 | /* | |
813 | * The entry before us is free, merge with it. | |
814 | */ | |
815 | else if (prevdup) { | |
c2066e26 | 816 | dfp = xfs_dir2_data_freefind(hdr, prevdup); |
413d57c9 | 817 | be16_add_cpu(&prevdup->length, len); |
bbaaf538 | 818 | *xfs_dir2_data_unused_tag_p(prevdup) = |
c2066e26 | 819 | cpu_to_be16((char *)prevdup - (char *)hdr); |
1da177e4 LT |
820 | xfs_dir2_data_log_unused(tp, bp, prevdup); |
821 | /* | |
822 | * If the previous entry was in the table, the new entry | |
823 | * is longer, so it will be in the table too. Remove | |
824 | * the old one and add the new one. | |
825 | */ | |
826 | if (dfp) { | |
c2066e26 CH |
827 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
828 | xfs_dir2_data_freeinsert(hdr, prevdup, needlogp); | |
1da177e4 LT |
829 | } |
830 | /* | |
831 | * Otherwise we need a scan if the new entry is big enough. | |
832 | */ | |
70e73f59 | 833 | else { |
ad354eb3 | 834 | needscan = be16_to_cpu(prevdup->length) > |
f5f3d9b0 | 835 | be16_to_cpu(bf[2].length); |
70e73f59 | 836 | } |
1da177e4 LT |
837 | } |
838 | /* | |
839 | * The following entry is free, merge with it. | |
840 | */ | |
841 | else if (postdup) { | |
c2066e26 CH |
842 | dfp = xfs_dir2_data_freefind(hdr, postdup); |
843 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); | |
ad354eb3 NS |
844 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
845 | newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length)); | |
bbaaf538 | 846 | *xfs_dir2_data_unused_tag_p(newdup) = |
c2066e26 | 847 | cpu_to_be16((char *)newdup - (char *)hdr); |
1da177e4 LT |
848 | xfs_dir2_data_log_unused(tp, bp, newdup); |
849 | /* | |
850 | * If the following entry was in the table, the new entry | |
851 | * is longer, so it will be in the table too. Remove | |
852 | * the old one and add the new one. | |
853 | */ | |
854 | if (dfp) { | |
c2066e26 CH |
855 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
856 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | |
1da177e4 LT |
857 | } |
858 | /* | |
859 | * Otherwise we need a scan if the new entry is big enough. | |
860 | */ | |
70e73f59 | 861 | else { |
ad354eb3 | 862 | needscan = be16_to_cpu(newdup->length) > |
f5f3d9b0 | 863 | be16_to_cpu(bf[2].length); |
70e73f59 | 864 | } |
1da177e4 LT |
865 | } |
866 | /* | |
867 | * Neither neighbor is free. Make a new entry. | |
868 | */ | |
869 | else { | |
c2066e26 | 870 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); |
ad354eb3 NS |
871 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
872 | newdup->length = cpu_to_be16(len); | |
bbaaf538 | 873 | *xfs_dir2_data_unused_tag_p(newdup) = |
c2066e26 | 874 | cpu_to_be16((char *)newdup - (char *)hdr); |
1da177e4 | 875 | xfs_dir2_data_log_unused(tp, bp, newdup); |
c2066e26 | 876 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); |
1da177e4 LT |
877 | } |
878 | *needscanp = needscan; | |
879 | } | |
880 | ||
881 | /* | |
882 | * Take a byte range out of an existing unused space and make it un-free. | |
883 | */ | |
884 | void | |
885 | xfs_dir2_data_use_free( | |
1d9025e5 DC |
886 | struct xfs_trans *tp, |
887 | struct xfs_buf *bp, | |
1da177e4 LT |
888 | xfs_dir2_data_unused_t *dup, /* unused entry */ |
889 | xfs_dir2_data_aoff_t offset, /* starting offset to use */ | |
890 | xfs_dir2_data_aoff_t len, /* length to use */ | |
891 | int *needlogp, /* out: need to log header */ | |
892 | int *needscanp) /* out: need regen bestfree */ | |
893 | { | |
c2066e26 | 894 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
1da177e4 LT |
895 | xfs_dir2_data_free_t *dfp; /* bestfree pointer */ |
896 | int matchback; /* matches end of freespace */ | |
897 | int matchfront; /* matches start of freespace */ | |
898 | int needscan; /* need to regen bestfree */ | |
899 | xfs_dir2_data_unused_t *newdup; /* new unused entry */ | |
900 | xfs_dir2_data_unused_t *newdup2; /* another new unused entry */ | |
901 | int oldlen; /* old unused entry's length */ | |
f5f3d9b0 | 902 | struct xfs_dir2_data_free *bf; |
1da177e4 | 903 | |
1d9025e5 | 904 | hdr = bp->b_addr; |
69ef921b | 905 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
33363fee | 906 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
f5f3d9b0 DC |
907 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
908 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
ad354eb3 | 909 | ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG); |
c2066e26 CH |
910 | ASSERT(offset >= (char *)dup - (char *)hdr); |
911 | ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr); | |
912 | ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup))); | |
1da177e4 LT |
913 | /* |
914 | * Look up the entry in the bestfree table. | |
915 | */ | |
c2066e26 | 916 | dfp = xfs_dir2_data_freefind(hdr, dup); |
ad354eb3 | 917 | oldlen = be16_to_cpu(dup->length); |
f5f3d9b0 DC |
918 | bf = xfs_dir3_data_bestfree_p(hdr); |
919 | ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length)); | |
1da177e4 LT |
920 | /* |
921 | * Check for alignment with front and back of the entry. | |
922 | */ | |
c2066e26 CH |
923 | matchfront = (char *)dup - (char *)hdr == offset; |
924 | matchback = (char *)dup + oldlen - (char *)hdr == offset + len; | |
1da177e4 LT |
925 | ASSERT(*needscanp == 0); |
926 | needscan = 0; | |
927 | /* | |
928 | * If we matched it exactly we just need to get rid of it from | |
929 | * the bestfree table. | |
930 | */ | |
931 | if (matchfront && matchback) { | |
932 | if (dfp) { | |
f5f3d9b0 | 933 | needscan = (bf[2].offset != 0); |
1da177e4 | 934 | if (!needscan) |
c2066e26 | 935 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
1da177e4 LT |
936 | } |
937 | } | |
938 | /* | |
939 | * We match the first part of the entry. | |
940 | * Make a new entry with the remaining freespace. | |
941 | */ | |
942 | else if (matchfront) { | |
c2066e26 | 943 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
ad354eb3 NS |
944 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
945 | newdup->length = cpu_to_be16(oldlen - len); | |
bbaaf538 | 946 | *xfs_dir2_data_unused_tag_p(newdup) = |
c2066e26 | 947 | cpu_to_be16((char *)newdup - (char *)hdr); |
1da177e4 LT |
948 | xfs_dir2_data_log_unused(tp, bp, newdup); |
949 | /* | |
950 | * If it was in the table, remove it and add the new one. | |
951 | */ | |
952 | if (dfp) { | |
c2066e26 CH |
953 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
954 | dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | |
1da177e4 | 955 | ASSERT(dfp != NULL); |
ad354eb3 | 956 | ASSERT(dfp->length == newdup->length); |
c2066e26 | 957 | ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr); |
1da177e4 LT |
958 | /* |
959 | * If we got inserted at the last slot, | |
960 | * that means we don't know if there was a better | |
961 | * choice for the last slot, or not. Rescan. | |
962 | */ | |
f5f3d9b0 | 963 | needscan = dfp == &bf[2]; |
1da177e4 LT |
964 | } |
965 | } | |
966 | /* | |
967 | * We match the last part of the entry. | |
968 | * Trim the allocated space off the tail of the entry. | |
969 | */ | |
970 | else if (matchback) { | |
971 | newdup = dup; | |
c2066e26 | 972 | newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup); |
bbaaf538 | 973 | *xfs_dir2_data_unused_tag_p(newdup) = |
c2066e26 | 974 | cpu_to_be16((char *)newdup - (char *)hdr); |
1da177e4 LT |
975 | xfs_dir2_data_log_unused(tp, bp, newdup); |
976 | /* | |
977 | * If it was in the table, remove it and add the new one. | |
978 | */ | |
979 | if (dfp) { | |
c2066e26 CH |
980 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
981 | dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | |
1da177e4 | 982 | ASSERT(dfp != NULL); |
ad354eb3 | 983 | ASSERT(dfp->length == newdup->length); |
c2066e26 | 984 | ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr); |
1da177e4 LT |
985 | /* |
986 | * If we got inserted at the last slot, | |
987 | * that means we don't know if there was a better | |
988 | * choice for the last slot, or not. Rescan. | |
989 | */ | |
f5f3d9b0 | 990 | needscan = dfp == &bf[2]; |
1da177e4 LT |
991 | } |
992 | } | |
993 | /* | |
994 | * Poking out the middle of an entry. | |
995 | * Make two new entries. | |
996 | */ | |
997 | else { | |
998 | newdup = dup; | |
c2066e26 | 999 | newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup); |
bbaaf538 | 1000 | *xfs_dir2_data_unused_tag_p(newdup) = |
c2066e26 | 1001 | cpu_to_be16((char *)newdup - (char *)hdr); |
1da177e4 | 1002 | xfs_dir2_data_log_unused(tp, bp, newdup); |
c2066e26 | 1003 | newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
ad354eb3 NS |
1004 | newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
1005 | newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length)); | |
bbaaf538 | 1006 | *xfs_dir2_data_unused_tag_p(newdup2) = |
c2066e26 | 1007 | cpu_to_be16((char *)newdup2 - (char *)hdr); |
1da177e4 LT |
1008 | xfs_dir2_data_log_unused(tp, bp, newdup2); |
1009 | /* | |
1010 | * If the old entry was in the table, we need to scan | |
1011 | * if the 3rd entry was valid, since these entries | |
1012 | * are smaller than the old one. | |
1013 | * If we don't need to scan that means there were 1 or 2 | |
1014 | * entries in the table, and removing the old and adding | |
1015 | * the 2 new will work. | |
1016 | */ | |
1017 | if (dfp) { | |
f5f3d9b0 | 1018 | needscan = (bf[2].length != 0); |
1da177e4 | 1019 | if (!needscan) { |
c2066e26 CH |
1020 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
1021 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | |
1022 | xfs_dir2_data_freeinsert(hdr, newdup2, | |
1023 | needlogp); | |
1da177e4 LT |
1024 | } |
1025 | } | |
1026 | } | |
1027 | *needscanp = needscan; | |
1028 | } |