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