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