1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_log.h"
22#include "xfs_inum.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
26#include "xfs_mount.h"
27#include "xfs_da_btree.h"
28#include "xfs_bmap_btree.h"
29#include "xfs_dinode.h"
30#include "xfs_inode.h"
31#include "xfs_dir2_format.h"
32#include "xfs_dir2_priv.h"
33#include "xfs_error.h"
34
35STATIC xfs_dir2_data_free_t *
36xfs_dir2_data_freefind(xfs_dir2_data_hdr_t *hdr, xfs_dir2_data_unused_t *dup);
37
38#ifdef DEBUG
39
40
41
42
43
44void
45xfs_dir2_data_check(
46 xfs_inode_t *dp,
47 xfs_dabuf_t *bp)
48{
49 xfs_dir2_dataptr_t addr;
50 xfs_dir2_data_free_t *bf;
51 xfs_dir2_block_tail_t *btp=NULL;
52 int count;
53 xfs_dir2_data_hdr_t *hdr;
54 xfs_dir2_data_entry_t *dep;
55 xfs_dir2_data_free_t *dfp;
56 xfs_dir2_data_unused_t *dup;
57 char *endp;
58 int freeseen;
59 xfs_dahash_t hash;
60 int i;
61 int lastfree;
62 xfs_dir2_leaf_entry_t *lep=NULL;
63 xfs_mount_t *mp;
64 char *p;
65 int stale;
66 struct xfs_name name;
67
68 mp = dp->i_mount;
69 hdr = bp->data;
70 bf = hdr->bestfree;
71 p = (char *)(hdr + 1);
72
73 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
74 btp = xfs_dir2_block_tail_p(mp, hdr);
75 lep = xfs_dir2_block_leaf_p(btp);
76 endp = (char *)lep;
77 } else {
78 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
79 endp = (char *)hdr + mp->m_dirblksize;
80 }
81
82 count = lastfree = freeseen = 0;
83
84
85
86 if (!bf[0].length) {
87 ASSERT(!bf[0].offset);
88 freeseen |= 1 << 0;
89 }
90 if (!bf[1].length) {
91 ASSERT(!bf[1].offset);
92 freeseen |= 1 << 1;
93 }
94 if (!bf[2].length) {
95 ASSERT(!bf[2].offset);
96 freeseen |= 1 << 2;
97 }
98 ASSERT(be16_to_cpu(bf[0].length) >= be16_to_cpu(bf[1].length));
99 ASSERT(be16_to_cpu(bf[1].length) >= be16_to_cpu(bf[2].length));
100
101
102
103 while (p < endp) {
104 dup = (xfs_dir2_data_unused_t *)p;
105
106
107
108
109
110 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
111 ASSERT(lastfree == 0);
112 ASSERT(be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
113 (char *)dup - (char *)hdr);
114 dfp = xfs_dir2_data_freefind(hdr, dup);
115 if (dfp) {
116 i = (int)(dfp - bf);
117 ASSERT((freeseen & (1 << i)) == 0);
118 freeseen |= 1 << i;
119 } else {
120 ASSERT(be16_to_cpu(dup->length) <=
121 be16_to_cpu(bf[2].length));
122 }
123 p += be16_to_cpu(dup->length);
124 lastfree = 1;
125 continue;
126 }
127
128
129
130
131
132
133 dep = (xfs_dir2_data_entry_t *)p;
134 ASSERT(dep->namelen != 0);
135 ASSERT(xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)) == 0);
136 ASSERT(be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
137 (char *)dep - (char *)hdr);
138 count++;
139 lastfree = 0;
140 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
141 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
142 (xfs_dir2_data_aoff_t)
143 ((char *)dep - (char *)hdr));
144 name.name = dep->name;
145 name.len = dep->namelen;
146 hash = mp->m_dirnameops->hashname(&name);
147 for (i = 0; i < be32_to_cpu(btp->count); i++) {
148 if (be32_to_cpu(lep[i].address) == addr &&
149 be32_to_cpu(lep[i].hashval) == hash)
150 break;
151 }
152 ASSERT(i < be32_to_cpu(btp->count));
153 }
154 p += xfs_dir2_data_entsize(dep->namelen);
155 }
156
157
158
159 ASSERT(freeseen == 7);
160 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
161 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
162 if (lep[i].address ==
163 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
164 stale++;
165 if (i > 0)
166 ASSERT(be32_to_cpu(lep[i].hashval) >= be32_to_cpu(lep[i - 1].hashval));
167 }
168 ASSERT(count == be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
169 ASSERT(stale == be32_to_cpu(btp->stale));
170 }
171}
172#endif
173
174
175
176
177
178STATIC xfs_dir2_data_free_t *
179xfs_dir2_data_freefind(
180 xfs_dir2_data_hdr_t *hdr,
181 xfs_dir2_data_unused_t *dup)
182{
183 xfs_dir2_data_free_t *dfp;
184 xfs_dir2_data_aoff_t off;
185#if defined(DEBUG) && defined(__KERNEL__)
186 int matched;
187 int seenzero;
188#endif
189
190 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
191#if defined(DEBUG) && defined(__KERNEL__)
192
193
194
195
196
197 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
198 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
199 for (dfp = &hdr->bestfree[0], seenzero = matched = 0;
200 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
201 dfp++) {
202 if (!dfp->offset) {
203 ASSERT(!dfp->length);
204 seenzero = 1;
205 continue;
206 }
207 ASSERT(seenzero == 0);
208 if (be16_to_cpu(dfp->offset) == off) {
209 matched = 1;
210 ASSERT(dfp->length == dup->length);
211 } else if (off < be16_to_cpu(dfp->offset))
212 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
213 else
214 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
215 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
216 if (dfp > &hdr->bestfree[0])
217 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
218 }
219#endif
220
221
222
223
224 if (be16_to_cpu(dup->length) <
225 be16_to_cpu(hdr->bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
226 return NULL;
227
228
229
230 for (dfp = &hdr->bestfree[0];
231 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
232 dfp++) {
233 if (!dfp->offset)
234 return NULL;
235 if (be16_to_cpu(dfp->offset) == off)
236 return dfp;
237 }
238
239
240
241 return NULL;
242}
243
244
245
246
247xfs_dir2_data_free_t *
248xfs_dir2_data_freeinsert(
249 xfs_dir2_data_hdr_t *hdr,
250 xfs_dir2_data_unused_t *dup,
251 int *loghead)
252{
253 xfs_dir2_data_free_t *dfp;
254 xfs_dir2_data_free_t new;
255
256#ifdef __KERNEL__
257 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
258 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
259#endif
260 dfp = hdr->bestfree;
261 new.length = dup->length;
262 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
263
264
265
266
267 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
268 dfp[2] = dfp[1];
269 dfp[1] = dfp[0];
270 dfp[0] = new;
271 *loghead = 1;
272 return &dfp[0];
273 }
274 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
275 dfp[2] = dfp[1];
276 dfp[1] = new;
277 *loghead = 1;
278 return &dfp[1];
279 }
280 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
281 dfp[2] = new;
282 *loghead = 1;
283 return &dfp[2];
284 }
285 return NULL;
286}
287
288
289
290
291STATIC void
292xfs_dir2_data_freeremove(
293 xfs_dir2_data_hdr_t *hdr,
294 xfs_dir2_data_free_t *dfp,
295 int *loghead)
296{
297#ifdef __KERNEL__
298 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
299 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
300#endif
301
302
303
304 if (dfp == &hdr->bestfree[0]) {
305 hdr->bestfree[0] = hdr->bestfree[1];
306 hdr->bestfree[1] = hdr->bestfree[2];
307 }
308
309
310
311 else if (dfp == &hdr->bestfree[1])
312 hdr->bestfree[1] = hdr->bestfree[2];
313
314
315
316 else
317 ASSERT(dfp == &hdr->bestfree[2]);
318
319
320
321 hdr->bestfree[2].length = 0;
322 hdr->bestfree[2].offset = 0;
323 *loghead = 1;
324}
325
326
327
328
329void
330xfs_dir2_data_freescan(
331 xfs_mount_t *mp,
332 xfs_dir2_data_hdr_t *hdr,
333 int *loghead)
334{
335 xfs_dir2_block_tail_t *btp;
336 xfs_dir2_data_entry_t *dep;
337 xfs_dir2_data_unused_t *dup;
338 char *endp;
339 char *p;
340
341#ifdef __KERNEL__
342 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
343 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
344#endif
345
346
347
348 memset(hdr->bestfree, 0, sizeof(hdr->bestfree));
349 *loghead = 1;
350
351
352
353 p = (char *)(hdr + 1);
354 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
355 btp = xfs_dir2_block_tail_p(mp, hdr);
356 endp = (char *)xfs_dir2_block_leaf_p(btp);
357 } else
358 endp = (char *)hdr + mp->m_dirblksize;
359
360
361
362 while (p < endp) {
363 dup = (xfs_dir2_data_unused_t *)p;
364
365
366
367 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
368 ASSERT((char *)dup - (char *)hdr ==
369 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
370 xfs_dir2_data_freeinsert(hdr, dup, loghead);
371 p += be16_to_cpu(dup->length);
372 }
373
374
375
376 else {
377 dep = (xfs_dir2_data_entry_t *)p;
378 ASSERT((char *)dep - (char *)hdr ==
379 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
380 p += xfs_dir2_data_entsize(dep->namelen);
381 }
382 }
383}
384
385
386
387
388
389int
390xfs_dir2_data_init(
391 xfs_da_args_t *args,
392 xfs_dir2_db_t blkno,
393 xfs_dabuf_t **bpp)
394{
395 xfs_dabuf_t *bp;
396 xfs_dir2_data_hdr_t *hdr;
397 xfs_inode_t *dp;
398 xfs_dir2_data_unused_t *dup;
399 int error;
400 int i;
401 xfs_mount_t *mp;
402 xfs_trans_t *tp;
403 int t;
404
405 dp = args->dp;
406 mp = dp->i_mount;
407 tp = args->trans;
408
409
410
411 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
412 XFS_DATA_FORK);
413 if (error) {
414 return error;
415 }
416 ASSERT(bp != NULL);
417
418
419
420
421 hdr = bp->data;
422 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
423 hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr));
424 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
425 hdr->bestfree[i].length = 0;
426 hdr->bestfree[i].offset = 0;
427 }
428
429
430
431
432 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
433 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
434
435 t = mp->m_dirblksize - (uint)sizeof(*hdr);
436 hdr->bestfree[0].length = cpu_to_be16(t);
437 dup->length = cpu_to_be16(t);
438 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
439
440
441
442 xfs_dir2_data_log_header(tp, bp);
443 xfs_dir2_data_log_unused(tp, bp, dup);
444 *bpp = bp;
445 return 0;
446}
447
448
449
450
451void
452xfs_dir2_data_log_entry(
453 xfs_trans_t *tp,
454 xfs_dabuf_t *bp,
455 xfs_dir2_data_entry_t *dep)
456{
457 xfs_dir2_data_hdr_t *hdr = bp->data;
458
459 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
460 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
461
462 xfs_da_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
463 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
464 (char *)hdr - 1));
465}
466
467
468
469
470void
471xfs_dir2_data_log_header(
472 xfs_trans_t *tp,
473 xfs_dabuf_t *bp)
474{
475 xfs_dir2_data_hdr_t *hdr = bp->data;
476
477 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
478 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
479
480 xfs_da_log_buf(tp, bp, 0, sizeof(*hdr) - 1);
481}
482
483
484
485
486void
487xfs_dir2_data_log_unused(
488 xfs_trans_t *tp,
489 xfs_dabuf_t *bp,
490 xfs_dir2_data_unused_t *dup)
491{
492 xfs_dir2_data_hdr_t *hdr = bp->data;
493
494 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
495 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
496
497
498
499
500 xfs_da_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
501 (uint)((char *)&dup->length + sizeof(dup->length) -
502 1 - (char *)hdr));
503
504
505
506 xfs_da_log_buf(tp, bp,
507 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
508 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
509 sizeof(xfs_dir2_data_off_t) - 1));
510}
511
512
513
514
515
516void
517xfs_dir2_data_make_free(
518 xfs_trans_t *tp,
519 xfs_dabuf_t *bp,
520 xfs_dir2_data_aoff_t offset,
521 xfs_dir2_data_aoff_t len,
522 int *needlogp,
523 int *needscanp)
524{
525 xfs_dir2_data_hdr_t *hdr;
526 xfs_dir2_data_free_t *dfp;
527 char *endptr;
528 xfs_mount_t *mp;
529 int needscan;
530 xfs_dir2_data_unused_t *newdup;
531 xfs_dir2_data_unused_t *postdup;
532 xfs_dir2_data_unused_t *prevdup;
533
534 mp = tp->t_mountp;
535 hdr = bp->data;
536
537
538
539
540 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC))
541 endptr = (char *)hdr + mp->m_dirblksize;
542 else {
543 xfs_dir2_block_tail_t *btp;
544
545 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
546 btp = xfs_dir2_block_tail_p(mp, hdr);
547 endptr = (char *)xfs_dir2_block_leaf_p(btp);
548 }
549
550
551
552
553 if (offset > sizeof(*hdr)) {
554 __be16 *tagp;
555
556 tagp = (__be16 *)((char *)hdr + offset) - 1;
557 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
558 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
559 prevdup = NULL;
560 } else
561 prevdup = NULL;
562
563
564
565
566 if ((char *)hdr + offset + len < endptr) {
567 postdup =
568 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
569 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
570 postdup = NULL;
571 } else
572 postdup = NULL;
573 ASSERT(*needscanp == 0);
574 needscan = 0;
575
576
577
578
579 if (prevdup && postdup) {
580 xfs_dir2_data_free_t *dfp2;
581
582
583
584
585 dfp = xfs_dir2_data_freefind(hdr, prevdup);
586 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
587
588
589
590
591
592
593 needscan = (hdr->bestfree[2].length != 0);
594
595
596
597 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
598 *xfs_dir2_data_unused_tag_p(prevdup) =
599 cpu_to_be16((char *)prevdup - (char *)hdr);
600 xfs_dir2_data_log_unused(tp, bp, prevdup);
601 if (!needscan) {
602
603
604
605
606
607
608 ASSERT(dfp && dfp2);
609 if (dfp == &hdr->bestfree[1]) {
610 dfp = &hdr->bestfree[0];
611 ASSERT(dfp2 == dfp);
612 dfp2 = &hdr->bestfree[1];
613 }
614 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
615 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
616
617
618
619 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
620 ASSERT(dfp == &hdr->bestfree[0]);
621 ASSERT(dfp->length == prevdup->length);
622 ASSERT(!dfp[1].length);
623 ASSERT(!dfp[2].length);
624 }
625 }
626
627
628
629 else if (prevdup) {
630 dfp = xfs_dir2_data_freefind(hdr, prevdup);
631 be16_add_cpu(&prevdup->length, len);
632 *xfs_dir2_data_unused_tag_p(prevdup) =
633 cpu_to_be16((char *)prevdup - (char *)hdr);
634 xfs_dir2_data_log_unused(tp, bp, prevdup);
635
636
637
638
639
640 if (dfp) {
641 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
642 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
643 }
644
645
646
647 else {
648 needscan = be16_to_cpu(prevdup->length) >
649 be16_to_cpu(hdr->bestfree[2].length);
650 }
651 }
652
653
654
655 else if (postdup) {
656 dfp = xfs_dir2_data_freefind(hdr, postdup);
657 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
658 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
659 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
660 *xfs_dir2_data_unused_tag_p(newdup) =
661 cpu_to_be16((char *)newdup - (char *)hdr);
662 xfs_dir2_data_log_unused(tp, bp, newdup);
663
664
665
666
667
668 if (dfp) {
669 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
670 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
671 }
672
673
674
675 else {
676 needscan = be16_to_cpu(newdup->length) >
677 be16_to_cpu(hdr->bestfree[2].length);
678 }
679 }
680
681
682
683 else {
684 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
685 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
686 newdup->length = cpu_to_be16(len);
687 *xfs_dir2_data_unused_tag_p(newdup) =
688 cpu_to_be16((char *)newdup - (char *)hdr);
689 xfs_dir2_data_log_unused(tp, bp, newdup);
690 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
691 }
692 *needscanp = needscan;
693}
694
695
696
697
698void
699xfs_dir2_data_use_free(
700 xfs_trans_t *tp,
701 xfs_dabuf_t *bp,
702 xfs_dir2_data_unused_t *dup,
703 xfs_dir2_data_aoff_t offset,
704 xfs_dir2_data_aoff_t len,
705 int *needlogp,
706 int *needscanp)
707{
708 xfs_dir2_data_hdr_t *hdr;
709 xfs_dir2_data_free_t *dfp;
710 int matchback;
711 int matchfront;
712 int needscan;
713 xfs_dir2_data_unused_t *newdup;
714 xfs_dir2_data_unused_t *newdup2;
715 int oldlen;
716
717 hdr = bp->data;
718 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
719 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
720 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
721 ASSERT(offset >= (char *)dup - (char *)hdr);
722 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
723 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
724
725
726
727 dfp = xfs_dir2_data_freefind(hdr, dup);
728 oldlen = be16_to_cpu(dup->length);
729 ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length));
730
731
732
733 matchfront = (char *)dup - (char *)hdr == offset;
734 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
735 ASSERT(*needscanp == 0);
736 needscan = 0;
737
738
739
740
741 if (matchfront && matchback) {
742 if (dfp) {
743 needscan = (hdr->bestfree[2].offset != 0);
744 if (!needscan)
745 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
746 }
747 }
748
749
750
751
752 else if (matchfront) {
753 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
754 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
755 newdup->length = cpu_to_be16(oldlen - len);
756 *xfs_dir2_data_unused_tag_p(newdup) =
757 cpu_to_be16((char *)newdup - (char *)hdr);
758 xfs_dir2_data_log_unused(tp, bp, newdup);
759
760
761
762 if (dfp) {
763 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
764 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
765 ASSERT(dfp != NULL);
766 ASSERT(dfp->length == newdup->length);
767 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
768
769
770
771
772
773 needscan = dfp == &hdr->bestfree[2];
774 }
775 }
776
777
778
779
780 else if (matchback) {
781 newdup = dup;
782 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
783 *xfs_dir2_data_unused_tag_p(newdup) =
784 cpu_to_be16((char *)newdup - (char *)hdr);
785 xfs_dir2_data_log_unused(tp, bp, newdup);
786
787
788
789 if (dfp) {
790 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
791 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
792 ASSERT(dfp != NULL);
793 ASSERT(dfp->length == newdup->length);
794 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
795
796
797
798
799
800 needscan = dfp == &hdr->bestfree[2];
801 }
802 }
803
804
805
806
807 else {
808 newdup = dup;
809 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
810 *xfs_dir2_data_unused_tag_p(newdup) =
811 cpu_to_be16((char *)newdup - (char *)hdr);
812 xfs_dir2_data_log_unused(tp, bp, newdup);
813 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
814 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
815 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
816 *xfs_dir2_data_unused_tag_p(newdup2) =
817 cpu_to_be16((char *)newdup2 - (char *)hdr);
818 xfs_dir2_data_log_unused(tp, bp, newdup2);
819
820
821
822
823
824
825
826
827 if (dfp) {
828 needscan = (hdr->bestfree[2].length != 0);
829 if (!needscan) {
830 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
831 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
832 xfs_dir2_data_freeinsert(hdr, newdup2,
833 needlogp);
834 }
835 }
836 }
837 *needscanp = needscan;
838}
839