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