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