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