16
16
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
* XFS directory version 2 implementation - single leaf form
22
* see xfs_dir2_leaf.h for data structures.
23
* These directories have multiple XFS_DIR2_DATA blocks and one
24
* XFS_DIR2_LEAF1 block containing the hash table and freespace map.
29
static void xfs_dir2_leaf_log_bests(xfs_trans_t *, xfs_dabuf_t *, int, int);
30
static void xfs_dir2_leaf_log_tail(xfs_trans_t *, xfs_dabuf_t *);
22
* Local function declarations.
25
static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
27
#define xfs_dir2_leaf_check(dp, bp)
29
static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
30
int *indexp, xfs_dabuf_t **dbpp);
31
static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
33
static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
33
37
* Convert a block form directory to a leaf form directory.
79
83
xfs_dir2_data_check(dp, dbp);
80
btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
81
blp = XFS_DIR2_BLOCK_LEAF_P(btp);
84
btp = xfs_dir2_block_tail_p(mp, block);
85
blp = xfs_dir2_block_leaf_p(btp);
83
87
* Set the counts in the leaf header.
85
INT_COPY(leaf->hdr.count, btp->count, ARCH_CONVERT); /* INT_: type change */
86
INT_COPY(leaf->hdr.stale, btp->stale, ARCH_CONVERT); /* INT_: type change */
89
leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
90
leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
88
92
* Could compact these but I think we always do the conversion
89
93
* after squeezing out stale entries.
91
memcpy(leaf->ents, blp, INT_GET(btp->count, ARCH_CONVERT) * sizeof(xfs_dir2_leaf_entry_t));
92
xfs_dir2_leaf_log_ents(tp, lbp, 0, INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1);
95
memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
96
xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
105
109
* Fix up the block header, make it a data block.
107
INT_SET(block->hdr.magic, ARCH_CONVERT, XFS_DIR2_DATA_MAGIC);
111
block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
109
xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog,
113
xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
112
115
* Set up leaf tail and bests table.
114
ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
115
INT_SET(ltp->bestcount, ARCH_CONVERT, 1);
116
bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
117
INT_COPY(bestsp[0], block->hdr.bestfree[0].length, ARCH_CONVERT);
117
ltp = xfs_dir2_leaf_tail_p(mp, leaf);
118
ltp->bestcount = cpu_to_be32(1);
119
bestsp = xfs_dir2_leaf_bests_p(ltp);
120
bestsp[0] = block->hdr.bestfree[0].length;
119
122
* Log the data header and leaf bests table.
184
187
index = xfs_dir2_leaf_search_hash(args, lbp);
185
188
leaf = lbp->data;
186
ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
187
bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
188
length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
189
ltp = xfs_dir2_leaf_tail_p(mp, leaf);
190
bestsp = xfs_dir2_leaf_bests_p(ltp);
191
length = xfs_dir2_data_entsize(args->namelen);
190
193
* See if there are any entries with the same hash value
191
194
* and space in their block for the new entry.
193
196
* in a data block, improving the lookup of those entries.
195
198
for (use_block = -1, lep = &leaf->ents[index];
196
index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval;
199
index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
197
200
index++, lep++) {
198
if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
201
if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
200
i = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
201
ASSERT(i < INT_GET(ltp->bestcount, ARCH_CONVERT));
202
ASSERT(INT_GET(bestsp[i], ARCH_CONVERT) != NULLDATAOFF);
203
if (INT_GET(bestsp[i], ARCH_CONVERT) >= length) {
203
i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
204
ASSERT(i < be32_to_cpu(ltp->bestcount));
205
ASSERT(be16_to_cpu(bestsp[i]) != NULLDATAOFF);
206
if (be16_to_cpu(bestsp[i]) >= length) {
209
212
* Didn't find a block yet, linear search all the data blocks.
211
214
if (use_block == -1) {
212
for (i = 0; i < INT_GET(ltp->bestcount, ARCH_CONVERT); i++) {
215
for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
214
217
* Remember a block we see that's missing.
216
if (INT_GET(bestsp[i], ARCH_CONVERT) == NULLDATAOFF && use_block == -1)
219
if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1)
218
else if (INT_GET(bestsp[i], ARCH_CONVERT) >= length) {
221
else if (be16_to_cpu(bestsp[i]) >= length) {
231
234
* Now kill use_block if it refers to a missing block, so we
232
235
* can use it as an indication of allocation needed.
234
if (use_block != -1 && INT_GET(bestsp[use_block], ARCH_CONVERT) == NULLDATAOFF)
237
if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF)
237
240
* If we don't have enough free bytes but we can make enough
238
241
* by compacting out stale entries, we'll do that.
240
if ((char *)bestsp - (char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] < needbytes &&
241
INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1) {
243
if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
244
needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
245
248
* Otherwise if we don't have enough free bytes we need to
246
249
* convert to node form.
248
else if ((char *)bestsp - (char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] <
251
else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
252
leaf->hdr.count)] < needbytes) {
251
254
* Just checking or no space reservation, give up.
253
if (args->justcheck || args->total == 0) {
256
if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
254
258
xfs_da_brelse(tp, lbp);
255
259
return XFS_ERROR(ENOSPC);
329
333
* If we're adding a new data block on the end we need to
330
334
* extend the bests table. Copy it up one entry.
332
if (use_block >= INT_GET(ltp->bestcount, ARCH_CONVERT)) {
336
if (use_block >= be32_to_cpu(ltp->bestcount)) {
334
338
memmove(&bestsp[0], &bestsp[1],
335
INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(bestsp[0]));
336
INT_MOD(ltp->bestcount, ARCH_CONVERT, +1);
339
be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
340
be32_add_cpu(<p->bestcount, 1);
337
341
xfs_dir2_leaf_log_tail(tp, lbp);
338
xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
342
xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
341
345
* If we're filling in a previously empty block just log it.
378
382
* Initialize our new entry (at last).
380
384
dep = (xfs_dir2_data_entry_t *)dup;
381
INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
385
dep->inumber = cpu_to_be64(args->inumber);
382
386
dep->namelen = args->namelen;
383
387
memcpy(dep->name, args->name, dep->namelen);
384
tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
385
INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)data));
388
tagp = xfs_dir2_data_entry_tag_p(dep);
389
*tagp = cpu_to_be16((char *)dep - (char *)data);
387
391
* Need to scan fix up the bestfree table.
390
xfs_dir2_data_freescan(mp, data, &needlog, NULL);
394
xfs_dir2_data_freescan(mp, data, &needlog);
392
396
* Need to log the data block's header.
398
402
* If the bests table needs to be changed, do it.
399
403
* Log the change unless we've already done that.
401
if (INT_GET(bestsp[use_block], ARCH_CONVERT) != INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) {
402
INT_COPY(bestsp[use_block], data->hdr.bestfree[0].length, ARCH_CONVERT);
405
if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
406
bestsp[use_block] = data->hdr.bestfree[0].length;
404
408
xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
412
416
* lep is still good as the index leaf entry.
414
if (index < INT_GET(leaf->hdr.count, ARCH_CONVERT))
418
if (index < be16_to_cpu(leaf->hdr.count))
415
419
memmove(lep + 1, lep,
416
(INT_GET(leaf->hdr.count, ARCH_CONVERT) - index) * sizeof(*lep));
420
(be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
418
422
* Record low and high logging indices for the leaf.
420
424
lfloglow = index;
421
lfloghigh = INT_GET(leaf->hdr.count, ARCH_CONVERT);
422
INT_MOD(leaf->hdr.count, ARCH_CONVERT, +1);
425
lfloghigh = be16_to_cpu(leaf->hdr.count);
426
be16_add_cpu(&leaf->hdr.count, 1);
425
429
* There are stale entries.
497
501
lfloglow = MIN(index, lfloglow);
498
502
lfloghigh = MAX(highstale, lfloghigh);
500
INT_MOD(leaf->hdr.stale, ARCH_CONVERT, -1);
504
be16_add_cpu(&leaf->hdr.stale, -1);
503
507
* Fill in the new leaf entry.
505
INT_SET(lep->hashval, ARCH_CONVERT, args->hashval);
506
INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_DB_OFF_TO_DATAPTR(mp, use_block, INT_GET(*tagp, ARCH_CONVERT)));
509
lep->hashval = cpu_to_be32(args->hashval);
510
lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
511
be16_to_cpu(*tagp)));
508
513
* Log the leaf fields and give up the buffers.
536
541
mp = dp->i_mount;
537
ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC);
542
ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
539
544
* This value is not restrictive enough.
540
545
* Should factor in the size of the bests table as well.
541
546
* We can deduce a value for that from di_size.
543
ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) <= XFS_DIR2_MAX_LEAF_ENTS(mp));
544
ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
548
ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
549
ltp = xfs_dir2_leaf_tail_p(mp, leaf);
546
551
* Leaves and bests don't overlap.
548
ASSERT((char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] <=
549
(char *)XFS_DIR2_LEAF_BESTS_P(ltp));
553
ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
554
(char *)xfs_dir2_leaf_bests_p(ltp));
551
556
* Check hash value order, count stale entries.
553
for (i = stale = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); i++) {
554
if (i + 1 < INT_GET(leaf->hdr.count, ARCH_CONVERT))
555
ASSERT(INT_GET(leaf->ents[i].hashval, ARCH_CONVERT) <=
556
INT_GET(leaf->ents[i + 1].hashval, ARCH_CONVERT));
557
if (INT_GET(leaf->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
558
for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
559
if (i + 1 < be16_to_cpu(leaf->hdr.count))
560
ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
561
be32_to_cpu(leaf->ents[i + 1].hashval));
562
if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
560
ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == stale);
565
ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
562
567
#endif /* DEBUG */
777
782
* Log the bests entries indicated from a leaf1 block.
780
785
xfs_dir2_leaf_log_bests(
781
786
xfs_trans_t *tp, /* transaction pointer */
782
787
xfs_dabuf_t *bp, /* leaf buffer */
783
788
int first, /* first entry to log */
784
789
int last) /* last entry to log */
786
xfs_dir2_data_off_t *firstb; /* pointer to first entry */
787
xfs_dir2_data_off_t *lastb; /* pointer to last entry */
791
__be16 *firstb; /* pointer to first entry */
792
__be16 *lastb; /* pointer to last entry */
788
793
xfs_dir2_leaf_t *leaf; /* leaf structure */
789
794
xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
792
ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC);
793
ltp = XFS_DIR2_LEAF_TAIL_P(tp->t_mountp, leaf);
794
firstb = XFS_DIR2_LEAF_BESTS_P(ltp) + first;
795
lastb = XFS_DIR2_LEAF_BESTS_P(ltp) + last;
797
ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
798
ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
799
firstb = xfs_dir2_leaf_bests_p(ltp) + first;
800
lastb = xfs_dir2_leaf_bests_p(ltp) + last;
796
801
xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
797
802
(uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
812
817
xfs_dir2_leaf_t *leaf; /* leaf structure */
815
ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC ||
816
INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
820
ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
821
be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
817
822
firstlep = &leaf->ents[first];
818
823
lastlep = &leaf->ents[last];
819
824
xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
897
902
dep = (xfs_dir2_data_entry_t *)
898
903
((char *)dbp->data +
899
XFS_DIR2_DATAPTR_TO_OFF(dp->i_mount, INT_GET(lep->address, ARCH_CONVERT)));
904
xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
901
* Return the found inode number.
906
* Return the found inode number & CI name if appropriate
903
args->inumber = INT_GET(dep->inumber, ARCH_CONVERT);
908
args->inumber = be64_to_cpu(dep->inumber);
909
error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
904
910
xfs_da_brelse(tp, dbp);
905
911
xfs_da_brelse(tp, lbp);
906
return XFS_ERROR(EEXIST);
912
return XFS_ERROR(error);
912
918
* If not found dbpp will be NULL, and ENOENT comes back.
913
919
* lbpp will always be filled in with the leaf buffer unless there's an error.
915
STATIC int /* error */
921
static int /* error */
916
922
xfs_dir2_leaf_lookup_int(
917
923
xfs_da_args_t *args, /* operation arguments */
918
924
xfs_dabuf_t **lbpp, /* out: leaf buffer */
919
925
int *indexp, /* out: index in leaf block */
920
926
xfs_dabuf_t **dbpp) /* out: data buffer */
922
xfs_dir2_db_t curdb; /* current data block number */
923
xfs_dabuf_t *dbp; /* data buffer */
928
xfs_dir2_db_t curdb = -1; /* current data block number */
929
xfs_dabuf_t *dbp = NULL; /* data buffer */
924
930
xfs_dir2_data_entry_t *dep; /* data entry */
925
931
xfs_inode_t *dp; /* incore directory inode */
926
932
int error; /* error return code */
954
961
* Loop over all the entries with the right hash value
955
962
* looking to match the name.
957
for (lep = &leaf->ents[index], dbp = NULL, curdb = -1;
958
index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval;
964
for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
965
be32_to_cpu(lep->hashval) == args->hashval;
961
968
* Skip over stale leaf entries.
963
if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
970
if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
966
973
* Get the new data block number.
968
newdb = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
975
newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
970
977
* If it's not the same as the old data block number,
971
978
* need to pitch the old one and read the new one.
987
994
* Point to the data entry.
989
dep = (xfs_dir2_data_entry_t *)
991
XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT)));
996
dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
997
xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
993
* If it matches then return it.
999
* Compare name and if it's an exact match, return the index
1000
* and buffer. If it's the first case-insensitive match, store
1001
* the index and buffer and continue looking for an exact match.
995
if (dep->namelen == args->namelen &&
996
dep->name[0] == args->name[0] &&
997
memcmp(dep->name, args->name, args->namelen) == 0) {
1003
cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1004
if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1005
args->cmpresult = cmp;
999
1006
*indexp = index;
1007
/* case exact match: return the current buffer. */
1008
if (cmp == XFS_CMP_EXACT) {
1015
ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1017
* Here, we can only be doing a lookup (not a rename or remove).
1018
* If a case-insensitive match was found earlier, re-read the
1019
* appropriate data block if required and return it.
1021
if (args->cmpresult == XFS_CMP_CASE) {
1023
if (cidb != curdb) {
1024
xfs_da_brelse(tp, dbp);
1025
error = xfs_da_read_buf(tp, dp,
1026
xfs_dir2_db_to_da(mp, cidb),
1027
-1, &dbp, XFS_DATA_FORK);
1029
xfs_da_brelse(tp, lbp);
1004
1037
* No match found, return ENOENT.
1006
ASSERT(args->oknoent);
1008
1041
xfs_da_brelse(tp, dbp);
1009
1042
xfs_da_brelse(tp, lbp);
1053
1086
* Point to the leaf entry, use that to point to the data entry.
1055
1088
lep = &leaf->ents[index];
1056
db = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
1089
db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1057
1090
dep = (xfs_dir2_data_entry_t *)
1058
((char *)data + XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT)));
1091
((char *)data + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1059
1092
needscan = needlog = 0;
1060
oldbest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT);
1061
ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
1062
bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
1063
ASSERT(INT_GET(bestsp[db], ARCH_CONVERT) == oldbest);
1093
oldbest = be16_to_cpu(data->hdr.bestfree[0].length);
1094
ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1095
bestsp = xfs_dir2_leaf_bests_p(ltp);
1096
ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1065
1098
* Mark the former data entry unused.
1067
1100
xfs_dir2_data_make_free(tp, dbp,
1068
1101
(xfs_dir2_data_aoff_t)((char *)dep - (char *)data),
1069
XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
1102
xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1071
1104
* We just mark the leaf entry stale by putting a null in it.
1073
INT_MOD(leaf->hdr.stale, ARCH_CONVERT, +1);
1106
be16_add_cpu(&leaf->hdr.stale, 1);
1074
1107
xfs_dir2_leaf_log_header(tp, lbp);
1075
INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_NULL_DATAPTR);
1108
lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1076
1109
xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1078
1111
* Scan the freespace in the data block again if necessary,
1079
1112
* log the data block header if necessary.
1082
xfs_dir2_data_freescan(mp, data, &needlog, NULL);
1115
xfs_dir2_data_freescan(mp, data, &needlog);
1084
1117
xfs_dir2_data_log_header(tp, dbp);
1086
1119
* If the longest freespace in the data block has changed,
1087
1120
* put the new value in the bests table and log that.
1089
if (INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) != oldbest) {
1090
INT_COPY(bestsp[db], data->hdr.bestfree[0].length, ARCH_CONVERT);
1122
if (be16_to_cpu(data->hdr.bestfree[0].length) != oldbest) {
1123
bestsp[db] = data->hdr.bestfree[0].length;
1091
1124
xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1093
1126
xfs_dir2_data_check(dp, dbp);
1095
1128
* If the data block is now empty then get rid of the data block.
1097
if (INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) ==
1130
if (be16_to_cpu(data->hdr.bestfree[0].length) ==
1098
1131
mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1099
1132
ASSERT(db != mp->m_dirdatablk);
1100
1133
if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1130
1163
* end are removed.
1132
1165
memmove(&bestsp[db - i], bestsp,
1133
(INT_GET(ltp->bestcount, ARCH_CONVERT) - (db - i)) * sizeof(*bestsp));
1134
INT_MOD(ltp->bestcount, ARCH_CONVERT, -(db - i));
1166
(be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1167
be32_add_cpu(<p->bestcount, -(db - i));
1135
1168
xfs_dir2_leaf_log_tail(tp, lbp);
1136
xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
1169
xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1138
INT_SET(bestsp[db], ARCH_CONVERT, NULLDATAOFF);
1171
bestsp[db] = cpu_to_be16(NULLDATAOFF);
1141
1174
* If the data block was not the first one, drop it.
1187
1220
dep = (xfs_dir2_data_entry_t *)
1188
1221
((char *)dbp->data +
1189
XFS_DIR2_DATAPTR_TO_OFF(dp->i_mount, INT_GET(lep->address, ARCH_CONVERT)));
1190
ASSERT(args->inumber != INT_GET(dep->inumber, ARCH_CONVERT));
1222
xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1223
ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1192
1225
* Put the new inode number in, log it.
1194
INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
1227
dep->inumber = cpu_to_be64(args->inumber);
1195
1228
tp = args->trans;
1196
1229
xfs_dir2_data_log_entry(tp, dbp, dep);
1197
1230
xfs_da_buf_done(dbp);
1227
1260
* Note, the table cannot be empty, so we have to go through the loop.
1228
1261
* Binary search the leaf entries looking for our hash value.
1230
for (lep = leaf->ents, low = 0, high = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1,
1263
for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
1231
1264
hashwant = args->hashval;
1232
1265
low <= high; ) {
1233
1266
mid = (low + high) >> 1;
1234
if ((hash = INT_GET(lep[mid].hashval, ARCH_CONVERT)) == hashwant)
1267
if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1236
1269
if (hash < hashwant)
1283
1316
* Read the offending data block. We need its buffer.
1285
if ((error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, db), -1, &dbp,
1318
if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
1286
1319
XFS_DATA_FORK))) {
1290
1323
data = dbp->data;
1291
ASSERT(INT_GET(data->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC);
1324
ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
1293
1326
/* this seems to be an error
1294
1327
* data is only valid if DEBUG is defined?
1312
1345
* Eliminate the last bests entry from the table.
1314
bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
1315
INT_MOD(ltp->bestcount, ARCH_CONVERT, -1);
1316
memmove(&bestsp[1], &bestsp[0], INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(*bestsp));
1347
bestsp = xfs_dir2_leaf_bests_p(ltp);
1348
be32_add_cpu(<p->bestcount, -1);
1349
memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1317
1350
xfs_dir2_leaf_log_tail(tp, lbp);
1318
xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
1351
xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1398
1431
free = fbp->data;
1399
ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1432
ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1400
1433
ASSERT(!free->hdr.firstdb);
1402
1435
* Now see if the leafn and free data will fit in a leaf1.
1403
1436
* If not, release the buffer and give up.
1405
1438
if ((uint)sizeof(leaf->hdr) +
1406
(INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT)) * (uint)sizeof(leaf->ents[0]) +
1407
INT_GET(free->hdr.nvalid, ARCH_CONVERT) * (uint)sizeof(leaf->bests[0]) +
1439
(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)) * (uint)sizeof(leaf->ents[0]) +
1440
be32_to_cpu(free->hdr.nvalid) * (uint)sizeof(leaf->bests[0]) +
1408
1441
(uint)sizeof(leaf->tail) >
1409
1442
mp->m_dirblksize) {
1410
1443
xfs_da_brelse(tp, fbp);
1414
1447
* If the leaf has any stale entries in it, compress them out.
1415
1448
* The compact routine will log the header.
1417
if (INT_GET(leaf->hdr.stale, ARCH_CONVERT))
1450
if (be16_to_cpu(leaf->hdr.stale))
1418
1451
xfs_dir2_leaf_compact(args, lbp);
1420
1453
xfs_dir2_leaf_log_header(tp, lbp);
1421
INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, XFS_DIR2_LEAF1_MAGIC);
1454
leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
1423
1456
* Set up the leaf tail from the freespace block.
1425
ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
1426
INT_COPY(ltp->bestcount, free->hdr.nvalid, ARCH_CONVERT);
1458
ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1459
ltp->bestcount = free->hdr.nvalid;
1428
1461
* Set up the leaf bests table.
1430
memcpy(XFS_DIR2_LEAF_BESTS_P(ltp), free->bests,
1431
INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(leaf->bests[0]));
1432
xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
1463
memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
1464
be32_to_cpu(ltp->bestcount) * sizeof(leaf->bests[0]));
1465
xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1433
1466
xfs_dir2_leaf_log_tail(tp, lbp);
1434
1467
xfs_dir2_leaf_check(dp, lbp);