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