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