125
121
xfs_dir2_data_make_free(tp, dbp,
126
(xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
127
(xfs_dir2_data_aoff_t)((char *)block + mp->m_dirblksize -
122
(xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
123
(xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
129
125
&needlog, &needscan);
131
127
* Fix up the block header, make it a data block.
133
block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
129
hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
135
xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
131
xfs_dir2_data_freescan(mp, hdr, &needlog);
137
133
* Set up leaf tail and bests table.
139
135
ltp = xfs_dir2_leaf_tail_p(mp, leaf);
140
136
ltp->bestcount = cpu_to_be32(1);
141
137
bestsp = xfs_dir2_leaf_bests_p(ltp);
142
bestsp[0] = block->hdr.bestfree[0].length;
138
bestsp[0] = hdr->bestfree[0].length;
144
140
* Log the data header and leaf bests table.
152
xfs_dir2_leaf_find_stale(
153
struct xfs_dir2_leaf *leaf,
159
* Find the first stale entry before our index, if any.
161
for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
162
if (leaf->ents[*lowstale].address ==
163
cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
168
* Find the first stale entry at or after our index, if any.
169
* Stop if the result would require moving more entries than using
172
for (*highstale = index;
173
*highstale < be16_to_cpu(leaf->hdr.count);
175
if (leaf->ents[*highstale].address ==
176
cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
178
if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
183
struct xfs_dir2_leaf_entry *
184
xfs_dir2_leaf_find_entry(
185
xfs_dir2_leaf_t *leaf, /* leaf structure */
186
int index, /* leaf table position */
187
int compact, /* need to compact leaves */
188
int lowstale, /* index of prev stale leaf */
189
int highstale, /* index of next stale leaf */
190
int *lfloglow, /* low leaf logging index */
191
int *lfloghigh) /* high leaf logging index */
193
if (!leaf->hdr.stale) {
194
xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
197
* Now we need to make room to insert the leaf entry.
199
* If there are no stale entries, just insert a hole at index.
201
lep = &leaf->ents[index];
202
if (index < be16_to_cpu(leaf->hdr.count))
203
memmove(lep + 1, lep,
204
(be16_to_cpu(leaf->hdr.count) - index) *
208
* Record low and high logging indices for the leaf.
211
*lfloghigh = be16_to_cpu(leaf->hdr.count);
212
be16_add_cpu(&leaf->hdr.count, 1);
217
* There are stale entries.
219
* We will use one of them for the new entry. It's probably not at
220
* the right location, so we'll have to shift some up or down first.
222
* If we didn't compact before, we need to find the nearest stale
223
* entries before and after our insertion point.
226
xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
229
* If the low one is better, use it.
232
(highstale == be16_to_cpu(leaf->hdr.count) ||
233
index - lowstale - 1 < highstale - index)) {
234
ASSERT(index - lowstale - 1 >= 0);
235
ASSERT(leaf->ents[lowstale].address ==
236
cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
239
* Copy entries up to cover the stale entry and make room
242
if (index - lowstale - 1 > 0) {
243
memmove(&leaf->ents[lowstale],
244
&leaf->ents[lowstale + 1],
245
(index - lowstale - 1) *
246
sizeof(xfs_dir2_leaf_entry_t));
248
*lfloglow = MIN(lowstale, *lfloglow);
249
*lfloghigh = MAX(index - 1, *lfloghigh);
250
be16_add_cpu(&leaf->hdr.stale, -1);
251
return &leaf->ents[index - 1];
255
* The high one is better, so use that one.
257
ASSERT(highstale - index >= 0);
258
ASSERT(leaf->ents[highstale].address ==
259
cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
262
* Copy entries down to cover the stale entry and make room for the
265
if (highstale - index > 0) {
266
memmove(&leaf->ents[index + 1],
268
(highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
270
*lfloglow = MIN(index, *lfloglow);
271
*lfloghigh = MAX(highstale, *lfloghigh);
272
be16_add_cpu(&leaf->hdr.stale, -1);
273
return &leaf->ents[index];
156
277
* Add an entry to a leaf form directory.
392
517
* Point to the biggest freespace in our data block.
394
519
dup = (xfs_dir2_data_unused_t *)
395
((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
520
((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
396
521
ASSERT(be16_to_cpu(dup->length) >= length);
397
522
needscan = needlog = 0;
399
524
* Mark the initial part of our freespace in use for the new entry.
401
526
xfs_dir2_data_use_free(tp, dbp, dup,
402
(xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
527
(xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
403
528
&needlog, &needscan);
405
530
* Initialize our new entry (at last).
425
550
* If the bests table needs to be changed, do it.
426
551
* Log the change unless we've already done that.
428
if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
429
bestsp[use_block] = data->hdr.bestfree[0].length;
553
if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
554
bestsp[use_block] = hdr->bestfree[0].length;
431
556
xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
434
* Now we need to make room to insert the leaf entry.
435
* If there are no stale entries, we just insert a hole at index.
437
if (!leaf->hdr.stale) {
439
* lep is still good as the index leaf entry.
441
if (index < be16_to_cpu(leaf->hdr.count))
442
memmove(lep + 1, lep,
443
(be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
445
* Record low and high logging indices for the leaf.
448
lfloghigh = be16_to_cpu(leaf->hdr.count);
449
be16_add_cpu(&leaf->hdr.count, 1);
452
* There are stale entries.
453
* We will use one of them for the new entry.
454
* It's probably not at the right location, so we'll have to
455
* shift some up or down first.
459
* If we didn't compact before, we need to find the nearest
460
* stale entries before and after our insertion point.
464
* Find the first stale entry before the insertion
467
for (lowstale = index - 1;
469
be32_to_cpu(leaf->ents[lowstale].address) !=
470
XFS_DIR2_NULL_DATAPTR;
474
* Find the next stale entry at or after the insertion
475
* point, if any. Stop if we go so far that the
476
* lowstale entry would be better.
478
for (highstale = index;
479
highstale < be16_to_cpu(leaf->hdr.count) &&
480
be32_to_cpu(leaf->ents[highstale].address) !=
481
XFS_DIR2_NULL_DATAPTR &&
483
index - lowstale - 1 >= highstale - index);
488
* If the low one is better, use it.
491
(highstale == be16_to_cpu(leaf->hdr.count) ||
492
index - lowstale - 1 < highstale - index)) {
493
ASSERT(index - lowstale - 1 >= 0);
494
ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
495
XFS_DIR2_NULL_DATAPTR);
497
* Copy entries up to cover the stale entry
498
* and make room for the new entry.
500
if (index - lowstale - 1 > 0)
501
memmove(&leaf->ents[lowstale],
502
&leaf->ents[lowstale + 1],
503
(index - lowstale - 1) * sizeof(*lep));
504
lep = &leaf->ents[index - 1];
505
lfloglow = MIN(lowstale, lfloglow);
506
lfloghigh = MAX(index - 1, lfloghigh);
509
* The high one is better, so use that one.
512
ASSERT(highstale - index >= 0);
513
ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
514
XFS_DIR2_NULL_DATAPTR);
516
* Copy entries down to cover the stale entry
517
* and make room for the new entry.
519
if (highstale - index > 0)
520
memmove(&leaf->ents[index + 1],
522
(highstale - index) * sizeof(*lep));
523
lep = &leaf->ents[index];
524
lfloglow = MIN(index, lfloglow);
525
lfloghigh = MAX(highstale, lfloghigh);
527
be16_add_cpu(&leaf->hdr.stale, -1);
559
lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
560
highstale, &lfloglow, &lfloghigh);
530
563
* Fill in the new leaf entry.
664
698
ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
667
* Find the first stale entry before our index, if any.
669
for (lowstale = index - 1;
671
be32_to_cpu(leaf->ents[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
675
* Find the first stale entry at or after our index, if any.
676
* Stop if the answer would be worse than lowstale.
678
for (highstale = index;
679
highstale < be16_to_cpu(leaf->hdr.count) &&
680
be32_to_cpu(leaf->ents[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
681
(lowstale < 0 || index - lowstale > highstale - index);
701
xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
685
704
* Pick the better of lowstale and highstale.
868
888
* we already have in the table.
870
890
nmap = map_size - map_valid;
871
error = xfs_bmapi(NULL, dp,
891
error = xfs_bmapi_read(dp, map_off,
873
892
xfs_dir2_byte_to_da(mp,
874
893
XFS_DIR2_LEAF_OFFSET) - map_off,
875
XFS_BMAPI_METADATA, NULL, 0,
876
&map[map_valid], &nmap, NULL);
894
&map[map_valid], &nmap, 0);
878
896
* Don't know if we should ignore this or
879
897
* try to return an error.
1018
1036
else if (curoff > newoff)
1019
1037
ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
1022
1040
xfs_dir2_data_check(dp, bp);
1024
1042
* Find our position in the block.
1026
ptr = (char *)&data->u;
1044
ptr = (char *)(hdr + 1);
1027
1045
byteoff = xfs_dir2_byte_to_off(mp, curoff);
1029
1047
* Skip past the header.
1031
1049
if (byteoff == 0)
1032
curoff += (uint)sizeof(data->hdr);
1050
curoff += (uint)sizeof(*hdr);
1034
1052
* Skip past entries until we reach our offset.
1037
while ((char *)ptr - (char *)data < byteoff) {
1055
while ((char *)ptr - (char *)hdr < byteoff) {
1038
1056
dup = (xfs_dir2_data_unused_t *)ptr;
1040
1058
if (be16_to_cpu(dup->freetag)
1475
1493
lep = &leaf->ents[index];
1476
1494
db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1477
1495
dep = (xfs_dir2_data_entry_t *)
1478
((char *)data + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1496
((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1479
1497
needscan = needlog = 0;
1480
oldbest = be16_to_cpu(data->hdr.bestfree[0].length);
1498
oldbest = be16_to_cpu(hdr->bestfree[0].length);
1481
1499
ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1482
1500
bestsp = xfs_dir2_leaf_bests_p(ltp);
1483
1501
ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1499
1517
* log the data block header if necessary.
1502
xfs_dir2_data_freescan(mp, data, &needlog);
1520
xfs_dir2_data_freescan(mp, hdr, &needlog);
1504
1522
xfs_dir2_data_log_header(tp, dbp);
1506
1524
* If the longest freespace in the data block has changed,
1507
1525
* put the new value in the bests table and log that.
1509
if (be16_to_cpu(data->hdr.bestfree[0].length) != oldbest) {
1510
bestsp[db] = data->hdr.bestfree[0].length;
1527
if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1528
bestsp[db] = hdr->bestfree[0].length;
1511
1529
xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1513
1531
xfs_dir2_data_check(dp, dbp);
1515
1533
* If the data block is now empty then get rid of the data block.
1517
if (be16_to_cpu(data->hdr.bestfree[0].length) ==
1518
mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1535
if (be16_to_cpu(hdr->bestfree[0].length) ==
1536
mp->m_dirblksize - (uint)sizeof(*hdr)) {
1519
1537
ASSERT(db != mp->m_dirdatablk);
1520
1538
if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1707
1722
XFS_DATA_FORK))) {
1712
ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
1714
/* this seems to be an error
1715
* data is only valid if DEBUG is defined?
1719
1726
leaf = lbp->data;
1720
1727
ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1721
ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) ==
1722
mp->m_dirblksize - (uint)sizeof(data->hdr));
1731
struct xfs_dir2_data_hdr *hdr = dbp->data;
1733
ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
1734
ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1735
mp->m_dirblksize - (uint)sizeof(*hdr));
1723
1736
ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1725
1741
* Get rid of the data block.
1821
1851
free = fbp->data;
1822
ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1852
ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
1823
1853
ASSERT(!free->hdr.firstdb);
1825
1856
* Now see if the leafn and free data will fit in a leaf1.
1826
1857
* If not, release the buffer and give up.
1828
if ((uint)sizeof(leaf->hdr) +
1829
(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)) * (uint)sizeof(leaf->ents[0]) +
1830
be32_to_cpu(free->hdr.nvalid) * (uint)sizeof(leaf->bests[0]) +
1831
(uint)sizeof(leaf->tail) >
1859
if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1833
1861
xfs_da_brelse(tp, fbp);
1837
1866
* If the leaf has any stale entries in it, compress them out.
1838
1867
* The compact routine will log the header.