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_sb_version_hascrc(&mp->m_sb)) {
256 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
257 return __this_address;
258 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
259 return __this_address;
260 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
261 return __this_address;
262 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
263 return __this_address;
264 } else {
265 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
266 return __this_address;
267 }
268 return __xfs_dir3_data_check(NULL, bp);
269}
270
271
272
273
274
275
276static void
277xfs_dir3_data_reada_verify(
278 struct xfs_buf *bp)
279{
280 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
281
282 switch (hdr->magic) {
283 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
284 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
285 bp->b_ops = &xfs_dir3_block_buf_ops;
286 bp->b_ops->verify_read(bp);
287 return;
288 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
289 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
290 bp->b_ops = &xfs_dir3_data_buf_ops;
291 bp->b_ops->verify_read(bp);
292 return;
293 default:
294 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
295 break;
296 }
297}
298
299static void
300xfs_dir3_data_read_verify(
301 struct xfs_buf *bp)
302{
303 struct xfs_mount *mp = bp->b_target->bt_mount;
304 xfs_failaddr_t fa;
305
306 if (xfs_sb_version_hascrc(&mp->m_sb) &&
307 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
308 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
309 else {
310 fa = xfs_dir3_data_verify(bp);
311 if (fa)
312 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
313 }
314}
315
316static void
317xfs_dir3_data_write_verify(
318 struct xfs_buf *bp)
319{
320 struct xfs_mount *mp = bp->b_target->bt_mount;
321 struct xfs_buf_log_item *bip = bp->b_log_item;
322 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
323 xfs_failaddr_t fa;
324
325 fa = xfs_dir3_data_verify(bp);
326 if (fa) {
327 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
328 return;
329 }
330
331 if (!xfs_sb_version_hascrc(&mp->m_sb))
332 return;
333
334 if (bip)
335 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
336
337 xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
338}
339
340const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
341 .name = "xfs_dir3_data",
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 .verify_read = xfs_dir3_data_reada_verify,
350 .verify_write = xfs_dir3_data_write_verify,
351};
352
353
354int
355xfs_dir3_data_read(
356 struct xfs_trans *tp,
357 struct xfs_inode *dp,
358 xfs_dablk_t bno,
359 xfs_daddr_t mapped_bno,
360 struct xfs_buf **bpp)
361{
362 int err;
363
364 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
365 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
366 if (!err && tp && *bpp)
367 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
368 return err;
369}
370
371int
372xfs_dir3_data_readahead(
373 struct xfs_inode *dp,
374 xfs_dablk_t bno,
375 xfs_daddr_t mapped_bno)
376{
377 return xfs_da_reada_buf(dp, bno, mapped_bno,
378 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
379}
380
381
382
383
384
385static xfs_failaddr_t
386xfs_dir2_data_freefind_verify(
387 struct xfs_dir2_data_hdr *hdr,
388 struct xfs_dir2_data_free *bf,
389 struct xfs_dir2_data_unused *dup,
390 struct xfs_dir2_data_free **bf_ent)
391{
392 struct xfs_dir2_data_free *dfp;
393 xfs_dir2_data_aoff_t off;
394 bool matched = false;
395 bool seenzero = false;
396
397 *bf_ent = NULL;
398 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
399
400
401
402
403
404
405 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
406 if (!dfp->offset) {
407 if (dfp->length)
408 return __this_address;
409 seenzero = true;
410 continue;
411 }
412 if (seenzero)
413 return __this_address;
414 if (be16_to_cpu(dfp->offset) == off) {
415 matched = true;
416 if (dfp->length != dup->length)
417 return __this_address;
418 } else if (be16_to_cpu(dfp->offset) > off) {
419 if (off + be16_to_cpu(dup->length) >
420 be16_to_cpu(dfp->offset))
421 return __this_address;
422 } else {
423 if (be16_to_cpu(dfp->offset) +
424 be16_to_cpu(dfp->length) > off)
425 return __this_address;
426 }
427 if (!matched &&
428 be16_to_cpu(dfp->length) < be16_to_cpu(dup->length))
429 return __this_address;
430 if (dfp > &bf[0] &&
431 be16_to_cpu(dfp[-1].length) < be16_to_cpu(dfp[0].length))
432 return __this_address;
433 }
434
435
436 *bf_ent = xfs_dir2_data_freefind(hdr, bf, dup);
437 return NULL;
438}
439
440
441
442
443
444xfs_dir2_data_free_t *
445xfs_dir2_data_freefind(
446 struct xfs_dir2_data_hdr *hdr,
447 struct xfs_dir2_data_free *bf,
448 struct xfs_dir2_data_unused *dup)
449{
450 xfs_dir2_data_free_t *dfp;
451 xfs_dir2_data_aoff_t off;
452
453 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
454
455
456
457
458
459 if (be16_to_cpu(dup->length) <
460 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
461 return NULL;
462
463
464
465 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
466 if (!dfp->offset)
467 return NULL;
468 if (be16_to_cpu(dfp->offset) == off)
469 return dfp;
470 }
471
472
473
474 return NULL;
475}
476
477
478
479
480xfs_dir2_data_free_t *
481xfs_dir2_data_freeinsert(
482 struct xfs_dir2_data_hdr *hdr,
483 struct xfs_dir2_data_free *dfp,
484 struct xfs_dir2_data_unused *dup,
485 int *loghead)
486{
487 xfs_dir2_data_free_t new;
488
489 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
490 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
491 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
492 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
493
494 new.length = dup->length;
495 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
496
497
498
499
500 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
501 dfp[2] = dfp[1];
502 dfp[1] = dfp[0];
503 dfp[0] = new;
504 *loghead = 1;
505 return &dfp[0];
506 }
507 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
508 dfp[2] = dfp[1];
509 dfp[1] = new;
510 *loghead = 1;
511 return &dfp[1];
512 }
513 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
514 dfp[2] = new;
515 *loghead = 1;
516 return &dfp[2];
517 }
518 return NULL;
519}
520
521
522
523
524STATIC void
525xfs_dir2_data_freeremove(
526 struct xfs_dir2_data_hdr *hdr,
527 struct xfs_dir2_data_free *bf,
528 struct xfs_dir2_data_free *dfp,
529 int *loghead)
530{
531
532 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
533 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
534 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
535 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
536
537
538
539
540 if (dfp == &bf[0]) {
541 bf[0] = bf[1];
542 bf[1] = bf[2];
543 }
544
545
546
547 else if (dfp == &bf[1])
548 bf[1] = bf[2];
549
550
551
552 else
553 ASSERT(dfp == &bf[2]);
554
555
556
557 bf[2].length = 0;
558 bf[2].offset = 0;
559 *loghead = 1;
560}
561
562
563
564
565void
566xfs_dir2_data_freescan_int(
567 struct xfs_da_geometry *geo,
568 const struct xfs_dir_ops *ops,
569 struct xfs_dir2_data_hdr *hdr,
570 int *loghead)
571{
572 xfs_dir2_data_entry_t *dep;
573 xfs_dir2_data_unused_t *dup;
574 struct xfs_dir2_data_free *bf;
575 char *endp;
576 char *p;
577
578 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
579 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
580 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
581 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
582
583
584
585
586 bf = ops->data_bestfree_p(hdr);
587 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
588 *loghead = 1;
589
590
591
592 p = (char *)ops->data_entry_p(hdr);
593 endp = xfs_dir3_data_endp(geo, hdr);
594
595
596
597 while (p < endp) {
598 dup = (xfs_dir2_data_unused_t *)p;
599
600
601
602 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
603 ASSERT((char *)dup - (char *)hdr ==
604 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
605 xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
606 p += be16_to_cpu(dup->length);
607 }
608
609
610
611 else {
612 dep = (xfs_dir2_data_entry_t *)p;
613 ASSERT((char *)dep - (char *)hdr ==
614 be16_to_cpu(*ops->data_entry_tag_p(dep)));
615 p += ops->data_entsize(dep->namelen);
616 }
617 }
618}
619
620void
621xfs_dir2_data_freescan(
622 struct xfs_inode *dp,
623 struct xfs_dir2_data_hdr *hdr,
624 int *loghead)
625{
626 return xfs_dir2_data_freescan_int(dp->i_mount->m_dir_geo, dp->d_ops,
627 hdr, loghead);
628}
629
630
631
632
633
634int
635xfs_dir3_data_init(
636 xfs_da_args_t *args,
637 xfs_dir2_db_t blkno,
638 struct xfs_buf **bpp)
639{
640 struct xfs_buf *bp;
641 xfs_dir2_data_hdr_t *hdr;
642 xfs_inode_t *dp;
643 xfs_dir2_data_unused_t *dup;
644 struct xfs_dir2_data_free *bf;
645 int error;
646 int i;
647 xfs_mount_t *mp;
648 xfs_trans_t *tp;
649 int t;
650
651 dp = args->dp;
652 mp = dp->i_mount;
653 tp = args->trans;
654
655
656
657 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
658 -1, &bp, XFS_DATA_FORK);
659 if (error)
660 return error;
661 bp->b_ops = &xfs_dir3_data_buf_ops;
662 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
663
664
665
666
667 hdr = bp->b_addr;
668 if (xfs_sb_version_hascrc(&mp->m_sb)) {
669 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
670
671 memset(hdr3, 0, sizeof(*hdr3));
672 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
673 hdr3->blkno = cpu_to_be64(bp->b_bn);
674 hdr3->owner = cpu_to_be64(dp->i_ino);
675 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
676
677 } else
678 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
679
680 bf = dp->d_ops->data_bestfree_p(hdr);
681 bf[0].offset = cpu_to_be16(dp->d_ops->data_entry_offset);
682 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
683 bf[i].length = 0;
684 bf[i].offset = 0;
685 }
686
687
688
689
690 dup = dp->d_ops->data_unused_p(hdr);
691 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
692
693 t = args->geo->blksize - (uint)dp->d_ops->data_entry_offset;
694 bf[0].length = cpu_to_be16(t);
695 dup->length = cpu_to_be16(t);
696 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
697
698
699
700 xfs_dir2_data_log_header(args, bp);
701 xfs_dir2_data_log_unused(args, bp, dup);
702 *bpp = bp;
703 return 0;
704}
705
706
707
708
709void
710xfs_dir2_data_log_entry(
711 struct xfs_da_args *args,
712 struct xfs_buf *bp,
713 xfs_dir2_data_entry_t *dep)
714{
715 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
716
717 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
718 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
719 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
720 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
721
722 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr),
723 (uint)((char *)(args->dp->d_ops->data_entry_tag_p(dep) + 1) -
724 (char *)hdr - 1));
725}
726
727
728
729
730void
731xfs_dir2_data_log_header(
732 struct xfs_da_args *args,
733 struct xfs_buf *bp)
734{
735#ifdef DEBUG
736 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
737
738 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
739 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
740 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
741 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
742#endif
743
744 xfs_trans_log_buf(args->trans, bp, 0,
745 args->dp->d_ops->data_entry_offset - 1);
746}
747
748
749
750
751void
752xfs_dir2_data_log_unused(
753 struct xfs_da_args *args,
754 struct xfs_buf *bp,
755 xfs_dir2_data_unused_t *dup)
756{
757 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
758
759 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
760 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
761 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
762 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
763
764
765
766
767 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
768 (uint)((char *)&dup->length + sizeof(dup->length) -
769 1 - (char *)hdr));
770
771
772
773 xfs_trans_log_buf(args->trans, bp,
774 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
775 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
776 sizeof(xfs_dir2_data_off_t) - 1));
777}
778
779
780
781
782
783void
784xfs_dir2_data_make_free(
785 struct xfs_da_args *args,
786 struct xfs_buf *bp,
787 xfs_dir2_data_aoff_t offset,
788 xfs_dir2_data_aoff_t len,
789 int *needlogp,
790 int *needscanp)
791{
792 xfs_dir2_data_hdr_t *hdr;
793 xfs_dir2_data_free_t *dfp;
794 char *endptr;
795 int needscan;
796 xfs_dir2_data_unused_t *newdup;
797 xfs_dir2_data_unused_t *postdup;
798 xfs_dir2_data_unused_t *prevdup;
799 struct xfs_dir2_data_free *bf;
800
801 hdr = bp->b_addr;
802
803
804
805
806 endptr = xfs_dir3_data_endp(args->geo, hdr);
807 ASSERT(endptr != NULL);
808
809
810
811
812
813 if (offset > args->dp->d_ops->data_entry_offset) {
814 __be16 *tagp;
815
816 tagp = (__be16 *)((char *)hdr + offset) - 1;
817 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
818 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
819 prevdup = NULL;
820 } else
821 prevdup = NULL;
822
823
824
825
826 if ((char *)hdr + offset + len < endptr) {
827 postdup =
828 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
829 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
830 postdup = NULL;
831 } else
832 postdup = NULL;
833 ASSERT(*needscanp == 0);
834 needscan = 0;
835
836
837
838
839 bf = args->dp->d_ops->data_bestfree_p(hdr);
840 if (prevdup && postdup) {
841 xfs_dir2_data_free_t *dfp2;
842
843
844
845
846 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
847 dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
848
849
850
851
852
853
854 needscan = (bf[2].length != 0);
855
856
857
858 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
859 *xfs_dir2_data_unused_tag_p(prevdup) =
860 cpu_to_be16((char *)prevdup - (char *)hdr);
861 xfs_dir2_data_log_unused(args, bp, prevdup);
862 if (!needscan) {
863
864
865
866
867
868
869 ASSERT(dfp && dfp2);
870 if (dfp == &bf[1]) {
871 dfp = &bf[0];
872 ASSERT(dfp2 == dfp);
873 dfp2 = &bf[1];
874 }
875 xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
876 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
877
878
879
880 dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
881 needlogp);
882 ASSERT(dfp == &bf[0]);
883 ASSERT(dfp->length == prevdup->length);
884 ASSERT(!dfp[1].length);
885 ASSERT(!dfp[2].length);
886 }
887 }
888
889
890
891 else if (prevdup) {
892 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
893 be16_add_cpu(&prevdup->length, len);
894 *xfs_dir2_data_unused_tag_p(prevdup) =
895 cpu_to_be16((char *)prevdup - (char *)hdr);
896 xfs_dir2_data_log_unused(args, bp, prevdup);
897
898
899
900
901
902 if (dfp) {
903 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
904 xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
905 }
906
907
908
909 else {
910 needscan = be16_to_cpu(prevdup->length) >
911 be16_to_cpu(bf[2].length);
912 }
913 }
914
915
916
917 else if (postdup) {
918 dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
919 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
920 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
921 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
922 *xfs_dir2_data_unused_tag_p(newdup) =
923 cpu_to_be16((char *)newdup - (char *)hdr);
924 xfs_dir2_data_log_unused(args, bp, newdup);
925
926
927
928
929
930 if (dfp) {
931 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
932 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
933 }
934
935
936
937 else {
938 needscan = be16_to_cpu(newdup->length) >
939 be16_to_cpu(bf[2].length);
940 }
941 }
942
943
944
945 else {
946 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
947 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
948 newdup->length = cpu_to_be16(len);
949 *xfs_dir2_data_unused_tag_p(newdup) =
950 cpu_to_be16((char *)newdup - (char *)hdr);
951 xfs_dir2_data_log_unused(args, bp, newdup);
952 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
953 }
954 *needscanp = needscan;
955}
956
957
958static inline xfs_failaddr_t
959xfs_dir2_data_check_free(
960 struct xfs_dir2_data_hdr *hdr,
961 struct xfs_dir2_data_unused *dup,
962 xfs_dir2_data_aoff_t offset,
963 xfs_dir2_data_aoff_t len)
964{
965 if (hdr->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC) &&
966 hdr->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC) &&
967 hdr->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) &&
968 hdr->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
969 return __this_address;
970 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG)
971 return __this_address;
972 if (offset < (char *)dup - (char *)hdr)
973 return __this_address;
974 if (offset + len > (char *)dup + be16_to_cpu(dup->length) - (char *)hdr)
975 return __this_address;
976 if ((char *)dup - (char *)hdr !=
977 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)))
978 return __this_address;
979 return NULL;
980}
981
982
983static inline xfs_failaddr_t
984xfs_dir2_data_check_new_free(
985 struct xfs_dir2_data_hdr *hdr,
986 struct xfs_dir2_data_free *dfp,
987 struct xfs_dir2_data_unused *newdup)
988{
989 if (dfp == NULL)
990 return __this_address;
991 if (dfp->length != newdup->length)
992 return __this_address;
993 if (be16_to_cpu(dfp->offset) != (char *)newdup - (char *)hdr)
994 return __this_address;
995 return NULL;
996}
997
998
999
1000
1001int
1002xfs_dir2_data_use_free(
1003 struct xfs_da_args *args,
1004 struct xfs_buf *bp,
1005 xfs_dir2_data_unused_t *dup,
1006 xfs_dir2_data_aoff_t offset,
1007 xfs_dir2_data_aoff_t len,
1008 int *needlogp,
1009 int *needscanp)
1010{
1011 xfs_dir2_data_hdr_t *hdr;
1012 xfs_dir2_data_free_t *dfp;
1013 xfs_dir2_data_unused_t *newdup;
1014 xfs_dir2_data_unused_t *newdup2;
1015 struct xfs_dir2_data_free *bf;
1016 xfs_failaddr_t fa;
1017 int matchback;
1018 int matchfront;
1019 int needscan;
1020 int oldlen;
1021
1022 hdr = bp->b_addr;
1023 fa = xfs_dir2_data_check_free(hdr, dup, offset, len);
1024 if (fa)
1025 goto corrupt;
1026
1027
1028
1029 oldlen = be16_to_cpu(dup->length);
1030 bf = args->dp->d_ops->data_bestfree_p(hdr);
1031 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
1032 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
1033
1034
1035
1036 matchfront = (char *)dup - (char *)hdr == offset;
1037 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
1038 ASSERT(*needscanp == 0);
1039 needscan = 0;
1040
1041
1042
1043
1044 if (matchfront && matchback) {
1045 if (dfp) {
1046 needscan = (bf[2].offset != 0);
1047 if (!needscan)
1048 xfs_dir2_data_freeremove(hdr, bf, dfp,
1049 needlogp);
1050 }
1051 }
1052
1053
1054
1055
1056 else if (matchfront) {
1057 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1058 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1059 newdup->length = cpu_to_be16(oldlen - len);
1060 *xfs_dir2_data_unused_tag_p(newdup) =
1061 cpu_to_be16((char *)newdup - (char *)hdr);
1062 xfs_dir2_data_log_unused(args, bp, newdup);
1063
1064
1065
1066 if (dfp) {
1067 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1068 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1069 needlogp);
1070 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1071 if (fa)
1072 goto corrupt;
1073
1074
1075
1076
1077
1078 needscan = dfp == &bf[2];
1079 }
1080 }
1081
1082
1083
1084
1085 else if (matchback) {
1086 newdup = dup;
1087 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1088 *xfs_dir2_data_unused_tag_p(newdup) =
1089 cpu_to_be16((char *)newdup - (char *)hdr);
1090 xfs_dir2_data_log_unused(args, bp, newdup);
1091
1092
1093
1094 if (dfp) {
1095 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1096 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1097 needlogp);
1098 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1099 if (fa)
1100 goto corrupt;
1101
1102
1103
1104
1105
1106 needscan = dfp == &bf[2];
1107 }
1108 }
1109
1110
1111
1112
1113 else {
1114 newdup = dup;
1115 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1116 *xfs_dir2_data_unused_tag_p(newdup) =
1117 cpu_to_be16((char *)newdup - (char *)hdr);
1118 xfs_dir2_data_log_unused(args, bp, newdup);
1119 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1120 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1121 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
1122 *xfs_dir2_data_unused_tag_p(newdup2) =
1123 cpu_to_be16((char *)newdup2 - (char *)hdr);
1124 xfs_dir2_data_log_unused(args, bp, newdup2);
1125
1126
1127
1128
1129
1130
1131
1132
1133 if (dfp) {
1134 needscan = (bf[2].length != 0);
1135 if (!needscan) {
1136 xfs_dir2_data_freeremove(hdr, bf, dfp,
1137 needlogp);
1138 xfs_dir2_data_freeinsert(hdr, bf, newdup,
1139 needlogp);
1140 xfs_dir2_data_freeinsert(hdr, bf, newdup2,
1141 needlogp);
1142 }
1143 }
1144 }
1145 *needscanp = needscan;
1146 return 0;
1147corrupt:
1148 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, args->dp->i_mount,
1149 hdr, sizeof(*hdr), __FILE__, __LINE__, fa);
1150 return -EFSCORRUPTED;
1151}
1152
1153
1154void *
1155xfs_dir3_data_endp(
1156 struct xfs_da_geometry *geo,
1157 struct xfs_dir2_data_hdr *hdr)
1158{
1159 switch (hdr->magic) {
1160 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
1161 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
1162 return xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo, hdr));
1163 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
1164 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
1165 return (char *)hdr + geo->blksize;
1166 default:
1167 return NULL;
1168 }
1169}
1170