~ubuntu-branches/ubuntu/utopic/xfsprogs/utopic-proposed

« back to all changes in this revision

Viewing changes to libxfs/xfs_dir2_leaf.c

  • Committer: Bazaar Package Importer
  • Author(s): Nathan Scott
  • Date: 2009-05-06 11:29:18 UTC
  • mfrom: (8.1.1 jaunty)
  • Revision ID: james.westby@ubuntu.com-20090506112918-uzoyzcp90rtr8td7
Tags: 3.0.2
New bugfix release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
 
2
 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3
3
 * All Rights Reserved.
4
4
 *
5
5
 * This program is free software; you can redistribute it and/or
16
16
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
17
 */
18
18
 
19
 
/*
20
 
 * xfs_dir2_leaf.c
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.
25
 
 */
26
 
 
27
19
#include <xfs.h>
28
20
 
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 *);
 
21
/*
 
22
 * Local function declarations.
 
23
 */
 
24
#ifdef DEBUG
 
25
static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
 
26
#else
 
27
#define xfs_dir2_leaf_check(dp, bp)
 
28
#endif
 
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,
 
32
                                    int first, int last);
 
33
static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
 
34
 
31
35
 
32
36
/*
33
37
 * Convert a block form directory to a leaf form directory.
37
41
        xfs_da_args_t           *args,          /* operation arguments */
38
42
        xfs_dabuf_t             *dbp)           /* input block's buffer */
39
43
{
40
 
        xfs_dir2_data_off_t     *bestsp;        /* leaf's bestsp entries */
 
44
        __be16                  *bestsp;        /* leaf's bestsp entries */
41
45
        xfs_dablk_t             blkno;          /* leaf block's bno */
42
46
        xfs_dir2_block_t        *block;         /* block structure */
43
47
        xfs_dir2_leaf_entry_t   *blp;           /* block's leaf entries */
65
69
        if ((error = xfs_da_grow_inode(args, &blkno))) {
66
70
                return error;
67
71
        }
68
 
        ldb = XFS_DIR2_DA_TO_DB(mp, blkno);
 
72
        ldb = xfs_dir2_da_to_db(mp, blkno);
69
73
        ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
70
74
        /*
71
75
         * Initialize the leaf block, get a buffer for it.
77
81
        leaf = lbp->data;
78
82
        block = dbp->data;
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);
82
86
        /*
83
87
         * Set the counts in the leaf header.
84
88
         */
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));
87
91
        /*
88
92
         * Could compact these but I think we always do the conversion
89
93
         * after squeezing out stale entries.
90
94
         */
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);
93
97
        needscan = 0;
94
98
        needlog = 1;
95
99
        /*
104
108
        /*
105
109
         * Fix up the block header, make it a data block.
106
110
         */
107
 
        INT_SET(block->hdr.magic, ARCH_CONVERT, XFS_DIR2_DATA_MAGIC);
 
111
        block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
108
112
        if (needscan)
109
 
                xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog,
110
 
                        NULL);
 
113
                xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
111
114
        /*
112
115
         * Set up leaf tail and bests table.
113
116
         */
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;
118
121
        /*
119
122
         * Log the data header and leaf bests table.
120
123
         */
134
137
xfs_dir2_leaf_addname(
135
138
        xfs_da_args_t           *args)          /* operation arguments */
136
139
{
137
 
        xfs_dir2_data_off_t     *bestsp;        /* freespace table in leaf */
 
140
        __be16                  *bestsp;        /* freespace table in leaf */
138
141
        int                     compact;        /* need to compact leaves */
139
142
        xfs_dir2_data_t         *data;          /* data block structure */
140
143
        xfs_dabuf_t             *dbp;           /* data block buffer */
158
161
        int                     needbytes;      /* leaf block bytes needed */
159
162
        int                     needlog;        /* need to log data header */
160
163
        int                     needscan;       /* need to rescan data free */
161
 
        xfs_dir2_data_off_t     *tagp;          /* end of data entry */
 
164
        __be16                  *tagp;          /* end of data entry */
162
165
        xfs_trans_t             *tp;            /* transaction pointer */
163
166
        xfs_dir2_db_t           use_block;      /* data block number */
164
167
 
183
186
         */
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);
189
192
        /*
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.
194
197
         */
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)
199
202
                        continue;
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) {
204
207
                        use_block = i;
205
208
                        break;
206
209
                }
