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