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