209
212
         * Didn't find a block yet, linear search all the data blocks.
210
213
         */
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++) {
213
216
                        /*
214
217
                         * Remember a block we see that's missing.
215
218
                         */
216
 
                        if (INT_GET(bestsp[i], ARCH_CONVERT) == NULLDATAOFF && use_block == -1)
 
219
                        if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1)
217
220
                                use_block = i;
218
 
                        else if (INT_GET(bestsp[i], ARCH_CONVERT) >= length) {
 
221
                        else if (be16_to_cpu(bestsp[i]) >= length) {
219
222
                                use_block = i;
220
223
                                break;
221
224
                        }
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.
233
236
         */
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)
235
238
                use_block = -1;
236
239
        /*
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.
239
242
         */
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) {
242
245
                compact = 1;
243
246
        }
244
247
        /*
245
248
         * Otherwise if we don't have enough free bytes we need to
246
249
         * convert to node form.
247
250
         */
248
 
        else if ((char *)bestsp - (char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] <
249
 
                 needbytes) {
 
251
        else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
 
252
                                                leaf->hdr.count)] < needbytes) {
250
253
                /*
251
254
                 * Just checking or no space reservation, give up.
252
255
                 */
253
 
                if (args->justcheck || args->total == 0) {
 
256
                if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
 
257
                                                        args->total == 0) {
254
258
                        xfs_da_brelse(tp, lbp);
255
259
                        return XFS_ERROR(ENOSPC);
256
260
                }
275
279
         * If just checking, then it will fit unless we needed to allocate
276
280
         * a new data block.
277
281
         */
278
 
        if (args->justcheck) {
 
282
        if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
279
283
                xfs_da_brelse(tp, lbp);
280
284
                return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
281
285
        }
301
305
         * There are stale entries, so we'll need log-low and log-high
302
306
         * impossibly bad values later.
303
307
         */
304
 
        else if (INT_GET(leaf->hdr.stale, ARCH_CONVERT)) {
305
 
                lfloglow = INT_GET(leaf->hdr.count, ARCH_CONVERT);
 
308
        else if (be16_to_cpu(leaf->hdr.stale)) {
 
309
                lfloglow = be16_to_cpu(leaf->hdr.count);
306
310
                lfloghigh = -1;
307
311
        }
308
312
        /*
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.
331
335
                 */
332
 
                if (use_block >= INT_GET(ltp->bestcount, ARCH_CONVERT)) {
 
336
                if (use_block >= be32_to_cpu(ltp->bestcount)) {
333
337
                        bestsp--;
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(&ltp->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);
339
343
                }
340
344
                /*
341
345
                 * If we're filling in a previously empty block just log it.
343
347
                else
344
348
                        xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
345
349
                data = dbp->data;
346
 
                INT_COPY(bestsp[use_block], data->hdr.bestfree[0].length, ARCH_CONVERT);
 
350
                bestsp[use_block] = data->hdr.bestfree[0].length;
347
351
                grown = 1;
348
352
        }
349
353
        /*
352
356
         */
353
357
        else {
354
358
                if ((error =
355
 
                    xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, use_block),
 
359
                    xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
356
360
                            -1, &dbp, XFS_DATA_FORK))) {
357
361
                        xfs_da_brelse(tp, lbp);
358
362
                        return error;
365
369
         * Point to the biggest freespace in our data block.
366
370
         */
367
371
        dup = (xfs_dir2_data_unused_t *)
368
 
              ((char *)data + INT_GET(data->hdr.bestfree[0].offset, ARCH_CONVERT));
369
 
        ASSERT(INT_GET(dup->length, ARCH_CONVERT) >= length);
 
372
              ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
 
373
        ASSERT(be16_to_cpu(dup->length) >= length);
370
374
        needscan = needlog = 0;
371
375
        /*
372
376
         * Mark the initial part of our freespace in use for the new entry.
378
382
         * Initialize our new entry (at last).
379
383
         */
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);
386
390
        /*
387
391
         * Need to scan fix up the bestfree table.
388
392
         */
389
393
        if (needscan)
390
 
                xfs_dir2_data_freescan(mp, data, &needlog, NULL);
 
394
                xfs_dir2_data_freescan(mp, data, &needlog);
391
395
        /*
392
396
         * Need to log the data block's header.
393
397
         */
398
402
         * If the bests table needs to be changed, do it.
399
403
         * Log the change unless we've already done that.
400
404
         */
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;
403
407
                if (!grown)
404
408
                        xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
405
409
        }
411
415
                /*
412
416
                 * lep is still good as the index leaf entry.
413
417
                 */
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));
417
421
                /*
418
422
                 * Record low and high logging indices for the leaf.
419
423
                 */
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);
423
427
        }
