~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise-security

« back to all changes in this revision

Viewing changes to fs/xfs/xfs_dir2_leaf.c

  • Committer: Package Import Robot
  • Author(s): Paolo Pisati, Paolo Pisati
  • Date: 2011-12-06 15:56:07 UTC
  • Revision ID: package-import@ubuntu.com-20111206155607-pcf44kv5fmhk564f
Tags: 3.2.0-1401.1
[ Paolo Pisati ]

* Rebased on top of Ubuntu-3.2.0-3.8
* Tilt-tracking @ ef2487af4bb15bdd0689631774b5a5e3a59f74e2
* Delete debian.ti-omap4/control, it shoudln't be tracked
* Fix architecture spelling (s/armel/armhf/)
* [Config] Update configs following 3.2 import
* [Config] Fix compilation: disable CODA and ARCH_OMAP3
* [Config] Fix compilation: disable Ethernet Faraday
* Update series to precise

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include "xfs_trans.h"
25
25
#include "xfs_sb.h"
26
26
#include "xfs_ag.h"
27
 
#include "xfs_dir2.h"
28
27
#include "xfs_mount.h"
29
28
#include "xfs_da_btree.h"
30
29
#include "xfs_bmap_btree.h"
31
 
#include "xfs_dir2_sf.h"
32
30
#include "xfs_dinode.h"
33
31
#include "xfs_inode.h"
34
32
#include "xfs_bmap.h"
35
 
#include "xfs_dir2_data.h"
36
 
#include "xfs_dir2_leaf.h"
37
 
#include "xfs_dir2_block.h"
38
 
#include "xfs_dir2_node.h"
 
33
#include "xfs_dir2_format.h"
 
34
#include "xfs_dir2_priv.h"
39
35
#include "xfs_error.h"
40
36
#include "xfs_trace.h"
41
37
 
64
60
{
65
61
        __be16                  *bestsp;        /* leaf's bestsp entries */
66
62
        xfs_dablk_t             blkno;          /* leaf block's bno */
67
 
        xfs_dir2_block_t        *block;         /* block structure */
 
63
        xfs_dir2_data_hdr_t     *hdr;           /* block header */
68
64
        xfs_dir2_leaf_entry_t   *blp;           /* block's leaf entries */
69
65
        xfs_dir2_block_tail_t   *btp;           /* block's tail */
70
66
        xfs_inode_t             *dp;            /* incore directory inode */
101
97
        }
102
98
        ASSERT(lbp != NULL);
103
99
        leaf = lbp->data;
104
 
        block = dbp->data;
 
100
        hdr = dbp->data;
105
101
        xfs_dir2_data_check(dp, dbp);
106
 
        btp = xfs_dir2_block_tail_p(mp, block);
 
102
        btp = xfs_dir2_block_tail_p(mp, hdr);
107
103
        blp = xfs_dir2_block_leaf_p(btp);
108
104
        /*
109
105
         * Set the counts in the leaf header.
123
119
         * tail be free.
124
120
         */
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 -
128
124
                                       (char *)blp),
129
125
                &needlog, &needscan);
130
126
        /*
131
127
         * Fix up the block header, make it a data block.
132
128
         */
133
 
        block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
 
129
        hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
134
130
        if (needscan)
135
 
                xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
 
131
                xfs_dir2_data_freescan(mp, hdr, &needlog);
136
132
        /*
137
133
         * Set up leaf tail and bests table.
138
134
         */
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;
143
139
        /*
144
140
         * Log the data header and leaf bests table.
145
141
         */
152
148
        return 0;
153
149
}
154
150
 
 
151
STATIC void
 
152
xfs_dir2_leaf_find_stale(
 
153
        struct xfs_dir2_leaf    *leaf,
 
154
        int                     index,
 
155
        int                     *lowstale,
 
156
        int                     *highstale)
 
