1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_bit.h"
22#include "xfs_log.h"
23#include "xfs_inum.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_da_btree.h"
31#include "xfs_bmap_btree.h"
32#include "xfs_attr_sf.h"
33#include "xfs_dir2_sf.h"
34#include "xfs_dinode.h"
35#include "xfs_inode.h"
36#include "xfs_bmap.h"
37#include "xfs_dir2_data.h"
38#include "xfs_dir2_leaf.h"
39#include "xfs_dir2_block.h"
40#include "xfs_dir2_node.h"
41#include "xfs_dir2_trace.h"
42#include "xfs_error.h"
43
44
45
46
47#ifdef DEBUG
48static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
49#else
50#define xfs_dir2_leaf_check(dp, bp)
51#endif
52static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
53 int *indexp, xfs_dabuf_t **dbpp);
54static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
55 int first, int last);
56static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
57
58
59
60
61
62int
63xfs_dir2_block_to_leaf(
64 xfs_da_args_t *args,
65 xfs_dabuf_t *dbp)
66{
67 __be16 *bestsp;
68 xfs_dablk_t blkno;
69 xfs_dir2_block_t *block;
70 xfs_dir2_leaf_entry_t *blp;
71 xfs_dir2_block_tail_t *btp;
72 xfs_inode_t *dp;
73 int error;
74 xfs_dabuf_t *lbp;
75 xfs_dir2_db_t ldb;
76 xfs_dir2_leaf_t *leaf;
77 xfs_dir2_leaf_tail_t *ltp;
78 xfs_mount_t *mp;
79 int needlog;
80 int needscan;
81 xfs_trans_t *tp;
82
83 xfs_dir2_trace_args_b("block_to_leaf", args, dbp);
84 dp = args->dp;
85 mp = dp->i_mount;
86 tp = args->trans;
87
88
89
90
91
92 if ((error = xfs_da_grow_inode(args, &blkno))) {
93 return error;
94 }
95 ldb = xfs_dir2_da_to_db(mp, blkno);
96 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
97
98
99
100 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
101 return error;
102 }
103 ASSERT(lbp != NULL);
104 leaf = lbp->data;
105 block = dbp->data;
106 xfs_dir2_data_check(dp, dbp);
107 btp = xfs_dir2_block_tail_p(mp, block);
108 blp = xfs_dir2_block_leaf_p(btp);
109
110
111
112 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
113 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
114
115
116
117
118 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
119 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
120 needscan = 0;
121 needlog = 1;
122
123
124
125
126 xfs_dir2_data_make_free(tp, dbp,
127 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
128 (xfs_dir2_data_aoff_t)((char *)block + mp->m_dirblksize -
129 (char *)blp),
130 &needlog, &needscan);
131
132
133
134 block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
135 if (needscan)
136 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
137
138
139
140 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
141 ltp->bestcount = cpu_to_be32(1);
142 bestsp = xfs_dir2_leaf_bests_p(ltp);
143 bestsp[0] = block->hdr.bestfree[0].length;
144
145
146
147 if (needlog)
148 xfs_dir2_data_log_header(tp, dbp);
149 xfs_dir2_leaf_check(dp, lbp);
150 xfs_dir2_data_check(dp, dbp);
151 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
152 xfs_da_buf_done(lbp);
153 return 0;
154}
155
156
157
158
159int
160xfs_dir2_leaf_addname(
161 xfs_da_args_t *args)
162{
163 __be16 *bestsp;
164 int compact;
165 xfs_dir2_data_t *data;
166 xfs_dabuf_t *dbp;
167 xfs_dir2_data_entry_t *dep;
168 xfs_inode_t *dp;
169 xfs_dir2_data_unused_t *dup;
170 int error;
171 int grown;
172 int highstale;
173 int i;
174 int index;
175 xfs_dabuf_t *lbp;
176 xfs_dir2_leaf_t *leaf;
177 int length;
178 xfs_dir2_leaf_entry_t *lep;
179 int lfloglow;
180 int lfloghigh;
181 int lowstale;
182 xfs_dir2_leaf_tail_t *ltp;
183 xfs_mount_t *mp;
184 int needbytes;
185 int needlog;
186 int needscan;
187 __be16 *tagp;
188 xfs_trans_t *tp;
189 xfs_dir2_db_t use_block;
190
191 xfs_dir2_trace_args("leaf_addname", args);
192 dp = args->dp;
193 tp = args->trans;
194 mp = dp->i_mount;
195
196
197
198 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
199 XFS_DATA_FORK);
200 if (error) {
201 return error;
202 }
203 ASSERT(lbp != NULL);
204
205
206
207
208
209
210 index = xfs_dir2_leaf_search_hash(args, lbp);
211 leaf = lbp->data;
212 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
213 bestsp = xfs_dir2_leaf_bests_p(ltp);
214 length = xfs_dir2_data_entsize(args->namelen);
215
216
217
218
219
220
221 for (use_block = -1, lep = &leaf->ents[index];
222 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
223 index++, lep++) {
224 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
225 continue;
226 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
227 ASSERT(i < be32_to_cpu(ltp->bestcount));
228 ASSERT(be16_to_cpu(bestsp[i]) != NULLDATAOFF);
229 if (be16_to_cpu(bestsp[i]) >= length) {
230 use_block = i;
231 break;
232 }
233 }
234
235
236
237 if (use_block == -1) {
238 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
239
240
241
242 if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1)
243 use_block = i;
244 else if (be16_to_cpu(bestsp[i]) >= length) {
245 use_block = i;
246 break;
247 }
248 }
249 }
250
251
252
253 needbytes =
254 (leaf->hdr.stale ? 0 : (uint)sizeof(leaf->ents[0])) +
255 (use_block != -1 ? 0 : (uint)sizeof(leaf->bests[0]));
256
257
258
259
260 if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF)
261 use_block = -1;
262
263
264
265
266 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
267 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
268 compact = 1;
269 }
270
271
272
273
274 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
275 leaf->hdr.count)] < needbytes) {
276
277
278
279 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
280 args->total == 0) {
281 xfs_da_brelse(tp, lbp);
282 return XFS_ERROR(ENOSPC);
283 }
284
285
286
287 error = xfs_dir2_leaf_to_node(args, lbp);
288 xfs_da_buf_done(lbp);
289 if (error)
290 return error;
291
292
293
294 return xfs_dir2_node_addname(args);
295 }
296
297
298
299 else
300 compact = 0;
301
302
303
304
305 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
306 xfs_da_brelse(tp, lbp);
307 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
308 }
309
310
311
312
313 if (args->total == 0 && use_block == -1) {
314 xfs_da_brelse(tp, lbp);
315 return XFS_ERROR(ENOSPC);
316 }
317
318
319
320
321
322
323 if (compact) {
324 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
325 &lfloglow, &lfloghigh);
326 }
327
328
329
330
331 else if (be16_to_cpu(leaf->hdr.stale)) {
332 lfloglow = be16_to_cpu(leaf->hdr.count);
333 lfloghigh = -1;
334 }
335
336
337
338
339 if (use_block == -1) {
340
341
342
343 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
344 &use_block))) {
345 xfs_da_brelse(tp, lbp);
346 return error;
347 }
348
349
350
351 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
352 xfs_da_brelse(tp, lbp);
353 return error;
354 }
355
356
357
358
359 if (use_block >= be32_to_cpu(ltp->bestcount)) {
360 bestsp--;
361 memmove(&bestsp[0], &bestsp[1],
362 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
363 be32_add_cpu(<p->bestcount, 1);
364 xfs_dir2_leaf_log_tail(tp, lbp);
365 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
366 }
367
368
369
370 else
371 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
372 data = dbp->data;
373 bestsp[use_block] = data->hdr.bestfree[0].length;
374 grown = 1;
375 }
376
377
378
379
380 else {
381 if ((error =
382 xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
383 -1, &dbp, XFS_DATA_FORK))) {
384 xfs_da_brelse(tp, lbp);
385 return error;
386 }
387 data = dbp->data;
388 grown = 0;
389 }
390 xfs_dir2_data_check(dp, dbp);
391
392
393
394 dup = (xfs_dir2_data_unused_t *)
395 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
396 ASSERT(be16_to_cpu(dup->length) >= length);
397 needscan = needlog = 0;
398
399
400
401 xfs_dir2_data_use_free(tp, dbp, dup,
402 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
403 &needlog, &needscan);
404
405
406
407 dep = (xfs_dir2_data_entry_t *)dup;
408 dep->inumber = cpu_to_be64(args->inumber);
409 dep->namelen = args->namelen;
410 memcpy(dep->name, args->name, dep->namelen);
411 tagp = xfs_dir2_data_entry_tag_p(dep);
412 *tagp = cpu_to_be16((char *)dep - (char *)data);
413
414
415
416 if (needscan)
417 xfs_dir2_data_freescan(mp, data, &needlog);
418
419
420
421 if (needlog)
422 xfs_dir2_data_log_header(tp, dbp);
423 xfs_dir2_data_log_entry(tp, dbp, dep);
424
425
426
427
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;
430 if (!grown)
431 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
432 }
433
434
435
436
437 if (!leaf->hdr.stale) {
438
439
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
446
447 lfloglow = index;
448 lfloghigh = be16_to_cpu(leaf->hdr.count);
449 be16_add_cpu(&leaf->hdr.count, 1);
450 }
451
452
453
454
455
456
457 else {
458
459
460
461
462 if (compact == 0) {
463
464
465
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
475
476
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
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
498
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
510
511 else {
512 ASSERT(highstale - index >= 0);
513 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
514 XFS_DIR2_NULL_DATAPTR);
515
516
517
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 }
529
530
531
532 lep->hashval = cpu_to_be32(args->hashval);
533 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
534 be16_to_cpu(*tagp)));
535
536
537
538 xfs_dir2_leaf_log_header(tp, lbp);
539 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
540 xfs_dir2_leaf_check(dp, lbp);
541 xfs_da_buf_done(lbp);
542 xfs_dir2_data_check(dp, dbp);
543 xfs_da_buf_done(dbp);
544 return 0;
545}
546
547#ifdef DEBUG
548
549
550
551
552void
553xfs_dir2_leaf_check(
554 xfs_inode_t *dp,
555 xfs_dabuf_t *bp)
556{
557 int i;
558 xfs_dir2_leaf_t *leaf;
559 xfs_dir2_leaf_tail_t *ltp;
560 xfs_mount_t *mp;
561 int stale;
562
563 leaf = bp->data;
564 mp = dp->i_mount;
565 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
566
567
568
569
570
571 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
572 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
573
574
575
576 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
577 (char *)xfs_dir2_leaf_bests_p(ltp));
578
579
580
581 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
582 if (i + 1 < be16_to_cpu(leaf->hdr.count))
583 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
584 be32_to_cpu(leaf->ents[i + 1].hashval));
585 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
586 stale++;
587 }
588 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
589}
590#endif
591
592
593
594
595
596void
597xfs_dir2_leaf_compact(
598 xfs_da_args_t *args,
599 xfs_dabuf_t *bp)
600{
601 int from;
602 xfs_dir2_leaf_t *leaf;
603 int loglow;
604 int to;
605
606 leaf = bp->data;
607 if (!leaf->hdr.stale) {
608 return;
609 }
610
611
612
613 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)
615 continue;
616
617
618
619 if (from > to) {
620 if (loglow == -1)
621 loglow = to;
622 leaf->ents[to] = leaf->ents[from];
623 }
624 to++;
625 }
626
627
628
629 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
630 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
631 leaf->hdr.stale = 0;
632 xfs_dir2_leaf_log_header(args->trans, bp);
633 if (loglow != -1)
634 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
635}
636
637
638
639
640
641
642
643
644
645void
646xfs_dir2_leaf_compact_x1(
647 xfs_dabuf_t *bp,
648 int *indexp,
649 int *lowstalep,
650 int *highstalep,
651 int *lowlogp,
652 int *highlogp)
653{
654 int from;
655 int highstale;
656 int index;
657 int keepstale;
658 xfs_dir2_leaf_t *leaf;
659 int lowstale;
660 int newindex=0;
661 int to;
662
663 leaf = bp->data;
664 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
665 index = *indexp;
666
667
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
676
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;
684
685
686
687 if (lowstale >= 0 &&
688 (highstale == be16_to_cpu(leaf->hdr.count) ||
689 index - lowstale <= highstale - index))
690 keepstale = lowstale;
691 else
692 keepstale = highstale;
693
694
695
696
697 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
698
699
700
701 if (index == from)
702 newindex = to;
703 if (from != keepstale &&
704 be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) {
705 if (from == to)
706 *lowlogp = to;
707 continue;
708 }
709
710
711
712 if (from == keepstale)
713 lowstale = highstale = to;
714
715
716
717 if (from > to)
718 leaf->ents[to] = leaf->ents[from];
719 to++;
720 }
721 ASSERT(from > to);
722
723
724
725
726 if (index == from)
727 newindex = to;
728 *indexp = newindex;
729
730
731
732 be16_add_cpu(&leaf->hdr.count, -(from - to));
733 leaf->hdr.stale = cpu_to_be16(1);
734
735
736
737
738 if (lowstale >= newindex)
739 lowstale = -1;
740 else
741 highstale = be16_to_cpu(leaf->hdr.count);
742 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
743 *lowstalep = lowstale;
744 *highstalep = highstale;
745}
746
747
748
749
750
751int
752xfs_dir2_leaf_getdents(
753 xfs_inode_t *dp,
754 void *dirent,
755 size_t bufsize,
756 xfs_off_t *offset,
757 filldir_t filldir)
758{
759 xfs_dabuf_t *bp;
760 int byteoff;
761 xfs_dir2_db_t curdb;
762 xfs_dir2_off_t curoff;
763 xfs_dir2_data_t *data;
764 xfs_dir2_data_entry_t *dep;
765 xfs_dir2_data_unused_t *dup;
766 int error = 0;
767 int i;
768 int j;
769 int length;
770 xfs_bmbt_irec_t *map;
771 xfs_extlen_t map_blocks;
772 xfs_dablk_t map_off;
773 int map_size;
774 int map_valid;
775 xfs_mount_t *mp;
776 xfs_dir2_off_t newoff;
777 int nmap;
778 char *ptr = NULL;
779 int ra_current;
780 int ra_index;
781 int ra_offset;
782 int ra_want;
783 xfs_ino_t ino;
784
785
786
787
788
789 if (*offset >= XFS_DIR2_MAX_DATAPTR)
790 return 0;
791
792 mp = dp->i_mount;
793
794
795
796
797
798
799 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
800 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
801 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
802 bp = NULL;
803
804
805
806
807
808 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
809
810
811
812
813
814 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
815
816
817
818
819 while (curoff < XFS_DIR2_LEAF_OFFSET) {
820
821
822
823
824 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
825
826
827
828
829 if (bp) {
830 xfs_da_brelse(NULL, bp);
831 bp = NULL;
832 map_blocks -= mp->m_dirblkfsbs;
833
834
835
836
837 for (i = mp->m_dirblkfsbs; i > 0; ) {
838 j = MIN((int)map->br_blockcount, i);
839 map->br_blockcount -= j;
840 map->br_startblock += j;
841 map->br_startoff += j;
842
843
844
845
846 if (!map->br_blockcount && --map_valid)
847 memmove(&map[0], &map[1],
848 sizeof(map[0]) *
849 map_valid);
850 i -= j;
851 }
852 }
853
854
855
856 ra_want = howmany(bufsize + mp->m_dirblksize,
857 mp->m_sb.sb_blocksize) - 1;
858
859
860
861
862
863 if (1 + ra_want > map_blocks &&
864 map_off <
865 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
866
867
868
869
870 nmap = map_size - map_valid;
871 error = xfs_bmapi(NULL, dp,
872 map_off,
873 xfs_dir2_byte_to_da(mp,
874 XFS_DIR2_LEAF_OFFSET) - map_off,
875 XFS_BMAPI_METADATA, NULL, 0,
876 &map[map_valid], &nmap, NULL, NULL);
877
878
879
880
881
882
883
884 if (error)
885 break;
886
887
888
889
890
891
892 if (nmap == map_size - map_valid)
893 map_off =
894 map[map_valid + nmap - 1].br_startoff +
895 map[map_valid + nmap - 1].br_blockcount;
896 else
897 map_off =
898 xfs_dir2_byte_to_da(mp,
899 XFS_DIR2_LEAF_OFFSET);
900
901
902
903
904 for (i = map_valid; i < map_valid + nmap; ) {
905 if (map[i].br_startblock ==
906 HOLESTARTBLOCK) {
907 nmap--;
908 length = map_valid + nmap - i;
909 if (length)
910 memmove(&map[i],
911 &map[i + 1],
912 sizeof(map[i]) *
913 length);
914 } else {
915 map_blocks +=
916 map[i].br_blockcount;
917 i++;
918 }
919 }
920 map_valid += nmap;
921 }
922
923
924
925 if (!map_valid) {
926 curoff = xfs_dir2_da_to_byte(mp, map_off);
927 break;
928 }
929
930
931
932
933 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
934 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
935 map->br_blockcount >= mp->m_dirblkfsbs ?
936 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
937 -1,
938 &bp, XFS_DATA_FORK);
939
940
941
942
943 if (error)
944 break;
945
946
947
948
949 if (ra_current)
950 ra_current -= mp->m_dirblkfsbs;
951
952
953
954 for (ra_index = ra_offset = i = 0;
955 ra_want > ra_current && i < map_blocks;
956 i += mp->m_dirblkfsbs) {
957 ASSERT(ra_index < map_valid);
958
959
960
961 if (i > ra_current &&
962 map[ra_index].br_blockcount >=
963 mp->m_dirblkfsbs) {
964 xfs_baread(mp->m_ddev_targp,
965 XFS_FSB_TO_DADDR(mp,
966 map[ra_index].br_startblock +
967 ra_offset),
968 (int)BTOBB(mp->m_dirblksize));
969 ra_current = i;
970 }
971
972
973
974
975
976 else if (i > ra_current) {
977 (void)xfs_da_reada_buf(NULL, dp,
978 map[ra_index].br_startoff +
979 ra_offset, XFS_DATA_FORK);
980 ra_current = i;
981 }
982
983
984
985 for (j = 0; j < mp->m_dirblkfsbs; j++) {
986
987
988
989
990 length = MIN(mp->m_dirblkfsbs,
991 (int)(map[ra_index].br_blockcount -
992 ra_offset));
993 j += length;
994 ra_offset += length;
995
996
997
998
999 if (ra_offset ==
1000 map[ra_index].br_blockcount) {
1001 ra_offset = 0;
1002 ra_index++;
1003 }
1004 }
1005 }
1006
1007
1008
1009 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
1010
1011
1012
1013 if (curoff < newoff)
1014 curoff = newoff;
1015
1016
1017
1018 else if (curoff > newoff)
1019 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
1020 curdb);
1021 data = bp->data;
1022 xfs_dir2_data_check(dp, bp);
1023
1024
1025
1026 ptr = (char *)&data->u;
1027 byteoff = xfs_dir2_byte_to_off(mp, curoff);
1028
1029
1030
1031 if (byteoff == 0)
1032 curoff += (uint)sizeof(data->hdr);
1033
1034
1035
1036 else {
1037 while ((char *)ptr - (char *)data < byteoff) {
1038 dup = (xfs_dir2_data_unused_t *)ptr;
1039
1040 if (be16_to_cpu(dup->freetag)
1041 == XFS_DIR2_DATA_FREE_TAG) {
1042
1043 length = be16_to_cpu(dup->length);
1044 ptr += length;
1045 continue;
1046 }
1047 dep = (xfs_dir2_data_entry_t *)ptr;
1048 length =
1049 xfs_dir2_data_entsize(dep->namelen);
1050 ptr += length;
1051 }
1052
1053
1054
1055 curoff =
1056 xfs_dir2_db_off_to_byte(mp,
1057 xfs_dir2_byte_to_db(mp, curoff),
1058 (char *)ptr - (char *)data);
1059 if (ptr >= (char *)data + mp->m_dirblksize) {
1060 continue;
1061 }
1062 }
1063 }
1064
1065
1066
1067
1068 dup = (xfs_dir2_data_unused_t *)ptr;
1069
1070
1071
1072 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1073 length = be16_to_cpu(dup->length);
1074 ptr += length;
1075 curoff += length;
1076 continue;
1077 }
1078
1079
1080
1081
1082 dep = (xfs_dir2_data_entry_t *)ptr;
1083
1084 length = xfs_dir2_data_entsize(dep->namelen);
1085
1086 ino = be64_to_cpu(dep->inumber);
1087#if XFS_BIG_INUMS
1088 ino += mp->m_inoadd;
1089#endif
1090
1091
1092
1093
1094 if (filldir(dirent, dep->name, dep->namelen,
1095 xfs_dir2_byte_to_dataptr(mp, curoff),
1096 ino, DT_UNKNOWN))
1097 break;
1098
1099
1100
1101
1102 ptr += length;
1103 curoff += length;
1104 bufsize -= length;
1105 }
1106
1107
1108
1109
1110 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
1111 *offset = XFS_DIR2_MAX_DATAPTR;
1112 else
1113 *offset = xfs_dir2_byte_to_dataptr(mp, curoff);
1114 kmem_free(map);
1115 if (bp)
1116 xfs_da_brelse(NULL, bp);
1117 return error;
1118}
1119
1120
1121
1122
1123int
1124xfs_dir2_leaf_init(
1125 xfs_da_args_t *args,
1126 xfs_dir2_db_t bno,
1127 xfs_dabuf_t **bpp,
1128 int magic)
1129{
1130 xfs_dabuf_t *bp;
1131 xfs_inode_t *dp;
1132 int error;
1133 xfs_dir2_leaf_t *leaf;
1134 xfs_dir2_leaf_tail_t *ltp;
1135 xfs_mount_t *mp;
1136 xfs_trans_t *tp;
1137
1138 dp = args->dp;
1139 ASSERT(dp != NULL);
1140 tp = args->trans;
1141 mp = dp->i_mount;
1142 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1143 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1144
1145
1146
1147 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
1148 XFS_DATA_FORK);
1149 if (error) {
1150 return error;
1151 }
1152 ASSERT(bp != NULL);
1153 leaf = bp->data;
1154
1155
1156
1157 leaf->hdr.info.magic = cpu_to_be16(magic);
1158 leaf->hdr.info.forw = 0;
1159 leaf->hdr.info.back = 0;
1160 leaf->hdr.count = 0;
1161 leaf->hdr.stale = 0;
1162 xfs_dir2_leaf_log_header(tp, bp);
1163
1164
1165
1166
1167
1168 if (magic == XFS_DIR2_LEAF1_MAGIC) {
1169 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1170 ltp->bestcount = 0;
1171 xfs_dir2_leaf_log_tail(tp, bp);
1172 }
1173 *bpp = bp;
1174 return 0;
1175}
1176
1177
1178
1179
1180static void
1181xfs_dir2_leaf_log_bests(
1182 xfs_trans_t *tp,
1183 xfs_dabuf_t *bp,
1184 int first,
1185 int last)
1186{
1187 __be16 *firstb;
1188 __be16 *lastb;
1189 xfs_dir2_leaf_t *leaf;
1190 xfs_dir2_leaf_tail_t *ltp;
1191
1192 leaf = bp->data;
1193 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
1194 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1195 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1196 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1197 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1198 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1199}
1200
1201
1202
1203
1204void
1205xfs_dir2_leaf_log_ents(
1206 xfs_trans_t *tp,
1207 xfs_dabuf_t *bp,
1208 int first,
1209 int last)
1210{
1211 xfs_dir2_leaf_entry_t *firstlep;
1212 xfs_dir2_leaf_entry_t *lastlep;
1213 xfs_dir2_leaf_t *leaf;
1214
1215 leaf = bp->data;
1216 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1217 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1218 firstlep = &leaf->ents[first];
1219 lastlep = &leaf->ents[last];
1220 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1221 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1222}
1223
1224
1225
1226
1227void
1228xfs_dir2_leaf_log_header(
1229 xfs_trans_t *tp,
1230 xfs_dabuf_t *bp)
1231{
1232 xfs_dir2_leaf_t *leaf;
1233
1234 leaf = bp->data;
1235 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1236 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1237 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1238 (uint)(sizeof(leaf->hdr) - 1));
1239}
1240
1241
1242
1243
1244STATIC void
1245xfs_dir2_leaf_log_tail(
1246 xfs_trans_t *tp,
1247 xfs_dabuf_t *bp)
1248{
1249 xfs_dir2_leaf_t *leaf;
1250 xfs_dir2_leaf_tail_t *ltp;
1251 xfs_mount_t *mp;
1252
1253 mp = tp->t_mountp;
1254 leaf = bp->data;
1255 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
1256 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1257 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1258 (uint)(mp->m_dirblksize - 1));
1259}
1260
1261
1262
1263
1264
1265
1266int
1267xfs_dir2_leaf_lookup(
1268 xfs_da_args_t *args)
1269{
1270 xfs_dabuf_t *dbp;
1271 xfs_dir2_data_entry_t *dep;
1272 xfs_inode_t *dp;
1273 int error;
1274 int index;
1275 xfs_dabuf_t *lbp;
1276 xfs_dir2_leaf_t *leaf;
1277 xfs_dir2_leaf_entry_t *lep;
1278 xfs_trans_t *tp;
1279
1280 xfs_dir2_trace_args("leaf_lookup", args);
1281
1282
1283
1284 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1285 return error;
1286 }
1287 tp = args->trans;
1288 dp = args->dp;
1289 xfs_dir2_leaf_check(dp, lbp);
1290 leaf = lbp->data;
1291
1292
1293
1294 lep = &leaf->ents[index];
1295
1296
1297
1298 dep = (xfs_dir2_data_entry_t *)
1299 ((char *)dbp->data +
1300 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1301
1302
1303
1304 args->inumber = be64_to_cpu(dep->inumber);
1305 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1306 xfs_da_brelse(tp, dbp);
1307 xfs_da_brelse(tp, lbp);
1308 return XFS_ERROR(error);
1309}
1310
1311
1312
1313
1314
1315
1316
1317static int
1318xfs_dir2_leaf_lookup_int(
1319 xfs_da_args_t *args,
1320 xfs_dabuf_t **lbpp,
1321 int *indexp,
1322 xfs_dabuf_t **dbpp)
1323{
1324 xfs_dir2_db_t curdb = -1;
1325 xfs_dabuf_t *dbp = NULL;
1326 xfs_dir2_data_entry_t *dep;
1327 xfs_inode_t *dp;
1328 int error;
1329 int index;
1330 xfs_dabuf_t *lbp;
1331 xfs_dir2_leaf_entry_t *lep;
1332 xfs_dir2_leaf_t *leaf;
1333 xfs_mount_t *mp;
1334 xfs_dir2_db_t newdb;
1335 xfs_trans_t *tp;
1336 xfs_dir2_db_t cidb = -1;
1337 enum xfs_dacmp cmp;
1338
1339 dp = args->dp;
1340 tp = args->trans;
1341 mp = dp->i_mount;
1342
1343
1344
1345 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1346 XFS_DATA_FORK);
1347 if (error)
1348 return error;
1349 *lbpp = lbp;
1350 leaf = lbp->data;
1351 xfs_dir2_leaf_check(dp, lbp);
1352
1353
1354
1355 index = xfs_dir2_leaf_search_hash(args, lbp);
1356
1357
1358
1359
1360 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
1361 be32_to_cpu(lep->hashval) == args->hashval;
1362 lep++, index++) {
1363
1364
1365
1366 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1367 continue;
1368
1369
1370
1371 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1372
1373
1374
1375
1376 if (newdb != curdb) {
1377 if (dbp)
1378 xfs_da_brelse(tp, dbp);
1379 error = xfs_da_read_buf(tp, dp,
1380 xfs_dir2_db_to_da(mp, newdb),
1381 -1, &dbp, XFS_DATA_FORK);
1382 if (error) {
1383 xfs_da_brelse(tp, lbp);
1384 return error;
1385 }
1386 xfs_dir2_data_check(dp, dbp);
1387 curdb = newdb;
1388 }
1389
1390
1391
1392 dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
1393 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1394
1395
1396
1397
1398
1399 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1400 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1401 args->cmpresult = cmp;
1402 *indexp = index;
1403
1404 if (cmp == XFS_CMP_EXACT) {
1405 *dbpp = dbp;
1406 return 0;
1407 }
1408 cidb = curdb;
1409 }
1410 }
1411 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1412
1413
1414
1415
1416
1417 if (args->cmpresult == XFS_CMP_CASE) {
1418 ASSERT(cidb != -1);
1419 if (cidb != curdb) {
1420 xfs_da_brelse(tp, dbp);
1421 error = xfs_da_read_buf(tp, dp,
1422 xfs_dir2_db_to_da(mp, cidb),
1423 -1, &dbp, XFS_DATA_FORK);
1424 if (error) {
1425 xfs_da_brelse(tp, lbp);
1426 return error;
1427 }
1428 }
1429 *dbpp = dbp;
1430 return 0;
1431 }
1432
1433
1434
1435 ASSERT(cidb == -1);
1436 if (dbp)
1437 xfs_da_brelse(tp, dbp);
1438 xfs_da_brelse(tp, lbp);
1439 return XFS_ERROR(ENOENT);
1440}
1441
1442
1443
1444
1445int
1446xfs_dir2_leaf_removename(
1447 xfs_da_args_t *args)
1448{
1449 __be16 *bestsp;
1450 xfs_dir2_data_t *data;
1451 xfs_dir2_db_t db;
1452 xfs_dabuf_t *dbp;
1453 xfs_dir2_data_entry_t *dep;
1454 xfs_inode_t *dp;
1455 int error;
1456 xfs_dir2_db_t i;
1457 int index;
1458 xfs_dabuf_t *lbp;
1459 xfs_dir2_leaf_t *leaf;
1460 xfs_dir2_leaf_entry_t *lep;
1461 xfs_dir2_leaf_tail_t *ltp;
1462 xfs_mount_t *mp;
1463 int needlog;
1464 int needscan;
1465 xfs_dir2_data_off_t oldbest;
1466 xfs_trans_t *tp;
1467
1468 xfs_dir2_trace_args("leaf_removename", args);
1469
1470
1471
1472 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1473 return error;
1474 }
1475 dp = args->dp;
1476 tp = args->trans;
1477 mp = dp->i_mount;
1478 leaf = lbp->data;
1479 data = dbp->data;
1480 xfs_dir2_data_check(dp, dbp);
1481
1482
1483
1484 lep = &leaf->ents[index];
1485 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1486 dep = (xfs_dir2_data_entry_t *)
1487 ((char *)data + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1488 needscan = needlog = 0;
1489 oldbest = be16_to_cpu(data->hdr.bestfree[0].length);
1490 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1491 bestsp = xfs_dir2_leaf_bests_p(ltp);
1492 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1493
1494
1495
1496 xfs_dir2_data_make_free(tp, dbp,
1497 (xfs_dir2_data_aoff_t)((char *)dep - (char *)data),
1498 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1499
1500
1501
1502 be16_add_cpu(&leaf->hdr.stale, 1);
1503 xfs_dir2_leaf_log_header(tp, lbp);
1504 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1505 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1506
1507
1508
1509
1510 if (needscan)
1511 xfs_dir2_data_freescan(mp, data, &needlog);
1512 if (needlog)
1513 xfs_dir2_data_log_header(tp, dbp);
1514
1515
1516
1517
1518 if (be16_to_cpu(data->hdr.bestfree[0].length) != oldbest) {
1519 bestsp[db] = data->hdr.bestfree[0].length;
1520 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1521 }
1522 xfs_dir2_data_check(dp, dbp);
1523
1524
1525
1526 if (be16_to_cpu(data->hdr.bestfree[0].length) ==
1527 mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1528 ASSERT(db != mp->m_dirdatablk);
1529 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1530
1531
1532
1533
1534
1535
1536 if (error == ENOSPC && args->total == 0) {
1537 xfs_da_buf_done(dbp);
1538 error = 0;
1539 }
1540 xfs_dir2_leaf_check(dp, lbp);
1541 xfs_da_buf_done(lbp);
1542 return error;
1543 }
1544 dbp = NULL;
1545
1546
1547
1548
1549 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1550
1551
1552
1553 for (i = db - 1; i > 0; i--) {
1554 if (be16_to_cpu(bestsp[i]) != NULLDATAOFF)
1555 break;
1556 }
1557
1558
1559
1560
1561 memmove(&bestsp[db - i], bestsp,
1562 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1563 be32_add_cpu(<p->bestcount, -(db - i));
1564 xfs_dir2_leaf_log_tail(tp, lbp);
1565 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1566 } else
1567 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1568 }
1569
1570
1571
1572 else if (db != mp->m_dirdatablk && dbp != NULL) {
1573 xfs_da_buf_done(dbp);
1574 dbp = NULL;
1575 }
1576 xfs_dir2_leaf_check(dp, lbp);
1577
1578
1579
1580 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1581}
1582
1583
1584
1585
1586int
1587xfs_dir2_leaf_replace(
1588 xfs_da_args_t *args)
1589{
1590 xfs_dabuf_t *dbp;
1591 xfs_dir2_data_entry_t *dep;
1592 xfs_inode_t *dp;
1593 int error;
1594 int index;
1595 xfs_dabuf_t *lbp;
1596 xfs_dir2_leaf_t *leaf;
1597 xfs_dir2_leaf_entry_t *lep;
1598 xfs_trans_t *tp;
1599
1600 xfs_dir2_trace_args("leaf_replace", args);
1601
1602
1603
1604 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1605 return error;
1606 }
1607 dp = args->dp;
1608 leaf = lbp->data;
1609
1610
1611
1612 lep = &leaf->ents[index];
1613
1614
1615
1616 dep = (xfs_dir2_data_entry_t *)
1617 ((char *)dbp->data +
1618 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1619 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1620
1621
1622
1623 dep->inumber = cpu_to_be64(args->inumber);
1624 tp = args->trans;
1625 xfs_dir2_data_log_entry(tp, dbp, dep);
1626 xfs_da_buf_done(dbp);
1627 xfs_dir2_leaf_check(dp, lbp);
1628 xfs_da_brelse(tp, lbp);
1629 return 0;
1630}
1631
1632
1633
1634
1635
1636
1637int
1638xfs_dir2_leaf_search_hash(
1639 xfs_da_args_t *args,
1640 xfs_dabuf_t *lbp)
1641{
1642 xfs_dahash_t hash=0;
1643 xfs_dahash_t hashwant;
1644 int high;
1645 int low;
1646 xfs_dir2_leaf_t *leaf;
1647 xfs_dir2_leaf_entry_t *lep;
1648 int mid=0;
1649
1650 leaf = lbp->data;
1651#ifndef __KERNEL__
1652 if (!leaf->hdr.count)
1653 return 0;
1654#endif
1655
1656
1657
1658
1659 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
1660 hashwant = args->hashval;
1661 low <= high; ) {
1662 mid = (low + high) >> 1;
1663 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1664 break;
1665 if (hash < hashwant)
1666 low = mid + 1;
1667 else
1668 high = mid - 1;
1669 }
1670
1671
1672
1673 if (hash == hashwant) {
1674 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1675 mid--;
1676 }
1677 }
1678
1679
1680
1681 else if (hash < hashwant)
1682 mid++;
1683 return mid;
1684}
1685
1686
1687
1688
1689
1690int
1691xfs_dir2_leaf_trim_data(
1692 xfs_da_args_t *args,
1693 xfs_dabuf_t *lbp,
1694 xfs_dir2_db_t db)
1695{
1696 __be16 *bestsp;
1697#ifdef DEBUG
1698 xfs_dir2_data_t *data;
1699#endif
1700 xfs_dabuf_t *dbp;
1701 xfs_inode_t *dp;
1702 int error;
1703 xfs_dir2_leaf_t *leaf;
1704 xfs_dir2_leaf_tail_t *ltp;
1705 xfs_mount_t *mp;
1706 xfs_trans_t *tp;
1707
1708 dp = args->dp;
1709 mp = dp->i_mount;
1710 tp = args->trans;
1711
1712
1713
1714 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
1715 XFS_DATA_FORK))) {
1716 return error;
1717 }
1718#ifdef DEBUG
1719 data = dbp->data;
1720 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
1721#endif
1722
1723
1724
1725
1726
1727 leaf = lbp->data;
1728 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1729 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) ==
1730 mp->m_dirblksize - (uint)sizeof(data->hdr));
1731 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1732
1733
1734
1735 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1736 ASSERT(error != ENOSPC);
1737 xfs_da_brelse(tp, dbp);
1738 return error;
1739 }
1740
1741
1742
1743 bestsp = xfs_dir2_leaf_bests_p(ltp);
1744 be32_add_cpu(<p->bestcount, -1);
1745 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1746 xfs_dir2_leaf_log_tail(tp, lbp);
1747 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1748 return 0;
1749}
1750
1751
1752
1753
1754
1755
1756int
1757xfs_dir2_node_to_leaf(
1758 xfs_da_state_t *state)
1759{
1760 xfs_da_args_t *args;
1761 xfs_inode_t *dp;
1762 int error;
1763 xfs_dabuf_t *fbp;
1764 xfs_fileoff_t fo;
1765 xfs_dir2_free_t *free;
1766 xfs_dabuf_t *lbp;
1767 xfs_dir2_leaf_tail_t *ltp;
1768 xfs_dir2_leaf_t *leaf;
1769 xfs_mount_t *mp;
1770 int rval;
1771 xfs_trans_t *tp;
1772
1773
1774
1775
1776
1777 if (state->path.active > 1)
1778 return 0;
1779 args = state->args;
1780 xfs_dir2_trace_args("node_to_leaf", args);
1781 mp = state->mp;
1782 dp = args->dp;
1783 tp = args->trans;
1784
1785
1786
1787 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1788 return error;
1789 }
1790 fo -= mp->m_dirblkfsbs;
1791
1792
1793
1794
1795
1796
1797 while (fo > mp->m_dirfreeblk) {
1798 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1799 return error;
1800 }
1801 if (rval)
1802 fo -= mp->m_dirblkfsbs;
1803 else
1804 return 0;
1805 }
1806
1807
1808
1809 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1810 return error;
1811 }
1812
1813
1814
1815 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1816 return 0;
1817 lbp = state->path.blk[0].bp;
1818 leaf = lbp->data;
1819 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1820
1821
1822
1823 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1824 XFS_DATA_FORK))) {
1825 return error;
1826 }
1827 free = fbp->data;
1828 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1829 ASSERT(!free->hdr.firstdb);
1830
1831
1832
1833
1834 if ((uint)sizeof(leaf->hdr) +
1835 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)) * (uint)sizeof(leaf->ents[0]) +
1836 be32_to_cpu(free->hdr.nvalid) * (uint)sizeof(leaf->bests[0]) +
1837 (uint)sizeof(leaf->tail) >
1838 mp->m_dirblksize) {
1839 xfs_da_brelse(tp, fbp);
1840 return 0;
1841 }
1842
1843
1844
1845
1846 if (be16_to_cpu(leaf->hdr.stale))
1847 xfs_dir2_leaf_compact(args, lbp);
1848 else
1849 xfs_dir2_leaf_log_header(tp, lbp);
1850 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
1851
1852
1853
1854 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1855 ltp->bestcount = free->hdr.nvalid;
1856
1857
1858
1859 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
1860 be32_to_cpu(ltp->bestcount) * sizeof(leaf->bests[0]));
1861 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1862 xfs_dir2_leaf_log_tail(tp, lbp);
1863 xfs_dir2_leaf_check(dp, lbp);
1864
1865
1866
1867 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1868 if (error) {
1869
1870
1871
1872
1873
1874 ASSERT(error != ENOSPC);
1875 return error;
1876 }
1877 fbp = NULL;
1878
1879
1880
1881
1882
1883
1884 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1885 state->path.blk[0].bp = NULL;
1886 return error;
1887}