424
428
        /*
425
429
         * There are stale entries.
439
443
                         */
440
444
                        for (lowstale = index - 1;
441
445
                             lowstale >= 0 &&
442
 
                                INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) !=
 
446
                                be32_to_cpu(leaf->ents[lowstale].address) !=
443
447
                                XFS_DIR2_NULL_DATAPTR;
444
448
                             lowstale--)
445
449
                                continue;
449
453
                         * lowstale entry would be better.
450
454
                         */
451
455
                        for (highstale = index;
452
 
                             highstale < INT_GET(leaf->hdr.count, ARCH_CONVERT) &&
453
 
                                INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) !=
 
456
                             highstale < be16_to_cpu(leaf->hdr.count) &&
 
457
                                be32_to_cpu(leaf->ents[highstale].address) !=
454
458
                                XFS_DIR2_NULL_DATAPTR &&
455
459
                                (lowstale < 0 ||
456
460
                                 index - lowstale - 1 >= highstale - index);
461
465
                 * If the low one is better, use it.
462
466
                 */
463
467
                if (lowstale >= 0 &&
464
 
                    (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
 
468
                    (highstale == be16_to_cpu(leaf->hdr.count) ||
465
469
                     index - lowstale - 1 < highstale - index)) {
466
470
                        ASSERT(index - lowstale - 1 >= 0);
467
 
                        ASSERT(INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) ==
 
471
                        ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
468
472
                               XFS_DIR2_NULL_DATAPTR);
469
473
                        /*
470
474
                         * Copy entries up to cover the stale entry
483
487
                 */
484
488
                else {
485
489
                        ASSERT(highstale - index >= 0);
486
 
                        ASSERT(INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) ==
 
490
                        ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
487
491
                               XFS_DIR2_NULL_DATAPTR);
488
492
                        /*
489
 
                         * Copy entries down to copver the stale entry
 
493
                         * Copy entries down to cover the stale entry
490
494
                         * and make room for the new entry.
491
495
                         */
492
496
                        if (highstale - index > 0)
497
501
                        lfloglow = MIN(index, lfloglow);
498
502
                        lfloghigh = MAX(highstale, lfloghigh);
499
503
                }
500
 
                INT_MOD(leaf->hdr.stale, ARCH_CONVERT, -1);
 
504
                be16_add_cpu(&leaf->hdr.stale, -1);
501
505
        }
502
506
        /*
503
507
         * Fill in the new leaf entry.
504
508
         */
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)));
507
512
        /*
508
513
         * Log the leaf fields and give up the buffers.
509
514
         */
534
539
 
535
540
        leaf = bp->data;
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);
538
543
        /*
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.
542
547
         */
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);
545
550
        /*
546
551
         * Leaves and bests don't overlap.
547
552
         */
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));
550
555
        /*
551
556
         * Check hash value order, count stale entries.
552
557
         */
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)
558
563
                        stale++;
559
564
        }
560
 
        ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == stale);
 
565
        ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
561
566
}
562
567
#endif  /* DEBUG */
563
568
 