157
{
 
158
        /*
 
159
         * Find the first stale entry before our index, if any.
 
160
         */
 
161
        for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
 
162
                if (leaf->ents[*lowstale].address ==
 
163
                    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
 
164
                        break;
 
165
        }
 
166
 
 
167
        /*
 
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
 
170
         * lowstale.
 
171
         */
 
172
        for (*highstale = index;
 
173
             *highstale < be16_to_cpu(leaf->hdr.count);
 
174
             ++*highstale) {
 
175
                if (leaf->ents[*highstale].address ==
 
176
                    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
 
177
                        break;
 
178
                if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
 
179
                        break;
 
180
        }
 
181
}
 
182
 
 
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 */
 
192
{
 
193
        if (!leaf->hdr.stale) {
 
194
                xfs_dir2_leaf_entry_t   *lep;   /* leaf entry table pointer */
 
195
 
 
196
                /*
 
197
                 * Now we need to make room to insert the leaf entry.
 
198
                 *
 
199
                 * If there are no stale entries, just insert a hole at index.
 
200
                 */
 
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) *
 
205
                                 sizeof(*lep));
 
206
 
 
207
                /*
 
208
                 * Record low and high logging indices for the leaf.
 
209
                 */
 
210
                *lfloglow = index;
 
211
                *lfloghigh = be16_to_cpu(leaf->hdr.count);
 
212
                be16_add_cpu(&leaf->hdr.count, 1);
 
213
                return lep;
 
214
        }
 
215
 
 
216
        /*
 
217
         * There are stale entries.
 
218
         *
 
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.
 
221
         *
 
222
         * If we didn't compact before, we need to find the nearest stale
 
223
         * entries before and after our insertion point.
 
224
         */
 
225
        if (compact == 0)
 
226
                xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
 
227
 
 
228
        /*
 
229
         * If the low one is better, use it.
 
230
         */
 
231
        if (lowstale >= 0 &&
 
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));
 
237
 
 
238
                /*
 
239
                 * Copy entries up to cover the stale entry and make room
 
240
                 * for the new entry.
 
241
                 */
 
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));
 
247
                }
 
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];
 
252
        }
 
253
 
 
254
        /*
 
255
         * The high one is better, so use that one.
 
256
         */
 
257
        ASSERT(highstale - index >= 0);
 
258
        ASSERT(leaf->ents[highstale].address ==
 
259
               cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
 
260
 
 
261
        /*
 
262
         * Copy entries down to cover the stale entry and make room for the
 
263
         * new entry.
 
264
         */
 
265
        if (highstale - index > 0) {
 
266
                memmove(&leaf->ents[index + 1],
 
267
                        &leaf->ents[index],
 
268
                        (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
 
269
        }
 
270
        *lfloglow = MIN(index, *lfloglow);
 
271
        *lfloghigh = MAX(highstale, *lfloghigh);
 
272
        be16_add_cpu(&leaf->hdr.stale, -1);
 
273
        return &leaf->ents[index];
 
274
}
 
275
 
155
276
/*
156
277
 * Add an entry to a leaf form directory.
157
278
 */
161
282
{
162
283
        __be16                  *bestsp;        /* freespace table in leaf */
163
284
        int                     compact;        /* need to compact leaves */
164
 
        xfs_dir2_data_t         *data;          /* data block structure */
 
285
        xfs_dir2_data_hdr_t     *hdr;           /* data block header */
165
286
        xfs_dabuf_t             *dbp;           /* data block buffer */
166
287
        xfs_dir2_data_entry_t   *dep;           /* data block entry */
167
288
        xfs_inode_t             *dp;            /* incore directory inode */
225
346
                        continue;
226
347
                i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
227
348
                ASSERT(i < be32_to_cpu(ltp->bestcount));
228
 
                ASSERT(be16_to_cpu(bestsp[i]) != NULLDATAOFF);
 
349
                ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
229
350
                if (be16_to_cpu(bestsp[i]) >= length) {
230
351
                        use_block = i;
231
352
                        break;
239
360
                        /*
240
361
                         * Remember a block we see that's missing.
241
362
                         */
242
 
                        if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1)
 
363
                        if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
 
364
                            use_block == -1)
243
365
                                use_block = i;
244
366
                        else if (be16_to_cpu(bestsp[i]) >= length) {
245
367
                                use_block = i;
250
372
        /*
251
373
         * How many bytes do we need in the leaf block?
252
374
         */
253
 
        needbytes =
254
 
                (leaf->hdr.stale ? 0 : (uint)sizeof(leaf->ents[0])) +
255
 
                (use_block != -1 ? 0 : (uint)sizeof(leaf->bests[0]));
 
375
        needbytes = 0;
 
376
        if (!leaf->hdr.stale)
 
377
                needbytes += sizeof(xfs_dir2_leaf_entry_t);
 
378
        if (use_block == -1)
 
379
                needbytes += sizeof(xfs_dir2_data_off_t);
 
380
 
256
381
        /*
257
382
         * Now kill use_block if it refers to a missing block, so we
258
383
         * can use it as an indication of allocation needed.
259
384
         */
260
 
        if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF)
 
385
        if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
261
386
                use_block = -1;
262
387
        /*
263
388
         * If we don't have enough free bytes but we can make enough
369
494
                 */
370
495
                else
371
496
                        xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
372
 
                data = dbp->data;
373
 
                bestsp[use_block] = data->hdr.bestfree[0].length;
 
497
                hdr = dbp->data;
 
498
                bestsp[use_block] = hdr->bestfree[0].length;
374
499
                grown = 1;
375
500
        }
376
501
        /*
384
509
                        xfs_da_brelse(tp, lbp);
385
510
                        return error;
386
511
                }
387
 
                data = dbp->data;
 
512
                hdr = dbp->data;
388
513
                grown = 0;
389
514
        }
390
515
        xfs_dir2_data_check(dp, dbp);
392
517
         * Point to the biggest freespace in our data block.
393
518
         */
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;
398
523
        /*
399
524
         * Mark the initial part of our freespace in use for the new entry.
400
525
         */
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);
404
529
        /*
405
530
         * Initialize our new entry (at last).
409
534
        dep->namelen = args->namelen;
410
535
        memcpy(dep->name, args->name, dep->namelen);
411
536
        tagp = xfs_dir2_data_entry_tag_p(dep);
412
 
        *tagp = cpu_to_be16((char *)dep - (char *)data);
 
537
        *tagp = cpu_to_be16((char *)dep - (char *)hdr);
413
538
        /*
414
539
         * Need to scan fix up the bestfree table.
415
540
         */
