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