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