582
587
        /*
583
588
         * Compress out the stale entries in place.
584
589
         */
585
 
        for (from = to = 0, loglow = -1; from < INT_GET(leaf->hdr.count, ARCH_CONVERT); from++) {
586
 
                if (INT_GET(leaf->ents[from].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
 
590
        for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
 
591
                if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
587
592
                        continue;
588
593
                /*
589
594
                 * Only actually copy the entries that are different.
598
603
        /*
599
604
         * Update and log the header, log the leaf entries.
600
605
         */
601
 
        ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == from - to);
602
 
        INT_MOD(leaf->hdr.count, ARCH_CONVERT, -(INT_GET(leaf->hdr.stale, ARCH_CONVERT)));
 
606
        ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
 
607
        be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
603
608
        leaf->hdr.stale = 0;
604
609
        xfs_dir2_leaf_log_header(args->trans, bp);
605
610
        if (loglow != -1)
633
638
        int             to;             /* destination copy index */
634
639
 
635
640
        leaf = bp->data;
636
 
        ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1);
 
641
        ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
637
642
        index = *indexp;
638
643
        /*
639
644
         * Find the first stale entry before our index, if any.
640
645
         */
641
646
        for (lowstale = index - 1;
642
647
             lowstale >= 0 &&
643
 
                INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR;
 
648
                be32_to_cpu(leaf->ents[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
644
649
             lowstale--)
645
650
                continue;
646
651
        /*
648
653
         * Stop if the answer would be worse than lowstale.
649
654
         */
650
655
        for (highstale = index;
651
 
             highstale < INT_GET(leaf->hdr.count, ARCH_CONVERT) &&
652
 
                INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR &&
 
656
             highstale < be16_to_cpu(leaf->hdr.count) &&
 
657
                be32_to_cpu(leaf->ents[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
653
658
                (lowstale < 0 || index - lowstale > highstale - index);
654
659
             highstale++)
655
660
                continue;
657
662
         * Pick the better of lowstale and highstale.
658
663
         */
659
664
        if (lowstale >= 0 &&
660
 
            (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
 
665
            (highstale == be16_to_cpu(leaf->hdr.count) ||
661
666
             index - lowstale <= highstale - index))
662
667
                keepstale = lowstale;
663
668
        else
666
671
         * Copy the entries in place, removing all the stale entries
667
672
         * except keepstale.
668
673
         */
669
 
        for (from = to = 0; from < INT_GET(leaf->hdr.count, ARCH_CONVERT); from++) {
 
674
        for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
670
675
                /*
671
676
                 * Notice the new value of index.
672
677
                 */
673
678
                if (index == from)
674
679
                        newindex = to;
675
680
                if (from != keepstale &&
676
 
                    INT_GET(leaf->ents[from].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR) {
 
681
                    be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) {
677
682
                        if (from == to)
678
683
                                *lowlogp = to;
679
684
                        continue;
701
706
        /*
702
707
         * Adjust the leaf header values.
703
708
         */
704
 
        INT_MOD(leaf->hdr.count, ARCH_CONVERT, -(from - to));
705
 
        INT_SET(leaf->hdr.stale, ARCH_CONVERT, 1);
 
709
        be16_add_cpu(&leaf->hdr.count, -(from - to));
 
710
        leaf->hdr.stale = cpu_to_be16(1);
706
711
        /*
707
712
         * Remember the low/high stale value only in the "right"
708
713
         * direction.
710
715
        if (lowstale >= newindex)
711
716
                lowstale = -1;
712
717
        else
713
 
                highstale = INT_GET(leaf->hdr.count, ARCH_CONVERT);
714
 
        *highlogp = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1;
 
718
                highstale = be16_to_cpu(leaf->hdr.count);
 
719
        *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
715
720
        *lowstalep = lowstale;
716
721
        *highstalep = highstale;
717
722
}
743
748
        /*
744
749
         * Get the buffer for the block.
745
750
         */
746
 
        error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, bno), -1, &bp,
 
751
        error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
747
752
                XFS_DATA_FORK);
748
753
        if (error) {
749
754
                return error;
753
758
        /*
754
759
         * Initialize the header.
755
760
         */
756
 
        INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, magic);
 
761
        leaf->hdr.info.magic = cpu_to_be16(magic);
757
762
        leaf->hdr.info.forw = 0;
758
763
        leaf->hdr.info.back = 0;
759
764
        leaf->hdr.count = 0;
765
770
         * the block.
766
771
         */
767
772
        if (magic == XFS_DIR2_LEAF1_MAGIC) {
768
 
                ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
 
773
                ltp = xfs_dir2_leaf_tail_p(mp, leaf);
769
774
                ltp->bestcount = 0;
770
775
                xfs_dir2_leaf_log_tail(tp, bp);
771
776
        }
776
781
/*
777
782
 * Log the bests entries indicated from a leaf1 block.
778
783
 */
779
 
void
 
784
static void
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 */
785
790
{
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 */
790
795
 
791
796
        leaf = bp->data;
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));
798
803
}
812
817
        xfs_dir2_leaf_t         *leaf;          /* leaf structure */
813
818
 
814
819
        leaf = bp->data;
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),
831
836
        xfs_dir2_leaf_t         *leaf;          /* leaf structure */
832
837
 
833
838
        leaf = bp->data;
834
 
        ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC ||
835
 
               INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
 
839
        ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
 
840
               be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
836
841
        xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
837
842
                (uint)(sizeof(leaf->hdr) - 1));
838
843
}
840
845
/*
841
846
 * Log the tail of the leaf1 block.
842
847
 */
843
 
static void
 
848
STATIC void
844
849
xfs_dir2_leaf_log_tail(
845
850
        xfs_trans_t             *tp,            /* transaction pointer */
846
851
        xfs_dabuf_t             *bp)            /* leaf buffer */
851
856
 
852
857
        mp = tp->t_mountp;
853
858
        leaf = bp->data;
854
 
        ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC);
855
 
        ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
 
859
        ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
 
860
        ltp = xfs_dir2_leaf_tail_p(mp, leaf);
856
861
        xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
857
862
                (uint)(mp->m_dirblksize - 1));
