1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_log.h"
22#include "xfs_inum.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
26#include "xfs_dir2.h"
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
29#include "xfs_da_btree.h"
30#include "xfs_bmap_btree.h"
31#include "xfs_dir2_sf.h"
32#include "xfs_attr_sf.h"
33#include "xfs_dinode.h"
34#include "xfs_inode.h"
35#include "xfs_inode_item.h"
36#include "xfs_dir2_data.h"
37#include "xfs_dir2_leaf.h"
38#include "xfs_dir2_block.h"
39#include "xfs_dir2_trace.h"
40#include "xfs_error.h"
41
42
43
44
45static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first,
46 int last);
47static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp);
48static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **bpp,
49 int *entno);
50static int xfs_dir2_block_sort(const void *a, const void *b);
51
52static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
53
54
55
56
57void
58xfs_dir_startup(void)
59{
60 xfs_dir_hash_dot = xfs_da_hashname(".", 1);
61 xfs_dir_hash_dotdot = xfs_da_hashname("..", 2);
62}
63
64
65
66
67int
68xfs_dir2_block_addname(
69 xfs_da_args_t *args)
70{
71 xfs_dir2_data_free_t *bf;
72 xfs_dir2_block_t *block;
73 xfs_dir2_leaf_entry_t *blp;
74 xfs_dabuf_t *bp;
75 xfs_dir2_block_tail_t *btp;
76 int compact;
77 xfs_dir2_data_entry_t *dep;
78 xfs_inode_t *dp;
79 xfs_dir2_data_unused_t *dup;
80 int error;
81 xfs_dir2_data_unused_t *enddup=NULL;
82 xfs_dahash_t hash;
83 int high;
84 int highstale;
85 int lfloghigh=0;
86 int lfloglow=0;
87 int len;
88 int low;
89 int lowstale;
90 int mid=0;
91 xfs_mount_t *mp;
92 int needlog;
93 int needscan;
94 __be16 *tagp;
95 xfs_trans_t *tp;
96
97 xfs_dir2_trace_args("block_addname", args);
98 dp = args->dp;
99 tp = args->trans;
100 mp = dp->i_mount;
101
102
103
104 if ((error =
105 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
106 return error;
107 }
108 ASSERT(bp != NULL);
109 block = bp->data;
110
111
112
113 if (unlikely(be32_to_cpu(block->hdr.magic) != XFS_DIR2_BLOCK_MAGIC)) {
114 XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
115 XFS_ERRLEVEL_LOW, mp, block);
116 xfs_da_brelse(tp, bp);
117 return XFS_ERROR(EFSCORRUPTED);
118 }
119 len = xfs_dir2_data_entsize(args->namelen);
120
121
122
123 bf = block->hdr.bestfree;
124 btp = xfs_dir2_block_tail_p(mp, block);
125 blp = xfs_dir2_block_leaf_p(btp);
126
127
128
129 if (!btp->stale) {
130
131
132
133 tagp = (__be16 *)blp - 1;
134
135
136
137 enddup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
138
139
140
141
142
143 if (be16_to_cpu(enddup->freetag) != XFS_DIR2_DATA_FREE_TAG)
144 dup = enddup = NULL;
145
146
147
148 else {
149 dup = (xfs_dir2_data_unused_t *)
150 ((char *)block + be16_to_cpu(bf[0].offset));
151 if (dup == enddup) {
152
153
154
155
156 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
157
158
159
160
161 if (be16_to_cpu(bf[1].length) >= len)
162 dup = (xfs_dir2_data_unused_t *)
163 ((char *)block +
164 be16_to_cpu(bf[1].offset));
165 else
166 dup = NULL;
167 }
168 } else {
169
170
171
172
173 if (be16_to_cpu(dup->length) < len) {
174 dup = NULL;
175 }
176 }
177 }
178 compact = 0;
179 }
180
181
182
183
184 else if (be16_to_cpu(bf[0].length) >= len) {
185 dup = (xfs_dir2_data_unused_t *)
186 ((char *)block + be16_to_cpu(bf[0].offset));
187 compact = 0;
188 }
189
190
191
192 else {
193
194
195
196 tagp = (__be16 *)blp - 1;
197
198
199
200 dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
201
202
203
204
205 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
206 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
207 (uint)sizeof(*blp) < len)
208 dup = NULL;
209 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
210 dup = NULL;
211 else
212 dup = (xfs_dir2_data_unused_t *)blp;
213 compact = 1;
214 }
215
216
217
218 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
219 xfs_da_brelse(tp, bp);
220
221
222
223 if (!dup) {
224
225
226
227
228 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
229 return XFS_ERROR(ENOSPC);
230
231
232
233
234 error = xfs_dir2_block_to_leaf(args, bp);
235 xfs_da_buf_done(bp);
236 if (error)
237 return error;
238 return xfs_dir2_leaf_addname(args);
239 }
240
241
242
243 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
244 return 0;
245 needlog = needscan = 0;
246
247
248
249
250
251 if (compact) {
252 int fromidx;
253 int toidx;
254
255 for (fromidx = toidx = be32_to_cpu(btp->count) - 1,
256 highstale = lfloghigh = -1;
257 fromidx >= 0;
258 fromidx--) {
259 if (be32_to_cpu(blp[fromidx].address) == XFS_DIR2_NULL_DATAPTR) {
260 if (highstale == -1)
261 highstale = toidx;
262 else {
263 if (lfloghigh == -1)
264 lfloghigh = toidx;
265 continue;
266 }
267 }
268 if (fromidx < toidx)
269 blp[toidx] = blp[fromidx];
270 toidx--;
271 }
272 lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
273 lfloghigh -= be32_to_cpu(btp->stale) - 1;
274 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
275 xfs_dir2_data_make_free(tp, bp,
276 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
277 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
278 &needlog, &needscan);
279 blp += be32_to_cpu(btp->stale) - 1;
280 btp->stale = cpu_to_be32(1);
281
282
283
284
285 if (needscan) {
286 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
287 needscan = 0;
288 }
289 }
290
291
292
293
294 else if (btp->stale) {
295 lfloglow = be32_to_cpu(btp->count);
296 lfloghigh = -1;
297 }
298
299
300
301 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
302 mid = (low + high) >> 1;
303 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
304 break;
305 if (hash < args->hashval)
306 low = mid + 1;
307 else
308 high = mid - 1;
309 }
310 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
311 mid--;
312 }
313
314
315
316 if (!btp->stale) {
317
318
319
320 xfs_dir2_data_use_free(tp, bp, enddup,
321 (xfs_dir2_data_aoff_t)
322 ((char *)enddup - (char *)block + be16_to_cpu(enddup->length) -
323 sizeof(*blp)),
324 (xfs_dir2_data_aoff_t)sizeof(*blp),
325 &needlog, &needscan);
326
327
328
329 be32_add_cpu(&btp->count, 1);
330
331
332
333
334 if (needscan) {
335 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
336 &needlog);
337 needscan = 0;
338 }
339
340
341
342
343
344 blp--;
345 mid++;
346 if (mid)
347 memmove(blp, &blp[1], mid * sizeof(*blp));
348 lfloglow = 0;
349 lfloghigh = mid;
350 }
351
352
353
354 else {
355 for (lowstale = mid;
356 lowstale >= 0 &&
357 be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
358 lowstale--)
359 continue;
360 for (highstale = mid + 1;
361 highstale < be32_to_cpu(btp->count) &&
362 be32_to_cpu(blp[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
363 (lowstale < 0 || mid - lowstale > highstale - mid);
364 highstale++)
365 continue;
366
367
368
369 if (lowstale >= 0 &&
370 (highstale == be32_to_cpu(btp->count) ||
371 mid - lowstale <= highstale - mid)) {
372 if (mid - lowstale)
373 memmove(&blp[lowstale], &blp[lowstale + 1],
374 (mid - lowstale) * sizeof(*blp));
375 lfloglow = MIN(lowstale, lfloglow);
376 lfloghigh = MAX(mid, lfloghigh);
377 }
378
379
380
381 else {
382 ASSERT(highstale < be32_to_cpu(btp->count));
383 mid++;
384 if (highstale - mid)
385 memmove(&blp[mid + 1], &blp[mid],
386 (highstale - mid) * sizeof(*blp));
387 lfloglow = MIN(mid, lfloglow);
388 lfloghigh = MAX(highstale, lfloghigh);
389 }
390 be32_add_cpu(&btp->stale, -1);
391 }
392
393
394
395 dep = (xfs_dir2_data_entry_t *)dup;
396
397
398
399 blp[mid].hashval = cpu_to_be32(args->hashval);
400 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
401 (char *)dep - (char *)block));
402 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
403
404
405
406 xfs_dir2_data_use_free(tp, bp, dup,
407 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
408 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
409
410
411
412 dep->inumber = cpu_to_be64(args->inumber);
413 dep->namelen = args->namelen;
414 memcpy(dep->name, args->name, args->namelen);
415 tagp = xfs_dir2_data_entry_tag_p(dep);
416 *tagp = cpu_to_be16((char *)dep - (char *)block);
417
418
419
420 if (needscan)
421 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
422 if (needlog)
423 xfs_dir2_data_log_header(tp, bp);
424 xfs_dir2_block_log_tail(tp, bp);
425 xfs_dir2_data_log_entry(tp, bp, dep);
426 xfs_dir2_data_check(dp, bp);
427 xfs_da_buf_done(bp);
428 return 0;
429}
430
431
432
433
434int
435xfs_dir2_block_getdents(
436 xfs_inode_t *dp,
437 void *dirent,
438 xfs_off_t *offset,
439 filldir_t filldir)
440{
441 xfs_dir2_block_t *block;
442 xfs_dabuf_t *bp;
443 xfs_dir2_block_tail_t *btp;
444 xfs_dir2_data_entry_t *dep;
445 xfs_dir2_data_unused_t *dup;
446 char *endptr;
447 int error;
448 xfs_mount_t *mp;
449 char *ptr;
450 int wantoff;
451 xfs_off_t cook;
452
453 mp = dp->i_mount;
454
455
456
457 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) {
458 return 0;
459 }
460
461
462
463 error = xfs_da_read_buf(NULL, dp, mp->m_dirdatablk, -1,
464 &bp, XFS_DATA_FORK);
465 if (error)
466 return error;
467
468 ASSERT(bp != NULL);
469
470
471
472
473 wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
474 block = bp->data;
475 xfs_dir2_data_check(dp, bp);
476
477
478
479 btp = xfs_dir2_block_tail_p(mp, block);
480 ptr = (char *)block->u;
481 endptr = (char *)xfs_dir2_block_leaf_p(btp);
482
483
484
485
486
487 while (ptr < endptr) {
488 dup = (xfs_dir2_data_unused_t *)ptr;
489
490
491
492 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
493 ptr += be16_to_cpu(dup->length);
494 continue;
495 }
496
497 dep = (xfs_dir2_data_entry_t *)ptr;
498
499
500
501
502 ptr += xfs_dir2_data_entsize(dep->namelen);
503
504
505
506 if ((char *)dep - (char *)block < wantoff)
507 continue;
508
509 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
510 (char *)dep - (char *)block);
511
512
513
514
515 if (filldir(dirent, dep->name, dep->namelen, cook & 0x7fffffff,
516 be64_to_cpu(dep->inumber), DT_UNKNOWN)) {
517 *offset = cook & 0x7fffffff;
518 xfs_da_brelse(NULL, bp);
519 return 0;
520 }
521 }
522
523
524
525
526
527 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
528 0x7fffffff;
529 xfs_da_brelse(NULL, bp);
530 return 0;
531}
532
533
534
535
536static void
537xfs_dir2_block_log_leaf(
538 xfs_trans_t *tp,
539 xfs_dabuf_t *bp,
540 int first,
541 int last)
542{
543 xfs_dir2_block_t *block;
544 xfs_dir2_leaf_entry_t *blp;
545 xfs_dir2_block_tail_t *btp;
546 xfs_mount_t *mp;
547
548 mp = tp->t_mountp;
549 block = bp->data;
550 btp = xfs_dir2_block_tail_p(mp, block);
551 blp = xfs_dir2_block_leaf_p(btp);
552 xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)block),
553 (uint)((char *)&blp[last + 1] - (char *)block - 1));
554}
555
556
557
558
559static void
560xfs_dir2_block_log_tail(
561 xfs_trans_t *tp,
562 xfs_dabuf_t *bp)
563{
564 xfs_dir2_block_t *block;
565 xfs_dir2_block_tail_t *btp;
566 xfs_mount_t *mp;
567
568 mp = tp->t_mountp;
569 block = bp->data;
570 btp = xfs_dir2_block_tail_p(mp, block);
571 xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)block),
572 (uint)((char *)(btp + 1) - (char *)block - 1));
573}
574
575
576
577
578
579int
580xfs_dir2_block_lookup(
581 xfs_da_args_t *args)
582{
583 xfs_dir2_block_t *block;
584 xfs_dir2_leaf_entry_t *blp;
585 xfs_dabuf_t *bp;
586 xfs_dir2_block_tail_t *btp;
587 xfs_dir2_data_entry_t *dep;
588 xfs_inode_t *dp;
589 int ent;
590 int error;
591 xfs_mount_t *mp;
592
593 xfs_dir2_trace_args("block_lookup", args);
594
595
596
597
598 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
599 return error;
600 dp = args->dp;
601 mp = dp->i_mount;
602 block = bp->data;
603 xfs_dir2_data_check(dp, bp);
604 btp = xfs_dir2_block_tail_p(mp, block);
605 blp = xfs_dir2_block_leaf_p(btp);
606
607
608
609 dep = (xfs_dir2_data_entry_t *)((char *)block +
610 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
611
612
613
614 args->inumber = be64_to_cpu(dep->inumber);
615 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
616 xfs_da_brelse(args->trans, bp);
617 return XFS_ERROR(error);
618}
619
620
621
622
623static int
624xfs_dir2_block_lookup_int(
625 xfs_da_args_t *args,
626 xfs_dabuf_t **bpp,
627 int *entno)
628{
629 xfs_dir2_dataptr_t addr;
630 xfs_dir2_block_t *block;
631 xfs_dir2_leaf_entry_t *blp;
632 xfs_dabuf_t *bp;
633 xfs_dir2_block_tail_t *btp;
634 xfs_dir2_data_entry_t *dep;
635 xfs_inode_t *dp;
636 int error;
637 xfs_dahash_t hash;
638 int high;
639 int low;
640 int mid;
641 xfs_mount_t *mp;
642 xfs_trans_t *tp;
643 enum xfs_dacmp cmp;
644
645 dp = args->dp;
646 tp = args->trans;
647 mp = dp->i_mount;
648
649
650
651 if ((error =
652 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
653 return error;
654 }
655 ASSERT(bp != NULL);
656 block = bp->data;
657 xfs_dir2_data_check(dp, bp);
658 btp = xfs_dir2_block_tail_p(mp, block);
659 blp = xfs_dir2_block_leaf_p(btp);
660
661
662
663
664 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
665 ASSERT(low <= high);
666 mid = (low + high) >> 1;
667 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
668 break;
669 if (hash < args->hashval)
670 low = mid + 1;
671 else
672 high = mid - 1;
673 if (low > high) {
674 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
675 xfs_da_brelse(tp, bp);
676 return XFS_ERROR(ENOENT);
677 }
678 }
679
680
681
682 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
683 mid--;
684 }
685
686
687
688
689 do {
690 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
691 continue;
692
693
694
695 dep = (xfs_dir2_data_entry_t *)
696 ((char *)block + xfs_dir2_dataptr_to_off(mp, addr));
697
698
699
700
701
702 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
703 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
704 args->cmpresult = cmp;
705 *bpp = bp;
706 *entno = mid;
707 if (cmp == XFS_CMP_EXACT)
708 return 0;
709 }
710 } while (++mid < be32_to_cpu(btp->count) &&
711 be32_to_cpu(blp[mid].hashval) == hash);
712
713 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
714
715
716
717
718 if (args->cmpresult == XFS_CMP_CASE)
719 return 0;
720
721
722
723 xfs_da_brelse(tp, bp);
724 return XFS_ERROR(ENOENT);
725}
726
727
728
729
730
731int
732xfs_dir2_block_removename(
733 xfs_da_args_t *args)
734{
735 xfs_dir2_block_t *block;
736 xfs_dir2_leaf_entry_t *blp;
737 xfs_dabuf_t *bp;
738 xfs_dir2_block_tail_t *btp;
739 xfs_dir2_data_entry_t *dep;
740 xfs_inode_t *dp;
741 int ent;
742 int error;
743 xfs_mount_t *mp;
744 int needlog;
745 int needscan;
746 xfs_dir2_sf_hdr_t sfh;
747 int size;
748 xfs_trans_t *tp;
749
750 xfs_dir2_trace_args("block_removename", args);
751
752
753
754
755 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
756 return error;
757 }
758 dp = args->dp;
759 tp = args->trans;
760 mp = dp->i_mount;
761 block = bp->data;
762 btp = xfs_dir2_block_tail_p(mp, block);
763 blp = xfs_dir2_block_leaf_p(btp);
764
765
766
767 dep = (xfs_dir2_data_entry_t *)
768 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
769
770
771
772 needlog = needscan = 0;
773 xfs_dir2_data_make_free(tp, bp,
774 (xfs_dir2_data_aoff_t)((char *)dep - (char *)block),
775 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
776
777
778
779 be32_add_cpu(&btp->stale, 1);
780 xfs_dir2_block_log_tail(tp, bp);
781
782
783
784 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
785 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
786
787
788
789 if (needscan)
790 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
791 if (needlog)
792 xfs_dir2_data_log_header(tp, bp);
793 xfs_dir2_data_check(dp, bp);
794
795
796
797 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
798 XFS_IFORK_DSIZE(dp)) {
799 xfs_da_buf_done(bp);
800 return 0;
801 }
802
803
804
805 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
806}
807
808
809
810
811
812int
813xfs_dir2_block_replace(
814 xfs_da_args_t *args)
815{
816 xfs_dir2_block_t *block;
817 xfs_dir2_leaf_entry_t *blp;
818 xfs_dabuf_t *bp;
819 xfs_dir2_block_tail_t *btp;
820 xfs_dir2_data_entry_t *dep;
821 xfs_inode_t *dp;
822 int ent;
823 int error;
824 xfs_mount_t *mp;
825
826 xfs_dir2_trace_args("block_replace", args);
827
828
829
830
831 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
832 return error;
833 }
834 dp = args->dp;
835 mp = dp->i_mount;
836 block = bp->data;
837 btp = xfs_dir2_block_tail_p(mp, block);
838 blp = xfs_dir2_block_leaf_p(btp);
839
840
841
842 dep = (xfs_dir2_data_entry_t *)
843 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
844 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
845
846
847
848 dep->inumber = cpu_to_be64(args->inumber);
849 xfs_dir2_data_log_entry(args->trans, bp, dep);
850 xfs_dir2_data_check(dp, bp);
851 xfs_da_buf_done(bp);
852 return 0;
853}
854
855
856
857
858static int
859xfs_dir2_block_sort(
860 const void *a,
861 const void *b)
862{
863 const xfs_dir2_leaf_entry_t *la;
864 const xfs_dir2_leaf_entry_t *lb;
865
866 la = a;
867 lb = b;
868 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
869 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
870}
871
872
873
874
875int
876xfs_dir2_leaf_to_block(
877 xfs_da_args_t *args,
878 xfs_dabuf_t *lbp,
879 xfs_dabuf_t *dbp)
880{
881 __be16 *bestsp;
882 xfs_dir2_block_t *block;
883 xfs_dir2_block_tail_t *btp;
884 xfs_inode_t *dp;
885 xfs_dir2_data_unused_t *dup;
886 int error;
887 int from;
888 xfs_dir2_leaf_t *leaf;
889 xfs_dir2_leaf_entry_t *lep;
890 xfs_dir2_leaf_tail_t *ltp;
891 xfs_mount_t *mp;
892 int needlog;
893 int needscan;
894 xfs_dir2_sf_hdr_t sfh;
895 int size;
896 __be16 *tagp;
897 int to;
898 xfs_trans_t *tp;
899
900 xfs_dir2_trace_args_bb("leaf_to_block", args, lbp, dbp);
901 dp = args->dp;
902 tp = args->trans;
903 mp = dp->i_mount;
904 leaf = lbp->data;
905 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
906 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
907
908
909
910
911
912
913 while (dp->i_d.di_size > mp->m_dirblksize) {
914 bestsp = xfs_dir2_leaf_bests_p(ltp);
915 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
916 mp->m_dirblksize - (uint)sizeof(block->hdr)) {
917 if ((error =
918 xfs_dir2_leaf_trim_data(args, lbp,
919 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
920 goto out;
921 } else {
922 error = 0;
923 goto out;
924 }
925 }
926
927
928
929 if (dbp == NULL &&
930 (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
931 XFS_DATA_FORK))) {
932 goto out;
933 }
934 block = dbp->data;
935 ASSERT(be32_to_cpu(block->hdr.magic) == XFS_DIR2_DATA_MAGIC);
936
937
938
939 size = (uint)sizeof(block->tail) +
940 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
941
942
943
944 tagp = (__be16 *)((char *)block + mp->m_dirblksize) - 1;
945 dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
946
947
948
949 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
950 be16_to_cpu(dup->length) < size) {
951 error = 0;
952 goto out;
953 }
954
955
956
957 block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
958 needlog = 1;
959 needscan = 0;
960
961
962
963 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
964 &needlog, &needscan);
965
966
967
968 btp = xfs_dir2_block_tail_p(mp, block);
969 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
970 btp->stale = 0;
971 xfs_dir2_block_log_tail(tp, dbp);
972
973
974
975 lep = xfs_dir2_block_leaf_p(btp);
976 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
977 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
978 continue;
979 lep[to++] = leaf->ents[from];
980 }
981 ASSERT(to == be32_to_cpu(btp->count));
982 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
983
984
985
986 if (needscan)
987 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
988 if (needlog)
989 xfs_dir2_data_log_header(tp, dbp);
990
991
992
993 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
994 lbp = NULL;
995 if (error) {
996 goto out;
997 }
998
999
1000
1001 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
1002 XFS_IFORK_DSIZE(dp)) {
1003 error = 0;
1004 goto out;
1005 }
1006 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1007out:
1008 if (lbp)
1009 xfs_da_buf_done(lbp);
1010 if (dbp)
1011 xfs_da_buf_done(dbp);
1012 return error;
1013}
1014
1015
1016
1017
1018int
1019xfs_dir2_sf_to_block(
1020 xfs_da_args_t *args)
1021{
1022 xfs_dir2_db_t blkno;
1023 xfs_dir2_block_t *block;
1024 xfs_dir2_leaf_entry_t *blp;
1025 xfs_dabuf_t *bp;
1026 xfs_dir2_block_tail_t *btp;
1027 char *buf;
1028 int buf_len;
1029 xfs_dir2_data_entry_t *dep;
1030 xfs_inode_t *dp;
1031 int dummy;
1032 xfs_dir2_data_unused_t *dup;
1033 int endoffset;
1034 int error;
1035 int i;
1036 xfs_mount_t *mp;
1037 int needlog;
1038 int needscan;
1039 int newoffset;
1040 int offset;
1041 xfs_dir2_sf_entry_t *sfep;
1042 xfs_dir2_sf_t *sfp;
1043 __be16 *tagp;
1044 xfs_trans_t *tp;
1045 struct xfs_name name;
1046
1047 xfs_dir2_trace_args("sf_to_block", args);
1048 dp = args->dp;
1049 tp = args->trans;
1050 mp = dp->i_mount;
1051 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1052
1053
1054
1055 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1056 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1057 return XFS_ERROR(EIO);
1058 }
1059 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1060 ASSERT(dp->i_df.if_u1.if_data != NULL);
1061 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1062 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
1063
1064
1065
1066
1067
1068 buf_len = dp->i_df.if_bytes;
1069 buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1070
1071 memcpy(buf, sfp, dp->i_df.if_bytes);
1072 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1073 dp->i_d.di_size = 0;
1074 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1075
1076
1077
1078 sfp = (xfs_dir2_sf_t *)buf;
1079
1080
1081
1082 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1083 if (error) {
1084 kmem_free(buf);
1085 return error;
1086 }
1087
1088
1089
1090 error = xfs_dir2_data_init(args, blkno, &bp);
1091 if (error) {
1092 kmem_free(buf);
1093 return error;
1094 }
1095 block = bp->data;
1096 block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
1097
1098
1099
1100 i = (uint)sizeof(*btp) +
1101 (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
1102
1103
1104
1105
1106 dup = (xfs_dir2_data_unused_t *)block->u;
1107 needlog = needscan = 0;
1108 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1109 &needscan);
1110 ASSERT(needscan == 0);
1111
1112
1113
1114 btp = xfs_dir2_block_tail_p(mp, block);
1115 btp->count = cpu_to_be32(sfp->hdr.count + 2);
1116 btp->stale = 0;
1117 blp = xfs_dir2_block_leaf_p(btp);
1118 endoffset = (uint)((char *)blp - (char *)block);
1119
1120
1121
1122 xfs_dir2_data_use_free(tp, bp, dup,
1123 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
1124 be16_to_cpu(dup->length), &needlog, &needscan);
1125
1126
1127
1128 dep = (xfs_dir2_data_entry_t *)
1129 ((char *)block + XFS_DIR2_DATA_DOT_OFFSET);
1130 dep->inumber = cpu_to_be64(dp->i_ino);
1131 dep->namelen = 1;
1132 dep->name[0] = '.';
1133 tagp = xfs_dir2_data_entry_tag_p(dep);
1134 *tagp = cpu_to_be16((char *)dep - (char *)block);
1135 xfs_dir2_data_log_entry(tp, bp, dep);
1136 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1137 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1138 (char *)dep - (char *)block));
1139
1140
1141
1142 dep = (xfs_dir2_data_entry_t *)
1143 ((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET);
1144 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent));
1145 dep->namelen = 2;
1146 dep->name[0] = dep->name[1] = '.';
1147 tagp = xfs_dir2_data_entry_tag_p(dep);
1148 *tagp = cpu_to_be16((char *)dep - (char *)block);
1149 xfs_dir2_data_log_entry(tp, bp, dep);
1150 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1151 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1152 (char *)dep - (char *)block));
1153 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1154
1155
1156
1157 if ((i = 0) == sfp->hdr.count)
1158 sfep = NULL;
1159 else
1160 sfep = xfs_dir2_sf_firstentry(sfp);
1161
1162
1163
1164
1165 while (offset < endoffset) {
1166
1167
1168
1169 if (sfep == NULL)
1170 newoffset = endoffset;
1171 else
1172 newoffset = xfs_dir2_sf_get_offset(sfep);
1173
1174
1175
1176 if (offset < newoffset) {
1177 dup = (xfs_dir2_data_unused_t *)
1178 ((char *)block + offset);
1179 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1180 dup->length = cpu_to_be16(newoffset - offset);
1181 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
1182 ((char *)dup - (char *)block));
1183 xfs_dir2_data_log_unused(tp, bp, dup);
1184 (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block,
1185 dup, &dummy);
1186 offset += be16_to_cpu(dup->length);
1187 continue;
1188 }
1189
1190
1191
1192 dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset);
1193 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp,
1194 xfs_dir2_sf_inumberp(sfep)));
1195 dep->namelen = sfep->namelen;
1196 memcpy(dep->name, sfep->name, dep->namelen);
1197 tagp = xfs_dir2_data_entry_tag_p(dep);
1198 *tagp = cpu_to_be16((char *)dep - (char *)block);
1199 xfs_dir2_data_log_entry(tp, bp, dep);
1200 name.name = sfep->name;
1201 name.len = sfep->namelen;
1202 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1203 hashname(&name));
1204 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1205 (char *)dep - (char *)block));
1206 offset = (int)((char *)(tagp + 1) - (char *)block);
1207 if (++i == sfp->hdr.count)
1208 sfep = NULL;
1209 else
1210 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1211 }
1212
1213 kmem_free(buf);
1214
1215
1216
1217 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
1218
1219
1220
1221
1222 ASSERT(needscan == 0);
1223 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1224 xfs_dir2_block_log_tail(tp, bp);
1225 xfs_dir2_data_check(dp, bp);
1226 xfs_da_buf_done(bp);
1227 return 0;
1228}
1229