416
541
        if (needscan)
417
 
                xfs_dir2_data_freescan(mp, data, &needlog);
 
542
                xfs_dir2_data_freescan(mp, hdr, &needlog);
418
543
        /*
419
544
         * Need to log the data block's header.
420
545
         */
425
550
         * If the bests table needs to be changed, do it.
426
551
         * Log the change unless we've already done that.
427
552
         */
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;
430
555
                if (!grown)
431
556
                        xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
432
557
        }
433
 
        /*
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.
436
 
         */
437
 
        if (!leaf->hdr.stale) {
438
 
                /*
439
 
                 * lep is still good as the index leaf entry.
440
 
                 */
441
 
                if (index < be16_to_cpu(leaf->hdr.count))
442
 
                        memmove(lep + 1, lep,
443
 
                                (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
444
 
                /*
445
 
                 * Record low and high logging indices for the leaf.
446
 
                 */
447
 
                lfloglow = index;
448
 
                lfloghigh = be16_to_cpu(leaf->hdr.count);
449
 
                be16_add_cpu(&leaf->hdr.count, 1);
450
 
        }
451
 
        /*
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.
456
 
         */
457
 
        else {
458
 
                /*
459
 
                 * If we didn't compact before, we need to find the nearest
460
 
                 * stale entries before and after our insertion point.
461
 
                 */
462
 
                if (compact == 0) {
463
 
                        /*
464
 
                         * Find the first stale entry before the insertion
465
 
                         * point, if any.
466
 
                         */
467
 
                        for (lowstale = index - 1;
468
 
                             lowstale >= 0 &&
469
 
                                be32_to_cpu(leaf->ents[lowstale].address) !=
470
 
                                XFS_DIR2_NULL_DATAPTR;
471
 
                             lowstale--)
472
 
                                continue;
473
 
                        /*
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.
477
 
                         */
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 &&
482
 
                                (lowstale < 0 ||
483
 
                                 index - lowstale - 1 >= highstale - index);
484
 
                             highstale++)
485
 
                                continue;
486
 
                }
487
 
                /*
488
 
                 * If the low one is better, use it.
489
 
                 */
490
 
                if (lowstale >= 0 &&
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);
496
 
                        /*
497
 
                         * Copy entries up to cover the stale entry
498
 
                         * and make room for the new entry.
499
 
                         */
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);
507
 
                }