858
863
}
896
901
         */
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)));
900
905
        /*
901
 
         * Return the found inode number.
 
906
         * Return the found inode number & CI name if appropriate
902
907
         */
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);
907
913
}
908
914
 
909
915
/*
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.
914
920
 */
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 */
921
927
{
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 */
931
937
        xfs_mount_t             *mp;            /* filesystem mount point */
932
938
        xfs_dir2_db_t           newdb;          /* new data block number */
933
939
        xfs_trans_t             *tp;            /* transaction pointer */
 
940
        xfs_dir2_db_t           cidb = -1;      /* case match data block no. */
 
941
        enum xfs_dacmp          cmp;            /* name compare result */
934
942
 
935
943
        dp = args->dp;
936
944
        tp = args->trans;
938
946
        /*
939
947
         * Read the leaf block into the buffer.
940
948
         */
941
 
        if ((error =
942
 
            xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
943
 
                    XFS_DATA_FORK))) {
 
949
        error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
 
950
                                                        XFS_DATA_FORK);
 
951
        if (error)
944
952
                return error;
945
 
        }
946
953
        *lbpp = lbp;
947
954
        leaf = lbp->data;
948
955
        xfs_dir2_leaf_check(dp, lbp);
954
961
         * Loop over all the entries with the right hash value
955
962
         * looking to match the name.
