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