508
 
                /*
509
 
                 * The high one is better, so use that one.
510
 
                 */
511
 
                else {
512
 
                        ASSERT(highstale - index >= 0);
513
 
                        ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
514
 
                               XFS_DIR2_NULL_DATAPTR);
515
 
                        /*
516
 
                         * Copy entries down to cover the stale entry
517
 
                         * and make room for the new entry.
518
 
                         */
519
 
                        if (highstale - index > 0)
520
 
                                memmove(&leaf->ents[index + 1],
521
 
                                        &leaf->ents[index],
522
 
                                        (highstale - index) * sizeof(*lep));
523
 
                        lep = &leaf->ents[index];
524
 
                        lfloglow = MIN(index, lfloglow);
525
 
                        lfloghigh = MAX(highstale, lfloghigh);
526
 
                }
527
 
                be16_add_cpu(&leaf->hdr.stale, -1);
528
 
        }
 
558
 
 
559
        lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
 
560
                                       highstale, &lfloglow, &lfloghigh);
 
561
 
529
562
        /*
530
563
         * Fill in the new leaf entry.
531
564
         */
562
595
 
563
596
        leaf = bp->data;
564
597
        mp = dp->i_mount;
565
 
        ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
 
598
        ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
566
599
        /*
567
600
         * This value is not restrictive enough.
568
601
         * Should factor in the size of the bests table as well.
582
615
                if (i + 1 < be16_to_cpu(leaf->hdr.count))
583
616
                        ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
584
617
                               be32_to_cpu(leaf->ents[i + 1].hashval));
585
 
                if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
 
618
                if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
586
619
                        stale++;
587
620
        }
588
621
        ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
611
644
         * Compress out the stale entries in place.
612
645
         */
613
646
        for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
614
 
                if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
 