956
963
         */
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;
959
 
             lep++, index++) {
 
964
        for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
 
965
                                be32_to_cpu(lep->hashval) == args->hashval;
 
966
                                lep++, index++) {
960
967
                /*
961
968
                 * Skip over stale leaf entries.
962
969
                 */
963
 
                if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
 
970
                if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
964
971
                        continue;
965
972
                /*
966
973
                 * Get the new data block number.
967
974
                 */
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));
969
976
                /*
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.
973
980
                if (newdb != curdb) {
974
981
                        if (dbp)
975
982
                                xfs_da_brelse(tp, dbp);
976
 
                        if ((error =
977
 
                            xfs_da_read_buf(tp, dp,
978
 
                                    XFS_DIR2_DB_TO_DA(mp, newdb), -1, &dbp,
979
 
                                    XFS_DATA_FORK))) {
 
983
                        error = xfs_da_read_buf(tp, dp,
 
984
                                                xfs_dir2_db_to_da(mp, newdb),
 
985
                                                -1, &dbp, XFS_DATA_FORK);
 
986
                        if (error) {
980
987
                                xfs_da_brelse(tp, lbp);
981
988
                                return error;
982
989
                        }
986
993
                /*
987
994
                 * Point to the data entry.
988
995
                 */
989
 
                dep = (xfs_dir2_data_entry_t *)
990
 
                      ((char *)dbp->data +
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)));
992
998
                /*
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.
994
1002
                 */
995
 
                if (dep->namelen == args->namelen &&
996
 
                    dep->name[0] == args->name[0] &&
997
 
                    memcmp(dep->name, args->name, args->namelen) == 0) {
998
 
                        *dbpp = dbp;
 
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;
1000
 
                        return 0;
1001
 
                }
 
1007
                        /* case exact match: return the current buffer. */
 
1008
                        if (cmp == XFS_CMP_EXACT) {
 
1009
                                *dbpp = dbp;
 
1010
                                return 0;
 
1011
                        }
 
1012
                        cidb = curdb;
 
1013
                }
 
1014
        }
 
1015
        ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
 
1016
        /*
 
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.
 
1020
         */
 
1021
        if (args->cmpresult == XFS_CMP_CASE) {
 
1022
                ASSERT(cidb != -1);
 
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);
 
1028
                        if (error) {
 
1029
                                xfs_da_brelse(tp, lbp);
 
1030
                                return error;
 
1031
                        }
 
1032
                }
 
1033
                *dbpp = dbp;
 
1034
                return 0;
1002
1035
        }
1003
1036
        /*
1004
1037
         * No match found, return ENOENT.
1005
1038
         */
1006
 
        ASSERT(args->oknoent);
 
1039
        ASSERT(cidb == -1);
1007
1040
        if (dbp)
1008
1041
                xfs_da_brelse(tp, dbp);
1009
1042
        xfs_da_brelse(tp, lbp);
1017
1050
xfs_dir2_leaf_removename(
1018
1051
        xfs_da_args_t           *args)          /* operation arguments */
1019
1052
{
1020
 
        xfs_dir2_data_off_t     *bestsp;        /* leaf block best freespace */
 
1053
        __be16                  *bestsp;        /* leaf block best freespace */
1021
1054
        xfs_dir2_data_t         *data;          /* data block structure */
1022
1055
        xfs_dir2_db_t           db;             /* data block number */
1023
1056
        xfs_dabuf_t             *dbp;           /* data block buffer */
1053
1086
         * Point to the leaf entry, use that to point to the data entry.
1054
1087
         */
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);
1064
1097
        /*
1065
1098
         * Mark the former data entry unused.
1066
1099
         */
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);
1070
1103
        /*
1071
1104
         * We just mark the leaf entry stale by putting a null in it.
1072
1105
         */
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);
1077
1110
        /*
1078
1111
         * Scan the freespace in the data block again if necessary,
1079
1112
         * log the data block header if necessary.
1080
1113
         */
1081
1114
        if (needscan)
1082
 
                xfs_dir2_data_freescan(mp, data, &needlog, NULL);
 
1115
                xfs_dir2_data_freescan(mp, data, &needlog);
1083
1116
        if (needlog)
1084
1117
                xfs_dir2_data_log_header(tp, dbp);
1085
1118
        /*
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.
1088
1121
         */
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);
1092
1125
        }
1093
1126
        xfs_dir2_data_check(dp, dbp);
