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