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