1094
1127
        /*
1095
1128
         * If the data block is now empty then get rid of the data block.
1096
1129
         */
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))) {
1117
1150
                 * If this is the last data block then compact the
1118
1151
                 * bests table by getting rid of entries.
1119
1152
                 */
1120
 
                if (db == INT_GET(ltp->bestcount, ARCH_CONVERT) - 1) {
 
1153
                if (db == be32_to_cpu(ltp->bestcount) - 1) {
1121
1154
                        /*
1122
1155
                         * Look for the last active entry (i).
1123
1156
                         */
1124
1157
                        for (i = db - 1; i > 0; i--) {
1125
 
                                if (INT_GET(bestsp[i], ARCH_CONVERT) != NULLDATAOFF)
 
1158
                                if (be16_to_cpu(bestsp[i]) != NULLDATAOFF)
1126
1159
                                        break;
1127
1160
                        }
1128
1161
                        /*
1130
1163
                         * end are removed.
1131
1164
                         */
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(&ltp->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);
1137
1170
                } else
1138
 
                        INT_SET(bestsp[db], ARCH_CONVERT, NULLDATAOFF);
 
1171
                        bestsp[db] = cpu_to_be16(NULLDATAOFF);
1139
1172
        }
1140
1173
        /*
1141
1174
         * If the data block was not the first one, drop it.
1186
1219
         */
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));
1191
1224
        /*
1192
1225
         * Put the new inode number in, log it.
1193
1226
         */
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.
1229
1262
         */
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)
1235
1268
                        break;
1236
1269
                if (hash < hashwant)
1237
1270
                        low = mid + 1;
1242
1275
         * Found one, back up through all the equal hash values.
1243
1276
         */
1244
1277
        if (hash == hashwant) {
1245
 
                while (mid > 0 && INT_GET(lep[mid - 1].hashval, ARCH_CONVERT) == hashwant) {
 
1278
                while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1246
1279
                        mid--;
1247
1280
                }
1248
1281
        }
1264
1297
        xfs_dabuf_t             *lbp,           /* leaf buffer */
1265
1298
        xfs_dir2_db_t           db)             /* data block number */
1266
1299
{
1267
 
        xfs_dir2_data_off_t     *bestsp;        /* leaf bests table */
 
1300
        __be16                  *bestsp;        /* leaf bests table */
1268
1301
#ifdef DEBUG
1269
1302
        xfs_dir2_data_t         *data;          /* data block structure */
1270
1303
#endif
1282
1315
        /*
1283
1316
         * Read the offending data block.  We need its buffer.
1284
1317
         */
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))) {
1287
1320
                return error;
1288
1321
        }
1289
1322
#ifdef DEBUG
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);
1292
1325
#endif
1293
1326
        /* this seems to be an error
1294
1327
         * data is only valid if DEBUG is defined?
1296
1329
         */
1297
1330
 
1298
1331
        leaf = lbp->data;
1299
 
        ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
1300
 
        ASSERT(INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) ==
 
1332
        ltp = xfs_dir2_leaf_tail_p(mp, leaf);
 
1333
        ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) ==
1301
1334
               mp->m_dirblksize - (uint)sizeof(data->hdr));
1302
 
        ASSERT(db == INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
 
1335
        ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1303
1336
        /*
1304
1337
         * Get rid of the data block.
1305
1338
         */
1311
1344
        /*
1312
1345
         * Eliminate the last bests entry from the table.
1313
1346
         */
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(&ltp->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);
1319
1352
        return 0;
1320
1353
}
1321
1354
 
1387
1420
                return 0;
1388
1421
        lbp = state->path.blk[0].bp;
1389
1422
        leaf = lbp->data;
1390
 
        ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
 
1423
        ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1391
1424
        /*
1392
1425
         * Read the freespace block.
1393
1426
         */
1396
1429
                return error;
1397
1430
        }
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);
1401
1434
        /*
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.
1404
1437
         */
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.
1416
1449
         */
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);
1419
1452
        else
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);
1422
1455
        /*
1423
1456
         * Set up the leaf tail from the freespace block.
1424
1457
         */
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;
1427
1460
        /*
1428
1461
         * Set up the leaf bests table.
1429
1462
         */
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);
1435
1468
        /*