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