647
                if (leaf->ents[from].address ==
 
648
                    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
615
649
                        continue;
616
650
                /*
617
651
                 * Only actually copy the entries that are different.
663
697
        leaf = bp->data;
664
698
        ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
665
699
        index = *indexp;
666
 
        /*
667
 
         * Find the first stale entry before our index, if any.
668
 
         */
669
 
        for (lowstale = index - 1;
670
 
             lowstale >= 0 &&
671
 
                be32_to_cpu(leaf->ents[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
672
 
             lowstale--)
673
 
                continue;
674
 
        /*
675
 
         * Find the first stale entry at or after our index, if any.
676
 
         * Stop if the answer would be worse than lowstale.
677
 
         */
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);
682
 
             highstale++)
683
 
                continue;
 
700
 
 
701
        xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
 
702
 
684
703
        /*
685
704
         * Pick the better of lowstale and highstale.
686
705
         */
701
720
                if (index == from)
702
721
                        newindex = to;
703
722
                if (from != keepstale &&
704
 
                    be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) {
 
723
                    leaf->ents[from].address ==
 
724
                    cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
705
725
                        if (from == to)
706
726
                                *lowlogp = to;
707
727
                        continue;
760
780
        int                     byteoff;        /* offset in current block */
761
781
        xfs_dir2_db_t           curdb;          /* db for current block */
762
782
        xfs_dir2_off_t          curoff;         /* current overall offset */
763
 
        xfs_dir2_data_t         *data;          /* data block structure */
 
783
        xfs_dir2_data_hdr_t     *hdr;           /* data block header */
764
784
        xfs_dir2_data_entry_t   *dep;           /* data entry */
765
785
        xfs_dir2_data_unused_t  *dup;           /* unused entry */
766
786
        int                     error = 0;      /* error return value */
868
888
                                 * we already have in the table.
869
889
                                 */
870
890
                                nmap = map_size - map_valid;
871
 
                                error = xfs_bmapi(NULL, dp,
872
 
                                        map_off,
 
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);
877
895
                                /*
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) ==
1020
1038
                                       curdb);
1021
 
                        data = bp->data;
 
1039
                        hdr = bp->data;
1022
1040
                        xfs_dir2_data_check(dp, bp);
1023
1041
                        /*
1024
1042
                         * Find our position in the block.
1025
1043
                         */
1026
 
                        ptr = (char *)&data->u;
 
1044
                        ptr = (char *)(hdr + 1);
1027
1045
                        byteoff = xfs_dir2_byte_to_off(mp, curoff);
1028
1046
                        /*
1029
1047
                         * Skip past the header.
1030
1048
                         */
1031
1049
                        if (byteoff == 0)
1032
 
                                curoff += (uint)sizeof(data->hdr);
 
1050
                                curoff += (uint)sizeof(*hdr);
1033
1051
                        /*
1034
1052
                         * Skip past entries until we reach our offset.
1035
1053
                         */
1036
1054
                        else {
1037
 
                                while ((char *)ptr - (char *)data < byteoff) {
 
1055
                                while ((char *)ptr - (char *)hdr < byteoff) {
1038
1056
                                        dup = (xfs_dir2_data_unused_t *)ptr;
1039
1057
 
1040
1058
                                        if (be16_to_cpu(dup->freetag)
1055
1073
                                curoff =
1056
1074
                                        xfs_dir2_db_off_to_byte(mp,
1057
1075
                                            xfs_dir2_byte_to_db(mp, curoff),
1058
 
                                            (char *)ptr - (char *)data);
1059
 
                                if (ptr >= (char *)data + mp->m_dirblksize) {
 
1076
                                            (char *)ptr - (char *)hdr);
 
1077
                                if (ptr >= (char *)hdr + mp->m_dirblksize) {
1060
1078
                                        continue;
1061
1079
                                }
1062
1080
                        }
1179
1197
        xfs_dir2_leaf_tail_t    *ltp;           /* leaf tail structure */
1180
1198
 
1181
1199
        leaf = bp->data;
1182
 
        ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
 
1200
        ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
1183
1201
        ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1184
1202
        firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1185
1203
        lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1202
1220
        xfs_dir2_leaf_t         *leaf;          /* leaf structure */
1203
1221
 
1204
1222
        leaf = bp->data;
1205
 
        ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1206
 
               be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
 
1223
        ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
 
1224
               leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1207
1225
        firstlep = &leaf->ents[first];
1208
1226
        lastlep = &leaf->ents[last];
1209
1227
        xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1221
1239
        xfs_dir2_leaf_t         *leaf;          /* leaf structure */
1222
1240
 
1223
1241
        leaf = bp->data;
1224
 
        ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1225
 
               be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
 
1242
        ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
 
1243
               leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1226
1244
        xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1227
1245
                (uint)(sizeof(leaf->hdr) - 1));
1228
1246
}
1241
1259
 
1242
1260
        mp = tp->t_mountp;
1243
1261
        leaf = bp->data;
1244
 
        ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
 
1262
        ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
1245
1263
        ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1246
1264
        xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1247
1265
                (uint)(mp->m_dirblksize - 1));
1437
1455
        xfs_da_args_t           *args)          /* operation arguments */
1438
1456
{
1439
1457
        __be16                  *bestsp;        /* leaf block best freespace */
1440
 
        xfs_dir2_data_t         *data;          /* data block structure */
 
1458
        xfs_dir2_data_hdr_t     *hdr;           /* data block header */
1441
1459
        xfs_dir2_db_t           db;             /* data block number */
1442
1460
        xfs_dabuf_t             *dbp;           /* data block buffer */
1443
1461
        xfs_dir2_data_entry_t   *dep;           /* data entry structure */
1467
1485
        tp = args->trans;
1468
1486
        mp = dp->i_mount;
1469
1487
        leaf = lbp->data;
1470
 
        data = dbp->data;
 
1488
        hdr = dbp->data;
1471
1489
        xfs_dir2_data_check(dp, dbp);
1472
1490
        /*
1473
1491
         * Point to the leaf entry, use that to point to the data entry.
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);
1485
1503
         * Mark the former data entry unused.
1486
1504
         */
1487
1505
        xfs_dir2_data_make_free(tp, dbp,
1488
 
                (xfs_dir2_data_aoff_t)((char *)dep - (char *)data),
 
1506
                (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1489
1507
                xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1490
1508
        /*
1491
1509
         * We just mark the leaf entry stale by putting a null in it.
1499
1517
         * log the data block header if necessary.
1500
1518
         */
1501
1519
        if (needscan)
1502
 
                xfs_dir2_data_freescan(mp, data, &needlog);
 
1520
                xfs_dir2_data_freescan(mp, hdr, &needlog);
1503
1521
        if (needlog)
1504
1522
                xfs_dir2_data_log_header(tp, dbp);
1505
1523
        /*
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.
1508
1526
         */
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);
1512
1530
        }
1513
1531
        xfs_dir2_data_check(dp, dbp);
1514
1532
        /*
1515
1533
         * If the data block is now empty then get rid of the data block.
1516
1534
         */
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))) {
1521
1539
                        /*
1542
1560
                         * Look for the last active entry (i).
1543
1561
                         */
1544
1562
                        for (i = db - 1; i > 0; i--) {
1545
 
                                if (be16_to_cpu(bestsp[i]) != NULLDATAOFF)
 
1563
                                if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1546
1564
                                        break;
1547
1565
                        }
1548
1566
                        /*
1686
1704
        xfs_dir2_db_t           db)             /* data block number */
1687
1705
{
1688
1706
        __be16                  *bestsp;        /* leaf bests table */
1689
 
#ifdef DEBUG
1690
 
        xfs_dir2_data_t         *data;          /* data block structure */
1691
 
#endif
1692
1707
        xfs_dabuf_t             *dbp;           /* data block buffer */
1693
1708
        xfs_inode_t             *dp;            /* incore directory inode */
1694
1709
        int                     error;          /* error return value */
1707
1722
                        XFS_DATA_FORK))) {
1708
1723
                return error;
1709
1724
        }
