1
2
3
4
5
6
7#include "xfs.h"
8#include "xfs_fs.h"
9#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
12#include "xfs_mount.h"
13#include "xfs_da_format.h"
14#include "xfs_da_btree.h"
15#include "xfs_inode.h"
16#include "xfs_dir2.h"
17#include "xfs_dir2_priv.h"
18#include "xfs_error.h"
19#include "xfs_trans.h"
20#include "xfs_buf_item.h"
21#include "xfs_cksum.h"
22#include "xfs_log.h"
23
24static xfs_failaddr_t xfs_dir2_data_freefind_verify(
25 struct xfs_dir2_data_hdr *hdr, struct xfs_dir2_data_free *bf,
26 struct xfs_dir2_data_unused *dup,
27 struct xfs_dir2_data_free **bf_ent);
28
29
30
31
32
33
34xfs_failaddr_t
35__xfs_dir3_data_check(
36 struct xfs_inode *dp,
37 struct xfs_buf *bp)
38{
39 xfs_dir2_dataptr_t addr;
40 xfs_dir2_data_free_t *bf;
41 xfs_dir2_block_tail_t *btp=NULL;
42 int count;
43 xfs_dir2_data_hdr_t *hdr;
44 xfs_dir2_data_entry_t *dep;
45 xfs_dir2_data_free_t *dfp;
46 xfs_dir2_data_unused_t *dup;
47 char *endp;
48 int freeseen;
49 xfs_dahash_t hash;
50 int i;
51 int lastfree;
52 xfs_dir2_leaf_entry_t *lep=NULL;
53 xfs_mount_t *mp;
54 char *p;
55 int stale;
56 struct xfs_name name;
57 const struct xfs_dir_ops *ops;
58 struct xfs_da_geometry *geo;
59
60 mp = bp->b_target->bt_mount;
61 geo = mp->m_dir_geo;
62
63
64
65
66
67 ops = xfs_dir_get_ops(mp, dp);
68
69
70
71
72
73 if ((dp && !S_ISDIR(VFS_I(dp)->i_mode)) ||
74 ops != xfs_dir_get_ops(mp, NULL))
75 return __this_address;
76
77 hdr = bp->b_addr;
78 p = (char *)ops->data_entry_p(hdr);
79
80 switch (hdr->magic) {
81 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
82 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
83 btp = xfs_dir2_block_tail_p(geo, hdr);
84 lep = xfs_dir2_block_leaf_p(btp);
85
86
87
88
89
90
91
92
93 if (be32_to_cpu(btp->count) >=
94 ((char *)btp - p) / sizeof(struct xfs_dir2_leaf_entry))
95 return __this_address;
96 break;
97 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
98 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
99 break;
100 default:
101 return __this_address;
102 }
103 endp = xfs_dir3_data_endp(geo, hdr);
104 if (!endp)
105 return __this_address;
106
107
108
109
110 bf = ops->data_bestfree_p(hdr);
111 count = lastfree = freeseen = 0;
112 if (!bf[0].length) {
113 if (bf[0].offset)
114 return __this_address;
115 freeseen |= 1 << 0;
116 }
117 if (!bf[1].length) {
118 if (bf[1].offset)
119 return __this_address;
120 freeseen |= 1 << 1;
121 }
122 if (!bf[2].length) {
123 if (bf[2].offset)
124 return __this_address;
125 freeseen |= 1 << 2;
126 }
127
128 if (be16_to_cpu(bf[0].length) < be16_to_cpu(bf[1].length))
129 return __this_address;
130 if (be16_to_cpu(bf[1].length) < be16_to_cpu(bf[2].length))
131 return __this_address;
132
133
134
135 while (p < endp) {
136 dup = (xfs_dir2_data_unused_t *)p;
137
138
139
140
141
142 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
143 xfs_failaddr_t fa;
144
145 if (lastfree != 0)
146 return __this_address;
147 if (endp < p + be16_to_cpu(dup->length))
148 return __this_address;
149 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
150 (char *)dup - (char *)hdr)
151 return __this_address;
152 fa = xfs_dir2_data_freefind_verify(hdr, bf, dup, &dfp);
153 if (fa)
154 return fa;
155 if (dfp) {
156 i = (int)(dfp - bf);
157 if ((freeseen & (1 << i)) != 0)
158 return __this_address;
159 freeseen |= 1 << i;
160 } else {
161 if (be16_to_cpu(dup->length) >
162 be16_to_cpu(bf[2].length))
163 return __this_address;
164 }
165 p += be16_to_cpu(dup->length);
166 lastfree = 1;
167 continue;
168 }
169
170
171
172
173
174
175 dep = (xfs_dir2_data_entry_t *)p;
176 if (dep->namelen == 0)
177 return __this_address;
178 if (xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)))
179 return __this_address;
180 if (endp < p + ops->data_entsize(dep->namelen))
181 return __this_address;
182 if (be16_to_cpu(*ops->data_entry_tag_p(dep)) !=
183 (char *)dep - (char *)hdr)
184 return __this_address;
185 if (ops->data_get_ftype(dep) >= XFS_DIR3_FT_MAX)
186 return __this_address;
187 count++;
188 lastfree = 0;
189 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
190 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
191 addr = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
192 (xfs_dir2_data_aoff_t)
193 ((char *)dep - (char *)hdr));
194 name.name = dep->name;
195 name.len = dep->namelen;
196 hash = mp->m_dirnameops->hashname(&name);
197 for (i = 0; i < be32_to_cpu(btp->count); i++) {
198 if (be32_to_cpu(lep[i].address) == addr &&
199 be32_to_cpu(lep[i].hashval) == hash)
200 break;
201 }
202 if (i >= be32_to_cpu(btp->count))
203 return __this_address;
204 }
205 p += ops->data_entsize(dep->namelen);
206 }
207
208
209
210 if (freeseen != 7)
211 return __this_address;
212 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
213 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
214 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
215 if (lep[i].address ==
216 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
217 stale++;
218 if (i > 0 && be32_to_cpu(lep[i].hashval) <
219 be32_to_cpu(lep[i - 1].hashval))
220 return __this_address;
221 }
222 if (count != be32_to_cpu(btp->count) - be32_to_cpu(btp->stale))
223 return __this_address;
224 if (stale != be32_to_cpu(btp->stale))
225 return __this_address;
226 }
227 return NULL;
228}
229
230#ifdef DEBUG
231void
232xfs_dir3_data_check(
233 struct xfs_inode *dp,
234 struct xfs_buf *bp)
235{
236 xfs_failaddr_t fa;
237
238 fa = __xfs_dir3_data_check(dp, bp);
239 if (!fa)
240 return;
241 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
242 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
243 fa);
244 ASSERT(0);
245}
246#endif
247
248static xfs_failaddr_t
249xfs_dir3_data_verify(
250 struct xfs_buf *bp)
251{
252 struct xfs_mount *mp = bp->b_target->bt_mount;
253 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
254
255 if (!xfs_verify_magic(bp, hdr3->magic))
256 return __this_address;
257
258 if (xfs_sb_version_hascrc(&mp->m_sb)) {
259 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
260 return __this_address;
261 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
262 return __this_address;
263 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
264 return __this_address;
265 }
266 return __xfs_dir3_data_check(NULL, bp);
267}
268
269
270
271
272
273
274static void
275xfs_dir3_data_reada_verify(
276 struct xfs_buf *bp)
277{
278 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
279
280 switch (hdr->magic) {
281 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
282 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
283 bp->b_ops = &xfs_dir3_block_buf_ops;
284 bp->b_ops->verify_read(bp);
285 return;
286 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
287 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
288 bp->b_ops = &xfs_dir3_data_buf_ops;
289 bp->b_ops->verify_read(bp);
290 return;
291 default:
292 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
293 break;
294 }
295}
296
297static void
298xfs_dir3_data_read_verify(
299 struct xfs_buf *bp)
300{
301 struct xfs_mount *mp = bp->b_target->bt_mount;
302 xfs_failaddr_t fa;
303
304 if (xfs_sb_version_hascrc(&mp->m_sb) &&
305 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
306 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
307 else {
308 fa = xfs_dir3_data_verify(bp);
309 if (fa)
310 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
311 }
312}
313
314static void
315xfs_dir3_data_write_verify(
316 struct xfs_buf *bp)
317{
318 struct xfs_mount *mp = bp->b_target->bt_mount;
319 struct xfs_buf_log_item *bip = bp->b_log_item;
320 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
321 xfs_failaddr_t fa;
322
323 fa = xfs_dir3_data_verify(bp);
324 if (fa) {
325 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
326 return;
327 }
328
329 if (!xfs_sb_version_hascrc(&mp->m_sb))
330 return;
331
332 if (bip)
333 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
334
335 xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
336}
337
338const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
339 .name = "xfs_dir3_data",
340 .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC),
341 cpu_to_be32(XFS_DIR3_DATA_MAGIC) },
342 .verify_read = xfs_dir3_data_read_verify,
343 .verify_write = xfs_dir3_data_write_verify,
344 .verify_struct = xfs_dir3_data_verify,
345};
346
347static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
348 .name = "xfs_dir3_data_reada",
349 .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC),
350 cpu_to_be32(XFS_DIR3_DATA_MAGIC) },
351 .verify_read = xfs_dir3_data_reada_verify,
352 .verify_write = xfs_dir3_data_write_verify,
353};
354
355
356int
357xfs_dir3_data_read(
358 struct xfs_trans *tp,
359 struct xfs_inode *dp,
360 xfs_dablk_t bno,
361 xfs_daddr_t mapped_bno,
362 struct xfs_buf **bpp)
363{
364 int err;
365
366 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
367 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
368 if (!err && tp && *bpp)
369 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
370 return err;
371}
372
373int
374xfs_dir3_data_readahead(
375 struct xfs_inode *dp,
376 xfs_dablk_t bno,
377 xfs_daddr_t mapped_bno)
378{
379 return xfs_da_reada_buf(dp, bno, mapped_bno,
380 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
381}
382
383
384
385
386
387static xfs_failaddr_t
388xfs_dir2_data_freefind_verify(
389 struct xfs_dir2_data_hdr *hdr,
390 struct xfs_dir2_data_free *bf,
391 struct xfs_dir2_data_unused *dup,
392 struct xfs_dir2_data_free **bf_ent)
393{
394 struct xfs_dir2_data_free *dfp;
395 xfs_dir2_data_aoff_t off;
396 bool matched = false;
397 bool seenzero = false;
398
399 *bf_ent = NULL;
400 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
401
402
403
404
405
406
407 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
408 if (!dfp->offset) {
409 if (dfp->length)
410 return __this_address;
411 seenzero = true;
412 continue;
413 }
414 if (seenzero)
415 return __this_address;
416 if (be16_to_cpu(dfp->offset) == off) {
417 matched = true;
418 if (dfp->length != dup->length)
419 return __this_address;
420 } else if (be16_to_cpu(dfp->offset) > off) {
421 if (off + be16_to_cpu(dup->length) >
422 be16_to_cpu(dfp->offset))
423 return __this_address;
424 } else {
425 if (be16_to_cpu(dfp->offset) +
426 be16_to_cpu(dfp->length) > off)
427 return __this_address;
428 }
429 if (!matched &&
430 be16_to_cpu(dfp->length) < be16_to_cpu(dup->length))
431 return __this_address;
432 if (dfp > &bf[0] &&
433 be16_to_cpu(dfp[-1].length) < be16_to_cpu(dfp[0].length))
434 return __this_address;
435 }
436
437
438 *bf_ent = xfs_dir2_data_freefind(hdr, bf, dup);
439 return NULL;
440}
441
442
443
444
445
446xfs_dir2_data_free_t *
447xfs_dir2_data_freefind(
448 struct xfs_dir2_data_hdr *hdr,
449 struct xfs_dir2_data_free *bf,
450 struct xfs_dir2_data_unused *dup)
451{
452 xfs_dir2_data_free_t *dfp;
453 xfs_dir2_data_aoff_t off;
454
455 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
456
457
458
459
460
461 if (be16_to_cpu(dup->length) <
462 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
463 return NULL;
464
465
466
467 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
468 if (!dfp->offset)
469 return NULL;
470 if (be16_to_cpu(dfp->offset) == off)
471 return dfp;
472 }
473
474
475
476 return NULL;
477}
478
479
480
481
482xfs_dir2_data_free_t *
483xfs_dir2_data_freeinsert(
484 struct xfs_dir2_data_hdr *hdr,
485 struct xfs_dir2_data_free *dfp,
486 struct xfs_dir2_data_unused *dup,
487 int *loghead)
488{
489 xfs_dir2_data_free_t new;
490
491 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
492 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
493 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
494 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
495
496 new.length = dup->length;
497 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
498
499
500
501
502 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
503 dfp[2] = dfp[1];
504 dfp[1] = dfp[0];
505 dfp[0] = new;
506 *loghead = 1;
507 return &dfp[0];
508 }
509 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
510 dfp[2] = dfp[1];
511 dfp[1] = new;
512 *loghead = 1;
513 return &dfp[1];
514 }
515 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
516 dfp[2] = new;
517 *loghead = 1;
518 return &dfp[2];
519 }
520 return NULL;
521}
522
523
524
525
526STATIC void
527xfs_dir2_data_freeremove(
528 struct xfs_dir2_data_hdr *hdr,
529 struct xfs_dir2_data_free *bf,
530 struct xfs_dir2_data_free *dfp,
531 int *loghead)
532{
533
534 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
535 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
536 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
537 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
538
539
540
541
542 if (dfp == &bf[0]) {
543 bf[0] = bf[1];
544 bf[1] = bf[2];
545 }
546
547
548
549 else if (dfp == &bf[1])
550 bf[1] = bf[2];
551
552
553
554 else
555 ASSERT(dfp == &bf[2]);
556
557
558
559 bf[2].length = 0;
560 bf[2].offset = 0;
561 *loghead = 1;
562}
563
564
565
566
567void
568xfs_dir2_data_freescan_int(
569 struct xfs_da_geometry *geo,
570 const struct xfs_dir_ops *ops,
571 struct xfs_dir2_data_hdr *hdr,
572 int *loghead)
573{
574 xfs_dir2_data_entry_t *dep;
575 xfs_dir2_data_unused_t *dup;
576 struct xfs_dir2_data_free *bf;
577 char *endp;
578 char *p;
579
580 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
581 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
582 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
583 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
584
585
586
587
588 bf = ops->data_bestfree_p(hdr);
589 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
590 *loghead = 1;
591
592
593
594 p = (char *)ops->data_entry_p(hdr);
595 endp = xfs_dir3_data_endp(geo, hdr);
596
597
598
599 while (p < endp) {
600 dup = (xfs_dir2_data_unused_t *)p;
601
602
603
604 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
605 ASSERT((char *)dup - (char *)hdr ==
606 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
607 xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
608 p += be16_to_cpu(dup->length);
609 }
610
611
612
613 else {
614 dep = (xfs_dir2_data_entry_t *)p;
615 ASSERT((char *)dep - (char *)hdr ==
616 be16_to_cpu(*ops->data_entry_tag_p(dep)));
617 p += ops->data_entsize(dep->namelen);
618 }
619 }
620}
621
622void
623xfs_dir2_data_freescan(
624 struct xfs_inode *dp,
625 struct xfs_dir2_data_hdr *hdr,
626 int *loghead)
627{
628 return xfs_dir2_data_freescan_int(dp->i_mount->m_dir_geo, dp->d_ops,
629 hdr, loghead);
630}
631
632
633
634
635
636int
637xfs_dir3_data_init(
638 xfs_da_args_t *args,
639 xfs_dir2_db_t blkno,
640 struct xfs_buf **bpp)
641{
642 struct xfs_buf *bp;
643 xfs_dir2_data_hdr_t *hdr;
644 xfs_inode_t *dp;
645 xfs_dir2_data_unused_t *dup;
646 struct xfs_dir2_data_free *bf;
647 int error;
648 int i;
649 xfs_mount_t *mp;
650 xfs_trans_t *tp;
651 int t;
652
653 dp = args->dp;
654 mp = dp->i_mount;
655 tp = args->trans;
656
657
658
659 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
660 -1, &bp, XFS_DATA_FORK);
661 if (error)
662 return error;
663 bp->b_ops = &xfs_dir3_data_buf_ops;
664 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
665
666
667
668
669 hdr = bp->b_addr;
670 if (xfs_sb_version_hascrc(&mp->m_sb)) {
671 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
672
673 memset(hdr3, 0, sizeof(*hdr3));
674 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
675 hdr3->blkno = cpu_to_be64(bp->b_bn);
676 hdr3->owner = cpu_to_be64(dp->i_ino);
677 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
678
679 } else
680 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
681
682 bf = dp->d_ops->data_bestfree_p(hdr);
683 bf[0].offset = cpu_to_be16(dp->d_ops->data_entry_offset);
684 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
685 bf[i].length = 0;
686 bf[i].offset = 0;
687 }
688
689
690
691
692 dup = dp->d_ops->data_unused_p(hdr);
693 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
694
695 t = args->geo->blksize - (uint)dp->d_ops->data_entry_offset;
696 bf[0].length = cpu_to_be16(t);
697 dup->length = cpu_to_be16(t);
698 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
699
700
701
702 xfs_dir2_data_log_header(args, bp);
703 xfs_dir2_data_log_unused(args, bp, dup);
704 *bpp = bp;
705 return 0;
706}
707
708
709
710
711void
712xfs_dir2_data_log_entry(
713 struct xfs_da_args *args,
714 struct xfs_buf *bp,
715 xfs_dir2_data_entry_t *dep)
716{
717 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
718
719 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
720 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
721 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
722 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
723
724 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr),
725 (uint)((char *)(args->dp->d_ops->data_entry_tag_p(dep) + 1) -
726 (char *)hdr - 1));
727}
728
729
730
731
732void
733xfs_dir2_data_log_header(
734 struct xfs_da_args *args,
735 struct xfs_buf *bp)
736{
737#ifdef DEBUG
738 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
739
740 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
741 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
742 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
743 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
744#endif
745
746 xfs_trans_log_buf(args->trans, bp, 0,
747 args->dp->d_ops->data_entry_offset - 1);
748}
749
750
751
752
753void
754xfs_dir2_data_log_unused(
755 struct xfs_da_args *args,
756 struct xfs_buf *bp,
757 xfs_dir2_data_unused_t *dup)
758{
759 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
760
761 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
762 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
763 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
764 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
765
766
767
768
769 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
770 (uint)((char *)&dup->length + sizeof(dup->length) -
771 1 - (char *)hdr));
772
773
774
775 xfs_trans_log_buf(args->trans, bp,
776 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
777 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
778 sizeof(xfs_dir2_data_off_t) - 1));
779}
780
781
782
783
784
785void
786xfs_dir2_data_make_free(
787 struct xfs_da_args *args,
788 struct xfs_buf *bp,
789 xfs_dir2_data_aoff_t offset,
790 xfs_dir2_data_aoff_t len,
791 int *needlogp,
792 int *needscanp)
793{
794 xfs_dir2_data_hdr_t *hdr;
795 xfs_dir2_data_free_t *dfp;
796 char *endptr;
797 int needscan;
798 xfs_dir2_data_unused_t *newdup;
799 xfs_dir2_data_unused_t *postdup;
800 xfs_dir2_data_unused_t *prevdup;
801 struct xfs_dir2_data_free *bf;
802
803 hdr = bp->b_addr;
804
805
806
807
808 endptr = xfs_dir3_data_endp(args->geo, hdr);
809 ASSERT(endptr != NULL);
810
811
812
813
814
815 if (offset > args->dp->d_ops->data_entry_offset) {
816 __be16 *tagp;
817
818 tagp = (__be16 *)((char *)hdr + offset) - 1;
819 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
820 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
821 prevdup = NULL;
822 } else
823 prevdup = NULL;
824
825
826
827
828 if ((char *)hdr + offset + len < endptr) {
829 postdup =
830 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
831 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
832 postdup = NULL;
833 } else
834 postdup = NULL;
835 ASSERT(*needscanp == 0);
836 needscan = 0;
837
838
839
840
841 bf = args->dp->d_ops->data_bestfree_p(hdr);
842 if (prevdup && postdup) {
843 xfs_dir2_data_free_t *dfp2;
844
845
846
847
848 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
849 dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
850
851
852
853
854
855
856 needscan = (bf[2].length != 0);
857
858
859
860 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
861 *xfs_dir2_data_unused_tag_p(prevdup) =
862 cpu_to_be16((char *)prevdup - (char *)hdr);
863 xfs_dir2_data_log_unused(args, bp, prevdup);
864 if (!needscan) {
865
866
867
868
869
870
871 ASSERT(dfp && dfp2);
872 if (dfp == &bf[1]) {
873 dfp = &bf[0];
874 ASSERT(dfp2 == dfp);
875 dfp2 = &bf[1];
876 }
877 xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
878 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
879
880
881
882 dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
883 needlogp);
884 ASSERT(dfp == &bf[0]);
885 ASSERT(dfp->length == prevdup->length);
886 ASSERT(!dfp[1].length);
887 ASSERT(!dfp[2].length);
888 }
889 }
890
891
892
893 else if (prevdup) {
894 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
895 be16_add_cpu(&prevdup->length, len);
896 *xfs_dir2_data_unused_tag_p(prevdup) =
897 cpu_to_be16((char *)prevdup - (char *)hdr);
898 xfs_dir2_data_log_unused(args, bp, prevdup);
899
900
901
902
903
904 if (dfp) {
905 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
906 xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
907 }
908
909
910
911 else {
912 needscan = be16_to_cpu(prevdup->length) >
913 be16_to_cpu(bf[2].length);
914 }
915 }
916
917
918
919 else if (postdup) {
920 dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
921 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
922 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
923 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
924 *xfs_dir2_data_unused_tag_p(newdup) =
925 cpu_to_be16((char *)newdup - (char *)hdr);
926 xfs_dir2_data_log_unused(args, bp, newdup);
927
928
929
930
931
932 if (dfp) {
933 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
934 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
935 }
936
937
938
939 else {
940 needscan = be16_to_cpu(newdup->length) >
941 be16_to_cpu(bf[2].length);
942 }
943 }
944
945
946
947 else {
948 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
949 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
950 newdup->length = cpu_to_be16(len);
951 *xfs_dir2_data_unused_tag_p(newdup) =
952 cpu_to_be16((char *)newdup - (char *)hdr);
953 xfs_dir2_data_log_unused(args, bp, newdup);
954 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
955 }
956 *needscanp = needscan;
957}
958
959
960static inline xfs_failaddr_t
961xfs_dir2_data_check_free(
962 struct xfs_dir2_data_hdr *hdr,
963 struct xfs_dir2_data_unused *dup,
964 xfs_dir2_data_aoff_t offset,
965 xfs_dir2_data_aoff_t len)
966{
967 if (hdr->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC) &&
968 hdr->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC) &&
969 hdr->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) &&
970 hdr->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
971 return __this_address;
972 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG)
973 return __this_address;
974 if (offset < (char *)dup - (char *)hdr)
975 return __this_address;
976 if (offset + len > (char *)dup + be16_to_cpu(dup->length) - (char *)hdr)
977 return __this_address;
978 if ((char *)dup - (char *)hdr !=
979 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)))
980 return __this_address;
981 return NULL;
982}
983
984
985static inline xfs_failaddr_t
986xfs_dir2_data_check_new_free(
987 struct xfs_dir2_data_hdr *hdr,
988 struct xfs_dir2_data_free *dfp,
989 struct xfs_dir2_data_unused *newdup)
990{
991 if (dfp == NULL)
992 return __this_address;
993 if (dfp->length != newdup->length)
994 return __this_address;
995 if (be16_to_cpu(dfp->offset) != (char *)newdup - (char *)hdr)
996 return __this_address;
997 return NULL;
998}
999
1000
1001
1002
1003int
1004xfs_dir2_data_use_free(
1005 struct xfs_da_args *args,
1006 struct xfs_buf *bp,
1007 xfs_dir2_data_unused_t *dup,
1008 xfs_dir2_data_aoff_t offset,
1009 xfs_dir2_data_aoff_t len,
1010 int *needlogp,
1011 int *needscanp)
1012{
1013 xfs_dir2_data_hdr_t *hdr;
1014 xfs_dir2_data_free_t *dfp;
1015 xfs_dir2_data_unused_t *newdup;
1016 xfs_dir2_data_unused_t *newdup2;
1017 struct xfs_dir2_data_free *bf;
1018 xfs_failaddr_t fa;
1019 int matchback;
1020 int matchfront;
1021 int needscan;
1022 int oldlen;
1023
1024 hdr = bp->b_addr;
1025 fa = xfs_dir2_data_check_free(hdr, dup, offset, len);
1026 if (fa)
1027 goto corrupt;
1028
1029
1030
1031 oldlen = be16_to_cpu(dup->length);
1032 bf = args->dp->d_ops->data_bestfree_p(hdr);
1033 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
1034 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
1035
1036
1037
1038 matchfront = (char *)dup - (char *)hdr == offset;
1039 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
1040 ASSERT(*needscanp == 0);
1041 needscan = 0;
1042
1043
1044
1045
1046 if (matchfront && matchback) {
1047 if (dfp) {
1048 needscan = (bf[2].offset != 0);
1049 if (!needscan)
1050 xfs_dir2_data_freeremove(hdr, bf, dfp,
1051 needlogp);
1052 }
1053 }
1054
1055
1056
1057
1058 else if (matchfront) {
1059 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1060 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1061 newdup->length = cpu_to_be16(oldlen - len);
1062 *xfs_dir2_data_unused_tag_p(newdup) =
1063 cpu_to_be16((char *)newdup - (char *)hdr);
1064 xfs_dir2_data_log_unused(args, bp, newdup);
1065
1066
1067
1068 if (dfp) {
1069 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1070 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1071 needlogp);
1072 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1073 if (fa)
1074 goto corrupt;
1075
1076
1077
1078
1079
1080 needscan = dfp == &bf[2];
1081 }
1082 }
1083
1084
1085
1086
1087 else if (matchback) {
1088 newdup = dup;
1089 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1090 *xfs_dir2_data_unused_tag_p(newdup) =
1091 cpu_to_be16((char *)newdup - (char *)hdr);
1092 xfs_dir2_data_log_unused(args, bp, newdup);
1093
1094
1095
1096 if (dfp) {
1097 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1098 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1099 needlogp);
1100 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1101 if (fa)
1102 goto corrupt;
1103
1104
1105
1106
1107
1108 needscan = dfp == &bf[2];
1109 }
1110 }
1111
1112
1113
1114
1115 else {
1116 newdup = dup;
1117 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1118 *xfs_dir2_data_unused_tag_p(newdup) =
1119 cpu_to_be16((char *)newdup - (char *)hdr);
1120 xfs_dir2_data_log_unused(args, bp, newdup);
1121 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1122 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1123 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
1124 *xfs_dir2_data_unused_tag_p(newdup2) =
1125 cpu_to_be16((char *)newdup2 - (char *)hdr);
1126 xfs_dir2_data_log_unused(args, bp, newdup2);
1127
1128
1129
1130
1131
1132
1133
1134
1135 if (dfp) {
1136 needscan = (bf[2].length != 0);
1137 if (!needscan) {
1138 xfs_dir2_data_freeremove(hdr, bf, dfp,
1139 needlogp);
1140 xfs_dir2_data_freeinsert(hdr, bf, newdup,
1141 needlogp);
1142 xfs_dir2_data_freeinsert(hdr, bf, newdup2,
1143 needlogp);
1144 }
1145 }
1146 }
1147 *needscanp = needscan;
1148 return 0;
1149corrupt:
1150 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, args->dp->i_mount,
1151 hdr, sizeof(*hdr), __FILE__, __LINE__, fa);
1152 return -EFSCORRUPTED;
1153}
1154
1155
1156void *
1157xfs_dir3_data_endp(
1158 struct xfs_da_geometry *geo,
1159 struct xfs_dir2_data_hdr *hdr)
1160{
1161 switch (hdr->magic) {
1162 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
1163 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
1164 return xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo, hdr));
1165 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
1166 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
1167 return (char *)hdr + geo->blksize;
1168 default:
1169 return NULL;
1170 }
1171}
1172