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