1710
 
#ifdef DEBUG
1711
 
        data = dbp->data;
1712
 
        ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
1713
 
#endif
1714
 
        /* this seems to be an error
1715
 
         * data is only valid if DEBUG is defined?
1716
 
         * RMC 09/08/1999
1717
 
         */
1718
1725
 
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));
 
1728
 
 
1729
#ifdef DEBUG
 
1730
{
 
1731
        struct xfs_dir2_data_hdr *hdr = dbp->data;
 
1732
 
 
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);
 
1737
}
 
1738
#endif
 
1739
 
1724
1740
        /*
1725
1741
         * Get rid of the data block.
1726
1742
         */
1740
1756
        return 0;
1741
1757
}
1742
1758
 
 
1759
static inline size_t
 
1760
xfs_dir2_leaf_size(
 
1761
        struct xfs_dir2_leaf_hdr        *hdr,
 
1762
        int                             counts)
 
1763
{
 
1764
        int                     entries;
 
1765
 
 
1766
        entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
 
1767
        return sizeof(xfs_dir2_leaf_hdr_t) +
 
1768
            entries * sizeof(xfs_dir2_leaf_entry_t) +
 
1769
            counts * sizeof(xfs_dir2_data_off_t) +
 
1770
            sizeof(xfs_dir2_leaf_tail_t);
 
1771
}
 
1772
 
1743
1773
/*
1744
1774
 * Convert node form directory to leaf form directory.
1745
1775
 * The root of the node form dir needs to already be a LEAFN block.
1810
1840
                return 0;
1811
1841
        lbp = state->path.blk[0].bp;
1812
1842
        leaf = lbp->data;
1813
 
        ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
 
1843
        ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1814
1844
        /*
1815
1845
         * Read the freespace block.
1816
1846
         */
1819
1849
                return error;
1820
1850
        }
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);
 
1854
 
1824
1855
        /*
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.
1827
1858
         */
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) >
1832
 
            mp->m_dirblksize) {
 
1859
        if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
 
1860
                        mp->m_dirblksize) {
1833
1861
                xfs_da_brelse(tp, fbp);
1834
1862
                return 0;
1835
1863
        }
 
1864
 
1836
1865
        /*
1837
1866
         * If the leaf has any stale entries in it, compress them out.
1838
1867
         * The compact routine will log the header.
1851
1880
         * Set up the leaf bests table.
1852
1881
         */
1853
1882
        memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
1854
 
                be32_to_cpu(ltp->bestcount) * sizeof(leaf->bests[0]));
 
1883
                be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
1855
1884
        xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1856
1885
        xfs_dir2_leaf_log_tail(tp, lbp);
1857
1886
        xfs_dir2_leaf_check(dp, lbp);