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