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