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