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_dir2.h"
27#include "xfs_mount.h"
28#include "xfs_da_btree.h"
29#include "xfs_bmap_btree.h"
30#include "xfs_dir2_sf.h"
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_bmap.h"
34#include "xfs_dir2_data.h"
35#include "xfs_dir2_leaf.h"
36#include "xfs_dir2_block.h"
37#include "xfs_dir2_node.h"
38#include "xfs_error.h"
39#include "xfs_trace.h"
40
41
42
43
44static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
45static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
46#ifdef DEBUG
47static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
48#else
49#define xfs_dir2_leafn_check(dp, bp)
50#endif
51static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
52 int start_s, xfs_dabuf_t *bp_d, int start_d,
53 int count);
54static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
55 xfs_da_state_blk_t *blk1,
56 xfs_da_state_blk_t *blk2);
57static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
58 int index, xfs_da_state_blk_t *dblk,
59 int *rval);
60static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
61 xfs_da_state_blk_t *fblk);
62
63
64
65
66STATIC void
67xfs_dir2_free_log_bests(
68 xfs_trans_t *tp,
69 xfs_dabuf_t *bp,
70 int first,
71 int last)
72{
73 xfs_dir2_free_t *free;
74
75 free = bp->data;
76 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
77 xfs_da_log_buf(tp, bp,
78 (uint)((char *)&free->bests[first] - (char *)free),
79 (uint)((char *)&free->bests[last] - (char *)free +
80 sizeof(free->bests[0]) - 1));
81}
82
83
84
85
86static void
87xfs_dir2_free_log_header(
88 xfs_trans_t *tp,
89 xfs_dabuf_t *bp)
90{
91 xfs_dir2_free_t *free;
92
93 free = bp->data;
94 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
95 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
96 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
97}
98
99
100
101
102
103
104int
105xfs_dir2_leaf_to_node(
106 xfs_da_args_t *args,
107 xfs_dabuf_t *lbp)
108{
109 xfs_inode_t *dp;
110 int error;
111 xfs_dabuf_t *fbp;
112 xfs_dir2_db_t fdb;
113 xfs_dir2_free_t *free;
114 __be16 *from;
115 int i;
116 xfs_dir2_leaf_t *leaf;
117 xfs_dir2_leaf_tail_t *ltp;
118 xfs_mount_t *mp;
119 int n;
120 xfs_dir2_data_off_t off;
121 __be16 *to;
122 xfs_trans_t *tp;
123
124 trace_xfs_dir2_leaf_to_node(args);
125
126 dp = args->dp;
127 mp = dp->i_mount;
128 tp = args->trans;
129
130
131
132 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
133 return error;
134 }
135 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
136
137
138
139 if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
140 XFS_DATA_FORK))) {
141 return error;
142 }
143 ASSERT(fbp != NULL);
144 free = fbp->data;
145 leaf = lbp->data;
146 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
147
148
149
150 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
151 free->hdr.firstdb = 0;
152 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
153 free->hdr.nvalid = ltp->bestcount;
154
155
156
157
158 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
159 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
160 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
161 n++;
162 *to = cpu_to_be16(off);
163 }
164 free->hdr.nused = cpu_to_be32(n);
165 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
166
167
168
169 xfs_dir2_leaf_log_header(tp, lbp);
170 xfs_dir2_free_log_header(tp, fbp);
171 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
172 xfs_da_buf_done(fbp);
173 xfs_dir2_leafn_check(dp, lbp);
174 return 0;
175}
176
177
178
179
180
181static int
182xfs_dir2_leafn_add(
183 xfs_dabuf_t *bp,
184 xfs_da_args_t *args,
185 int index)
186{
187 int compact;
188 xfs_inode_t *dp;
189 int highstale;
190 xfs_dir2_leaf_t *leaf;
191 xfs_dir2_leaf_entry_t *lep;
192 int lfloghigh;
193 int lfloglow;
194 int lowstale;
195 xfs_mount_t *mp;
196 xfs_trans_t *tp;
197
198 trace_xfs_dir2_leafn_add(args, index);
199
200 dp = args->dp;
201 mp = dp->i_mount;
202 tp = args->trans;
203 leaf = bp->data;
204
205
206
207
208
209 if (index < 0)
210 return XFS_ERROR(EFSCORRUPTED);
211
212
213
214
215
216
217
218
219 if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
220 if (!leaf->hdr.stale)
221 return XFS_ERROR(ENOSPC);
222 compact = be16_to_cpu(leaf->hdr.stale) > 1;
223 } else
224 compact = 0;
225 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
226 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
227 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
228
229 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
230 return 0;
231
232
233
234
235
236 if (compact) {
237 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
238 &lfloglow, &lfloghigh);
239 }
240
241
242
243 else if (leaf->hdr.stale) {
244 lfloglow = be16_to_cpu(leaf->hdr.count);
245 lfloghigh = -1;
246 }
247
248
249
250 if (!leaf->hdr.stale) {
251 lep = &leaf->ents[index];
252 if (index < be16_to_cpu(leaf->hdr.count))
253 memmove(lep + 1, lep,
254 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
255 lfloglow = index;
256 lfloghigh = be16_to_cpu(leaf->hdr.count);
257 be16_add_cpu(&leaf->hdr.count, 1);
258 }
259
260
261
262 else {
263
264
265
266
267 if (compact == 0) {
268
269
270
271 for (lowstale = index - 1;
272 lowstale >= 0 &&
273 be32_to_cpu(leaf->ents[lowstale].address) !=
274 XFS_DIR2_NULL_DATAPTR;
275 lowstale--)
276 continue;
277
278
279
280
281
282 for (highstale = index;
283 highstale < be16_to_cpu(leaf->hdr.count) &&
284 be32_to_cpu(leaf->ents[highstale].address) !=
285 XFS_DIR2_NULL_DATAPTR &&
286 (lowstale < 0 ||
287 index - lowstale - 1 >= highstale - index);
288 highstale++)
289 continue;
290 }
291
292
293
294
295 if (lowstale >= 0 &&
296 (highstale == be16_to_cpu(leaf->hdr.count) ||
297 index - lowstale - 1 < highstale - index)) {
298 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
299 XFS_DIR2_NULL_DATAPTR);
300 ASSERT(index - lowstale - 1 >= 0);
301 if (index - lowstale - 1 > 0)
302 memmove(&leaf->ents[lowstale],
303 &leaf->ents[lowstale + 1],
304 (index - lowstale - 1) * sizeof(*lep));
305 lep = &leaf->ents[index - 1];
306 lfloglow = MIN(lowstale, lfloglow);
307 lfloghigh = MAX(index - 1, lfloghigh);
308 }
309
310
311
312
313 else {
314 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
315 XFS_DIR2_NULL_DATAPTR);
316 ASSERT(highstale - index >= 0);
317 if (highstale - index > 0)
318 memmove(&leaf->ents[index + 1],
319 &leaf->ents[index],
320 (highstale - index) * sizeof(*lep));
321 lep = &leaf->ents[index];
322 lfloglow = MIN(index, lfloglow);
323 lfloghigh = MAX(highstale, lfloghigh);
324 }
325 be16_add_cpu(&leaf->hdr.stale, -1);
326 }
327
328
329
330 lep->hashval = cpu_to_be32(args->hashval);
331 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
332 args->blkno, args->index));
333 xfs_dir2_leaf_log_header(tp, bp);
334 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
335 xfs_dir2_leafn_check(dp, bp);
336 return 0;
337}
338
339#ifdef DEBUG
340
341
342
343void
344xfs_dir2_leafn_check(
345 xfs_inode_t *dp,
346 xfs_dabuf_t *bp)
347{
348 int i;
349 xfs_dir2_leaf_t *leaf;
350 xfs_mount_t *mp;
351 int stale;
352
353 leaf = bp->data;
354 mp = dp->i_mount;
355 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
356 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
357 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
358 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
359 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
360 be32_to_cpu(leaf->ents[i + 1].hashval));
361 }
362 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
363 stale++;
364 }
365 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
366}
367#endif
368
369
370
371
372
373xfs_dahash_t
374xfs_dir2_leafn_lasthash(
375 xfs_dabuf_t *bp,
376 int *count)
377{
378 xfs_dir2_leaf_t *leaf;
379
380 leaf = bp->data;
381 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
382 if (count)
383 *count = be16_to_cpu(leaf->hdr.count);
384 if (!leaf->hdr.count)
385 return 0;
386 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
387}
388
389
390
391
392
393STATIC int
394xfs_dir2_leafn_lookup_for_addname(
395 xfs_dabuf_t *bp,
396 xfs_da_args_t *args,
397 int *indexp,
398 xfs_da_state_t *state)
399{
400 xfs_dabuf_t *curbp = NULL;
401 xfs_dir2_db_t curdb = -1;
402 xfs_dir2_db_t curfdb = -1;
403 xfs_inode_t *dp;
404 int error;
405 int fi;
406 xfs_dir2_free_t *free = NULL;
407 int index;
408 xfs_dir2_leaf_t *leaf;
409 int length;
410 xfs_dir2_leaf_entry_t *lep;
411 xfs_mount_t *mp;
412 xfs_dir2_db_t newdb;
413 xfs_dir2_db_t newfdb;
414 xfs_trans_t *tp;
415
416 dp = args->dp;
417 tp = args->trans;
418 mp = dp->i_mount;
419 leaf = bp->data;
420 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
421#ifdef __KERNEL__
422 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
423#endif
424 xfs_dir2_leafn_check(dp, bp);
425
426
427
428 index = xfs_dir2_leaf_search_hash(args, bp);
429
430
431
432 if (state->extravalid) {
433
434 curbp = state->extrablk.bp;
435 curfdb = state->extrablk.blkno;
436 free = curbp->data;
437 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
438 }
439 length = xfs_dir2_data_entsize(args->namelen);
440
441
442
443 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
444 be32_to_cpu(lep->hashval) == args->hashval;
445 lep++, index++) {
446
447
448
449 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
450 continue;
451
452
453
454 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
455
456
457
458
459
460
461
462
463 if (newdb != curdb) {
464 curdb = newdb;
465
466
467
468
469 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
470
471
472
473 if (newfdb != curfdb) {
474
475
476
477 if (curbp)
478 xfs_da_brelse(tp, curbp);
479
480
481
482 error = xfs_da_read_buf(tp, dp,
483 xfs_dir2_db_to_da(mp, newfdb),
484 -1, &curbp, XFS_DATA_FORK);
485 if (error)
486 return error;
487 free = curbp->data;
488 ASSERT(be32_to_cpu(free->hdr.magic) ==
489 XFS_DIR2_FREE_MAGIC);
490 ASSERT((be32_to_cpu(free->hdr.firstdb) %
491 XFS_DIR2_MAX_FREE_BESTS(mp)) == 0);
492 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
493 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
494 be32_to_cpu(free->hdr.nvalid));
495 }
496
497
498
499 fi = xfs_dir2_db_to_fdindex(mp, curdb);
500
501
502
503 if (unlikely(be16_to_cpu(free->bests[fi]) == NULLDATAOFF)) {
504 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
505 XFS_ERRLEVEL_LOW, mp);
506 if (curfdb != newfdb)
507 xfs_da_brelse(tp, curbp);
508 return XFS_ERROR(EFSCORRUPTED);
509 }
510 curfdb = newfdb;
511 if (be16_to_cpu(free->bests[fi]) >= length)
512 goto out;
513 }
514 }
515
516 fi = -1;
517out:
518 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
519 if (curbp) {
520
521 state->extravalid = 1;
522 state->extrablk.bp = curbp;
523 state->extrablk.index = fi;
524 state->extrablk.blkno = curfdb;
525 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
526 } else {
527 state->extravalid = 0;
528 }
529
530
531
532 *indexp = index;
533 return XFS_ERROR(ENOENT);
534}
535
536
537
538
539
540STATIC int
541xfs_dir2_leafn_lookup_for_entry(
542 xfs_dabuf_t *bp,
543 xfs_da_args_t *args,
544 int *indexp,
545 xfs_da_state_t *state)
546{
547 xfs_dabuf_t *curbp = NULL;
548 xfs_dir2_db_t curdb = -1;
549 xfs_dir2_data_entry_t *dep;
550 xfs_inode_t *dp;
551 int error;
552 int index;
553 xfs_dir2_leaf_t *leaf;
554 xfs_dir2_leaf_entry_t *lep;
555 xfs_mount_t *mp;
556 xfs_dir2_db_t newdb;
557 xfs_trans_t *tp;
558 enum xfs_dacmp cmp;
559
560 dp = args->dp;
561 tp = args->trans;
562 mp = dp->i_mount;
563 leaf = bp->data;
564 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
565#ifdef __KERNEL__
566 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
567#endif
568 xfs_dir2_leafn_check(dp, bp);
569
570
571
572 index = xfs_dir2_leaf_search_hash(args, bp);
573
574
575
576 if (state->extravalid) {
577 curbp = state->extrablk.bp;
578 curdb = state->extrablk.blkno;
579 }
580
581
582
583 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
584 be32_to_cpu(lep->hashval) == args->hashval;
585 lep++, index++) {
586
587
588
589 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
590 continue;
591
592
593
594 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
595
596
597
598
599
600
601 if (newdb != curdb) {
602
603
604
605
606 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
607 curdb != state->extrablk.blkno))
608 xfs_da_brelse(tp, curbp);
609
610
611
612
613 if (args->cmpresult != XFS_CMP_DIFFERENT &&
614 newdb == state->extrablk.blkno) {
615 ASSERT(state->extravalid);
616 curbp = state->extrablk.bp;
617 } else {
618 error = xfs_da_read_buf(tp, dp,
619 xfs_dir2_db_to_da(mp, newdb),
620 -1, &curbp, XFS_DATA_FORK);
621 if (error)
622 return error;
623 }
624 xfs_dir2_data_check(dp, curbp);
625 curdb = newdb;
626 }
627
628
629
630 dep = (xfs_dir2_data_entry_t *)((char *)curbp->data +
631 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
632
633
634
635
636
637 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
638 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
639
640 if (args->cmpresult != XFS_CMP_DIFFERENT &&
641 curdb != state->extrablk.blkno)
642 xfs_da_brelse(tp, state->extrablk.bp);
643 args->cmpresult = cmp;
644 args->inumber = be64_to_cpu(dep->inumber);
645 *indexp = index;
646 state->extravalid = 1;
647 state->extrablk.bp = curbp;
648 state->extrablk.blkno = curdb;
649 state->extrablk.index = (int)((char *)dep -
650 (char *)curbp->data);
651 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
652 if (cmp == XFS_CMP_EXACT)
653 return XFS_ERROR(EEXIST);
654 }
655 }
656 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
657 (args->op_flags & XFS_DA_OP_OKNOENT));
658 if (curbp) {
659 if (args->cmpresult == XFS_CMP_DIFFERENT) {
660
661 state->extravalid = 1;
662 state->extrablk.bp = curbp;
663 state->extrablk.index = -1;
664 state->extrablk.blkno = curdb;
665 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
666 } else {
667
668 if (state->extrablk.bp != curbp)
669 xfs_da_brelse(tp, curbp);
670 }
671 } else {
672 state->extravalid = 0;
673 }
674 *indexp = index;
675 return XFS_ERROR(ENOENT);
676}
677
678
679
680
681
682
683int
684xfs_dir2_leafn_lookup_int(
685 xfs_dabuf_t *bp,
686 xfs_da_args_t *args,
687 int *indexp,
688 xfs_da_state_t *state)
689{
690 if (args->op_flags & XFS_DA_OP_ADDNAME)
691 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
692 state);
693 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
694}
695
696
697
698
699
700static void
701xfs_dir2_leafn_moveents(
702 xfs_da_args_t *args,
703 xfs_dabuf_t *bp_s,
704 int start_s,
705 xfs_dabuf_t *bp_d,
706 int start_d,
707 int count)
708{
709 xfs_dir2_leaf_t *leaf_d;
710 xfs_dir2_leaf_t *leaf_s;
711 int stale;
712 xfs_trans_t *tp;
713
714 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
715
716
717
718
719 if (count == 0) {
720 return;
721 }
722 tp = args->trans;
723 leaf_s = bp_s->data;
724 leaf_d = bp_d->data;
725
726
727
728
729
730 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
731 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
732 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
733 sizeof(xfs_dir2_leaf_entry_t));
734 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
735 count + be16_to_cpu(leaf_d->hdr.count) - 1);
736 }
737
738
739
740
741 if (leaf_s->hdr.stale) {
742 int i;
743
744 for (i = start_s, stale = 0; i < start_s + count; i++) {
745 if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
746 stale++;
747 }
748 } else
749 stale = 0;
750
751
752
753 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
754 count * sizeof(xfs_dir2_leaf_entry_t));
755 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
756
757
758
759
760 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
761 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
762 count * sizeof(xfs_dir2_leaf_entry_t));
763 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
764 }
765
766
767
768 be16_add_cpu(&leaf_s->hdr.count, -(count));
769 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
770 be16_add_cpu(&leaf_d->hdr.count, count);
771 be16_add_cpu(&leaf_d->hdr.stale, stale);
772 xfs_dir2_leaf_log_header(tp, bp_s);
773 xfs_dir2_leaf_log_header(tp, bp_d);
774 xfs_dir2_leafn_check(args->dp, bp_s);
775 xfs_dir2_leafn_check(args->dp, bp_d);
776}
777
778
779
780
781
782int
783xfs_dir2_leafn_order(
784 xfs_dabuf_t *leaf1_bp,
785 xfs_dabuf_t *leaf2_bp)
786{
787 xfs_dir2_leaf_t *leaf1;
788 xfs_dir2_leaf_t *leaf2;
789
790 leaf1 = leaf1_bp->data;
791 leaf2 = leaf2_bp->data;
792 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
793 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
794 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
795 be16_to_cpu(leaf2->hdr.count) > 0 &&
796 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
797 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
798 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
799 return 1;
800 return 0;
801}
802
803
804
805
806
807
808
809
810static void
811xfs_dir2_leafn_rebalance(
812 xfs_da_state_t *state,
813 xfs_da_state_blk_t *blk1,
814 xfs_da_state_blk_t *blk2)
815{
816 xfs_da_args_t *args;
817 int count;
818 int isleft;
819 xfs_dir2_leaf_t *leaf1;
820 xfs_dir2_leaf_t *leaf2;
821 int mid;
822#ifdef DEBUG
823 int oldstale;
824#endif
825 int oldsum;
826 int swap;
827
828 args = state->args;
829
830
831
832 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
833 xfs_da_state_blk_t *tmp;
834
835 tmp = blk1;
836 blk1 = blk2;
837 blk2 = tmp;
838 }
839 leaf1 = blk1->bp->data;
840 leaf2 = blk2->bp->data;
841 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
842#ifdef DEBUG
843 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
844#endif
845 mid = oldsum >> 1;
846
847
848
849
850 if (oldsum & 1) {
851 xfs_dahash_t midhash;
852
853 if (mid >= be16_to_cpu(leaf1->hdr.count))
854 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
855 else
856 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
857 isleft = args->hashval <= midhash;
858 }
859
860
861
862
863
864 else
865 isleft = 1;
866
867
868
869
870 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
871 if (count > 0)
872 xfs_dir2_leafn_moveents(args, blk1->bp,
873 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
874 else if (count < 0)
875 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
876 be16_to_cpu(leaf1->hdr.count), count);
877 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
878 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
879
880
881
882 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
883 state->inleaf = swap;
884 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
885 state->inleaf = !swap;
886 else
887 state->inleaf =
888 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
889
890
891
892 if (!state->inleaf)
893 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
894
895
896
897
898
899 if(blk2->index < 0) {
900 state->inleaf = 1;
901 blk2->index = 0;
902 xfs_alert(args->dp->i_mount,
903 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
904 __func__, blk1->index);
905 }
906}
907
908
909
910
911
912
913static int
914xfs_dir2_leafn_remove(
915 xfs_da_args_t *args,
916 xfs_dabuf_t *bp,
917 int index,
918 xfs_da_state_blk_t *dblk,
919 int *rval)
920{
921 xfs_dir2_data_t *data;
922 xfs_dir2_db_t db;
923 xfs_dabuf_t *dbp;
924 xfs_dir2_data_entry_t *dep;
925 xfs_inode_t *dp;
926 xfs_dir2_leaf_t *leaf;
927 xfs_dir2_leaf_entry_t *lep;
928 int longest;
929 int off;
930 xfs_mount_t *mp;
931 int needlog;
932 int needscan;
933 xfs_trans_t *tp;
934
935 trace_xfs_dir2_leafn_remove(args, index);
936
937 dp = args->dp;
938 tp = args->trans;
939 mp = dp->i_mount;
940 leaf = bp->data;
941 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
942
943
944
945 lep = &leaf->ents[index];
946
947
948
949 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
950 ASSERT(dblk->blkno == db);
951 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
952 ASSERT(dblk->index == off);
953
954
955
956
957 be16_add_cpu(&leaf->hdr.stale, 1);
958 xfs_dir2_leaf_log_header(tp, bp);
959 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
960 xfs_dir2_leaf_log_ents(tp, bp, index, index);
961
962
963
964
965 dbp = dblk->bp;
966 data = dbp->data;
967 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
968 longest = be16_to_cpu(data->hdr.bestfree[0].length);
969 needlog = needscan = 0;
970 xfs_dir2_data_make_free(tp, dbp, off,
971 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
972
973
974
975
976 if (needscan)
977 xfs_dir2_data_freescan(mp, data, &needlog);
978 if (needlog)
979 xfs_dir2_data_log_header(tp, dbp);
980 xfs_dir2_data_check(dp, dbp);
981
982
983
984
985 if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) {
986 int error;
987 xfs_dabuf_t *fbp;
988 xfs_dir2_db_t fdb;
989 int findex;
990 xfs_dir2_free_t *free;
991 int logfree;
992
993
994
995
996
997 fdb = xfs_dir2_db_to_fdb(mp, db);
998 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
999 -1, &fbp, XFS_DATA_FORK))) {
1000 return error;
1001 }
1002 free = fbp->data;
1003 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1004 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
1005 XFS_DIR2_MAX_FREE_BESTS(mp) *
1006 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
1007
1008
1009
1010 findex = xfs_dir2_db_to_fdindex(mp, db);
1011 longest = be16_to_cpu(data->hdr.bestfree[0].length);
1012
1013
1014
1015
1016 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1017
1018
1019
1020 error = xfs_dir2_shrink_inode(args, db, dbp);
1021 if (error == 0) {
1022 dblk->bp = NULL;
1023 data = NULL;
1024 }
1025
1026
1027
1028
1029
1030 else if (error == ENOSPC && args->total == 0)
1031 xfs_da_buf_done(dbp);
1032 else
1033 return error;
1034 }
1035
1036
1037
1038
1039 if (data == NULL) {
1040
1041
1042
1043 be32_add_cpu(&free->hdr.nused, -1);
1044 xfs_dir2_free_log_header(tp, fbp);
1045
1046
1047
1048
1049
1050
1051 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
1052 int i;
1053
1054 for (i = findex - 1;
1055 i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF;
1056 i--)
1057 continue;
1058 free->hdr.nvalid = cpu_to_be32(i + 1);
1059 logfree = 0;
1060 }
1061
1062
1063
1064 else {
1065 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
1066 logfree = 1;
1067 }
1068
1069
1070
1071
1072 if (!free->hdr.nused) {
1073 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1074 if (error == 0) {
1075 fbp = NULL;
1076 logfree = 0;
1077 } else if (error != ENOSPC || args->total != 0)
1078 return error;
1079
1080
1081
1082
1083
1084 }
1085 }
1086
1087
1088
1089
1090 else {
1091 free->bests[findex] = cpu_to_be16(longest);
1092 logfree = 1;
1093 }
1094
1095
1096
1097 if (logfree)
1098 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1099
1100
1101
1102 if (fbp)
1103 xfs_da_buf_done(fbp);
1104 }
1105 xfs_dir2_leafn_check(dp, bp);
1106
1107
1108
1109
1110 *rval =
1111 ((uint)sizeof(leaf->hdr) +
1112 (uint)sizeof(leaf->ents[0]) *
1113 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
1114 mp->m_dir_magicpct;
1115 return 0;
1116}
1117
1118
1119
1120
1121int
1122xfs_dir2_leafn_split(
1123 xfs_da_state_t *state,
1124 xfs_da_state_blk_t *oldblk,
1125 xfs_da_state_blk_t *newblk)
1126{
1127 xfs_da_args_t *args;
1128 xfs_dablk_t blkno;
1129 int error;
1130 xfs_mount_t *mp;
1131
1132
1133
1134
1135 args = state->args;
1136 mp = args->dp->i_mount;
1137 ASSERT(args != NULL);
1138 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1139 error = xfs_da_grow_inode(args, &blkno);
1140 if (error) {
1141 return error;
1142 }
1143
1144
1145
1146 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
1147 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1148 if (error) {
1149 return error;
1150 }
1151 newblk->blkno = blkno;
1152 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1153
1154
1155
1156
1157 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1158 error = xfs_da_blk_link(state, oldblk, newblk);
1159 if (error) {
1160 return error;
1161 }
1162
1163
1164
1165 if (state->inleaf)
1166 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1167 else
1168 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1169
1170
1171
1172 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1173 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1174 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1175 xfs_dir2_leafn_check(args->dp, newblk->bp);
1176 return error;
1177}
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188int
1189xfs_dir2_leafn_toosmall(
1190 xfs_da_state_t *state,
1191 int *action)
1192{
1193 xfs_da_state_blk_t *blk;
1194 xfs_dablk_t blkno;
1195 xfs_dabuf_t *bp;
1196 int bytes;
1197 int count;
1198 int error;
1199 int forward;
1200 int i;
1201 xfs_da_blkinfo_t *info;
1202 xfs_dir2_leaf_t *leaf;
1203 int rval;
1204
1205
1206
1207
1208
1209
1210 blk = &state->path.blk[state->path.active - 1];
1211 info = blk->bp->data;
1212 ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC);
1213 leaf = (xfs_dir2_leaf_t *)info;
1214 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1215 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1216 if (bytes > (state->blocksize >> 1)) {
1217
1218
1219
1220 *action = 0;
1221 return 0;
1222 }
1223
1224
1225
1226
1227
1228
1229 if (count == 0) {
1230
1231
1232
1233
1234 forward = (info->forw != 0);
1235 memcpy(&state->altpath, &state->path, sizeof(state->path));
1236 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1237 &rval);
1238 if (error)
1239 return error;
1240 *action = rval ? 2 : 0;
1241 return 0;
1242 }
1243
1244
1245
1246
1247
1248
1249
1250 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
1251 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1252 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
1253 if (blkno == 0)
1254 continue;
1255
1256
1257
1258 if ((error =
1259 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1260 -1, &bp, XFS_DATA_FORK))) {
1261 return error;
1262 }
1263 ASSERT(bp != NULL);
1264
1265
1266
1267 leaf = (xfs_dir2_leaf_t *)info;
1268 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1269 bytes = state->blocksize - (state->blocksize >> 2);
1270 leaf = bp->data;
1271 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1272 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1273 bytes -= count * (uint)sizeof(leaf->ents[0]);
1274
1275
1276
1277 if (bytes >= 0)
1278 break;
1279 xfs_da_brelse(state->args->trans, bp);
1280 }
1281
1282
1283
1284 if (i >= 2) {
1285 *action = 0;
1286 return 0;
1287 }
1288
1289
1290
1291
1292 xfs_da_buf_done(bp);
1293
1294
1295
1296
1297 memcpy(&state->altpath, &state->path, sizeof(state->path));
1298 if (blkno < blk->blkno)
1299 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1300 &rval);
1301 else
1302 error = xfs_da_path_shift(state, &state->path, forward, 0,
1303 &rval);
1304 if (error) {
1305 return error;
1306 }
1307 *action = rval ? 0 : 1;
1308 return 0;
1309}
1310
1311
1312
1313
1314
1315void
1316xfs_dir2_leafn_unbalance(
1317 xfs_da_state_t *state,
1318 xfs_da_state_blk_t *drop_blk,
1319 xfs_da_state_blk_t *save_blk)
1320{
1321 xfs_da_args_t *args;
1322 xfs_dir2_leaf_t *drop_leaf;
1323 xfs_dir2_leaf_t *save_leaf;
1324
1325 args = state->args;
1326 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1327 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1328 drop_leaf = drop_blk->bp->data;
1329 save_leaf = save_blk->bp->data;
1330 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1331 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1332
1333
1334
1335
1336 if (drop_leaf->hdr.stale)
1337 xfs_dir2_leaf_compact(args, drop_blk->bp);
1338 if (save_leaf->hdr.stale)
1339 xfs_dir2_leaf_compact(args, save_blk->bp);
1340
1341
1342
1343 drop_blk->hashval = be32_to_cpu(drop_leaf->ents[be16_to_cpu(drop_leaf->hdr.count) - 1].hashval);
1344 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1345 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
1346 be16_to_cpu(drop_leaf->hdr.count));
1347 else
1348 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
1349 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
1350 save_blk->hashval = be32_to_cpu(save_leaf->ents[be16_to_cpu(save_leaf->hdr.count) - 1].hashval);
1351 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1352}
1353
1354
1355
1356
1357int
1358xfs_dir2_node_addname(
1359 xfs_da_args_t *args)
1360{
1361 xfs_da_state_blk_t *blk;
1362 int error;
1363 int rval;
1364 xfs_da_state_t *state;
1365
1366 trace_xfs_dir2_node_addname(args);
1367
1368
1369
1370
1371 state = xfs_da_state_alloc();
1372 state->args = args;
1373 state->mp = args->dp->i_mount;
1374 state->blocksize = state->mp->m_dirblksize;
1375 state->node_ents = state->mp->m_dir_node_ents;
1376
1377
1378
1379
1380 error = xfs_da_node_lookup_int(state, &rval);
1381 if (error)
1382 rval = error;
1383 if (rval != ENOENT) {
1384 goto done;
1385 }
1386
1387
1388
1389
1390 rval = xfs_dir2_node_addname_int(args,
1391 state->extravalid ? &state->extrablk : NULL);
1392 if (rval) {
1393 goto done;
1394 }
1395 blk = &state->path.blk[state->path.active - 1];
1396 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1397
1398
1399
1400 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1401 if (rval == 0) {
1402
1403
1404
1405 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
1406 xfs_da_fixhashpath(state, &state->path);
1407 } else {
1408
1409
1410
1411 if (args->total == 0) {
1412 ASSERT(rval == ENOSPC);
1413 goto done;
1414 }
1415
1416
1417
1418 rval = xfs_da_split(state);
1419 }
1420done:
1421 xfs_da_state_free(state);
1422 return rval;
1423}
1424
1425
1426
1427
1428
1429
1430static int
1431xfs_dir2_node_addname_int(
1432 xfs_da_args_t *args,
1433 xfs_da_state_blk_t *fblk)
1434{
1435 xfs_dir2_data_t *data;
1436 xfs_dir2_db_t dbno;
1437 xfs_dabuf_t *dbp;
1438 xfs_dir2_data_entry_t *dep;
1439 xfs_inode_t *dp;
1440 xfs_dir2_data_unused_t *dup;
1441 int error;
1442 xfs_dir2_db_t fbno;
1443 xfs_dabuf_t *fbp;
1444 int findex;
1445 xfs_dir2_free_t *free=NULL;
1446 xfs_dir2_db_t ifbno;
1447 xfs_dir2_db_t lastfbno=0;
1448 int length;
1449 int logfree;
1450 xfs_mount_t *mp;
1451 int needlog;
1452 int needscan;
1453 __be16 *tagp;
1454 xfs_trans_t *tp;
1455
1456 dp = args->dp;
1457 mp = dp->i_mount;
1458 tp = args->trans;
1459 length = xfs_dir2_data_entsize(args->namelen);
1460
1461
1462
1463
1464
1465 if (fblk) {
1466 fbp = fblk->bp;
1467
1468
1469
1470 ifbno = fblk->blkno;
1471 free = fbp->data;
1472 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1473 findex = fblk->index;
1474
1475
1476
1477
1478
1479 if (findex >= 0) {
1480 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1481 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1482 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1483 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
1484 }
1485
1486
1487
1488
1489 else {
1490 dbno = -1;
1491 findex = 0;
1492 }
1493 }
1494
1495
1496
1497 else {
1498 ifbno = dbno = -1;
1499 fbp = NULL;
1500 findex = 0;
1501 }
1502
1503
1504
1505
1506
1507 if (dbno == -1) {
1508 xfs_fileoff_t fo;
1509
1510 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1511 return error;
1512 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
1513 fbno = ifbno;
1514 }
1515
1516
1517
1518
1519
1520 while (dbno == -1) {
1521
1522
1523
1524 if (fbp == NULL) {
1525
1526
1527
1528
1529 if (++fbno == 0)
1530 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1531
1532
1533
1534 if (fbno == ifbno)
1535 fbno++;
1536
1537
1538
1539 if (fbno >= lastfbno)
1540 break;
1541
1542
1543
1544
1545
1546
1547 if ((error = xfs_da_read_buf(tp, dp,
1548 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
1549 XFS_DATA_FORK))) {
1550 return error;
1551 }
1552 if (unlikely(fbp == NULL)) {
1553 continue;
1554 }
1555 free = fbp->data;
1556 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1557 findex = 0;
1558 }
1559
1560
1561
1562 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1563 be16_to_cpu(free->bests[findex]) >= length)
1564 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
1565 else {
1566
1567
1568
1569 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
1570
1571
1572
1573 xfs_da_brelse(tp, fbp);
1574 fbp = NULL;
1575 if (fblk && fblk->bp)
1576 fblk->bp = NULL;
1577 }
1578 }
1579 }
1580
1581
1582
1583
1584 if (unlikely(dbno == -1)) {
1585
1586
1587
1588 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
1589 args->total == 0) {
1590
1591
1592
1593
1594 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1595 xfs_da_buf_done(fbp);
1596 return XFS_ERROR(ENOSPC);
1597 }
1598
1599
1600
1601 if (unlikely((error = xfs_dir2_grow_inode(args,
1602 XFS_DIR2_DATA_SPACE,
1603 &dbno)) ||
1604 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1605
1606
1607
1608
1609 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1610 xfs_da_buf_done(fbp);
1611 return error;
1612 }
1613
1614
1615
1616 if (fbp)
1617 xfs_da_brelse(tp, fbp);
1618 if (fblk && fblk->bp)
1619 fblk->bp = NULL;
1620
1621
1622
1623
1624
1625 fbno = xfs_dir2_db_to_fdb(mp, dbno);
1626 if (unlikely(error = xfs_da_read_buf(tp, dp,
1627 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
1628 XFS_DATA_FORK))) {
1629 xfs_da_buf_done(dbp);
1630 return error;
1631 }
1632
1633
1634
1635
1636 if( fbp == NULL ) {
1637 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1638 &fbno))) {
1639 return error;
1640 }
1641
1642 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
1643 xfs_alert(mp,
1644 "%s: dir ino " "%llu needed freesp block %lld for\n"
1645 " data block %lld, got %lld ifbno %llu lastfbno %d",
1646 __func__, (unsigned long long)dp->i_ino,
1647 (long long)xfs_dir2_db_to_fdb(mp, dbno),
1648 (long long)dbno, (long long)fbno,
1649 (unsigned long long)ifbno, lastfbno);
1650 if (fblk) {
1651 xfs_alert(mp,
1652 " fblk 0x%p blkno %llu index %d magic 0x%x",
1653 fblk,
1654 (unsigned long long)fblk->blkno,
1655 fblk->index,
1656 fblk->magic);
1657 } else {
1658 xfs_alert(mp, " ... fblk is NULL");
1659 }
1660 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1661 XFS_ERRLEVEL_LOW, mp);
1662 return XFS_ERROR(EFSCORRUPTED);
1663 }
1664
1665
1666
1667
1668 if ((error = xfs_da_get_buf(tp, dp,
1669 xfs_dir2_db_to_da(mp, fbno),
1670 -1, &fbp, XFS_DATA_FORK))) {
1671 return error;
1672 }
1673 ASSERT(fbp != NULL);
1674
1675
1676
1677
1678
1679 free = fbp->data;
1680 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1681 free->hdr.firstdb = cpu_to_be32(
1682 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1683 XFS_DIR2_MAX_FREE_BESTS(mp));
1684 free->hdr.nvalid = 0;
1685 free->hdr.nused = 0;
1686 } else {
1687 free = fbp->data;
1688 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1689 }
1690
1691
1692
1693
1694 findex = xfs_dir2_db_to_fdindex(mp, dbno);
1695
1696
1697
1698
1699 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
1700 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
1701 free->hdr.nvalid = cpu_to_be32(findex + 1);
1702
1703
1704
1705 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
1706 }
1707
1708
1709
1710
1711 if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) {
1712 be32_add_cpu(&free->hdr.nused, 1);
1713 xfs_dir2_free_log_header(tp, fbp);
1714 }
1715
1716
1717
1718
1719
1720 data = dbp->data;
1721 free->bests[findex] = data->hdr.bestfree[0].length;
1722 logfree = 1;
1723 }
1724
1725
1726
1727 else {
1728
1729
1730
1731 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1732 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1733 xfs_da_buf_done(fbp);
1734 return 0;
1735 }
1736
1737
1738
1739 if (unlikely(
1740 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
1741 -1, &dbp, XFS_DATA_FORK))) {
1742 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1743 xfs_da_buf_done(fbp);
1744 return error;
1745 }
1746 data = dbp->data;
1747 logfree = 0;
1748 }
1749 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length);
1750
1751
1752
1753 dup = (xfs_dir2_data_unused_t *)
1754 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
1755 needscan = needlog = 0;
1756
1757
1758
1759 xfs_dir2_data_use_free(tp, dbp, dup,
1760 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1761 &needlog, &needscan);
1762
1763
1764
1765 dep = (xfs_dir2_data_entry_t *)dup;
1766 dep->inumber = cpu_to_be64(args->inumber);
1767 dep->namelen = args->namelen;
1768 memcpy(dep->name, args->name, dep->namelen);
1769 tagp = xfs_dir2_data_entry_tag_p(dep);
1770 *tagp = cpu_to_be16((char *)dep - (char *)data);
1771 xfs_dir2_data_log_entry(tp, dbp, dep);
1772
1773
1774
1775 if (needscan)
1776 xfs_dir2_data_freescan(mp, data, &needlog);
1777
1778
1779
1780 if (needlog)
1781 xfs_dir2_data_log_header(tp, dbp);
1782
1783
1784
1785 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
1786 free->bests[findex] = data->hdr.bestfree[0].length;
1787 logfree = 1;
1788 }
1789
1790
1791
1792 if (logfree)
1793 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1794
1795
1796
1797 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1798 xfs_da_buf_done(fbp);
1799
1800
1801
1802 args->blkno = (xfs_dablk_t)dbno;
1803 args->index = be16_to_cpu(*tagp);
1804 xfs_da_buf_done(dbp);
1805 return 0;
1806}
1807
1808
1809
1810
1811
1812
1813int
1814xfs_dir2_node_lookup(
1815 xfs_da_args_t *args)
1816{
1817 int error;
1818 int i;
1819 int rval;
1820 xfs_da_state_t *state;
1821
1822 trace_xfs_dir2_node_lookup(args);
1823
1824
1825
1826
1827 state = xfs_da_state_alloc();
1828 state->args = args;
1829 state->mp = args->dp->i_mount;
1830 state->blocksize = state->mp->m_dirblksize;
1831 state->node_ents = state->mp->m_dir_node_ents;
1832
1833
1834
1835 error = xfs_da_node_lookup_int(state, &rval);
1836 if (error)
1837 rval = error;
1838 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1839
1840 xfs_dir2_data_entry_t *dep;
1841
1842 dep = (xfs_dir2_data_entry_t *)((char *)state->extrablk.bp->
1843 data + state->extrablk.index);
1844 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1845 }
1846
1847
1848
1849 for (i = 0; i < state->path.active; i++) {
1850 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1851 state->path.blk[i].bp = NULL;
1852 }
1853
1854
1855
1856 if (state->extravalid && state->extrablk.bp) {
1857 xfs_da_brelse(args->trans, state->extrablk.bp);
1858 state->extrablk.bp = NULL;
1859 }
1860 xfs_da_state_free(state);
1861 return rval;
1862}
1863
1864
1865
1866
1867int
1868xfs_dir2_node_removename(
1869 xfs_da_args_t *args)
1870{
1871 xfs_da_state_blk_t *blk;
1872 int error;
1873 int rval;
1874 xfs_da_state_t *state;
1875
1876 trace_xfs_dir2_node_removename(args);
1877
1878
1879
1880
1881 state = xfs_da_state_alloc();
1882 state->args = args;
1883 state->mp = args->dp->i_mount;
1884 state->blocksize = state->mp->m_dirblksize;
1885 state->node_ents = state->mp->m_dir_node_ents;
1886
1887
1888
1889 error = xfs_da_node_lookup_int(state, &rval);
1890 if (error)
1891 rval = error;
1892
1893
1894
1895 if (rval != EEXIST) {
1896 xfs_da_state_free(state);
1897 return rval;
1898 }
1899 blk = &state->path.blk[state->path.active - 1];
1900 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1901 ASSERT(state->extravalid);
1902
1903
1904
1905
1906 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1907 &state->extrablk, &rval);
1908 if (error)
1909 return error;
1910
1911
1912
1913 xfs_da_fixhashpath(state, &state->path);
1914
1915
1916
1917 if (rval && state->path.active > 1)
1918 error = xfs_da_join(state);
1919
1920
1921
1922 if (!error)
1923 error = xfs_dir2_node_to_leaf(state);
1924 xfs_da_state_free(state);
1925 return error;
1926}
1927
1928
1929
1930
1931int
1932xfs_dir2_node_replace(
1933 xfs_da_args_t *args)
1934{
1935 xfs_da_state_blk_t *blk;
1936 xfs_dir2_data_t *data;
1937 xfs_dir2_data_entry_t *dep;
1938 int error;
1939 int i;
1940 xfs_ino_t inum;
1941 xfs_dir2_leaf_t *leaf;
1942 xfs_dir2_leaf_entry_t *lep;
1943 int rval;
1944 xfs_da_state_t *state;
1945
1946 trace_xfs_dir2_node_replace(args);
1947
1948
1949
1950
1951 state = xfs_da_state_alloc();
1952 state->args = args;
1953 state->mp = args->dp->i_mount;
1954 state->blocksize = state->mp->m_dirblksize;
1955 state->node_ents = state->mp->m_dir_node_ents;
1956 inum = args->inumber;
1957
1958
1959
1960 error = xfs_da_node_lookup_int(state, &rval);
1961 if (error) {
1962 rval = error;
1963 }
1964
1965
1966
1967
1968 if (rval == EEXIST) {
1969
1970
1971
1972 blk = &state->path.blk[state->path.active - 1];
1973 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1974 leaf = blk->bp->data;
1975 lep = &leaf->ents[blk->index];
1976 ASSERT(state->extravalid);
1977
1978
1979
1980 data = state->extrablk.bp->data;
1981 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
1982 dep = (xfs_dir2_data_entry_t *)
1983 ((char *)data +
1984 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
1985 ASSERT(inum != be64_to_cpu(dep->inumber));
1986
1987
1988
1989 dep->inumber = cpu_to_be64(inum);
1990 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1991 rval = 0;
1992 }
1993
1994
1995
1996 else if (state->extravalid) {
1997 xfs_da_brelse(args->trans, state->extrablk.bp);
1998 state->extrablk.bp = NULL;
1999 }
2000
2001
2002
2003 for (i = 0; i < state->path.active; i++) {
2004 xfs_da_brelse(args->trans, state->path.blk[i].bp);
2005 state->path.blk[i].bp = NULL;
2006 }
2007 xfs_da_state_free(state);
2008 return rval;
2009}
2010
2011
2012
2013
2014
2015int
2016xfs_dir2_node_trim_free(
2017 xfs_da_args_t *args,
2018 xfs_fileoff_t fo,
2019 int *rvalp)
2020{
2021 xfs_dabuf_t *bp;
2022 xfs_inode_t *dp;
2023 int error;
2024 xfs_dir2_free_t *free;
2025 xfs_mount_t *mp;
2026 xfs_trans_t *tp;
2027
2028 dp = args->dp;
2029 mp = dp->i_mount;
2030 tp = args->trans;
2031
2032
2033
2034 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
2035 XFS_DATA_FORK))) {
2036 return error;
2037 }
2038
2039
2040
2041
2042
2043 if (bp == NULL) {
2044 return 0;
2045 }
2046 free = bp->data;
2047 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
2048
2049
2050
2051 if (be32_to_cpu(free->hdr.nused) > 0) {
2052 xfs_da_brelse(tp, bp);
2053 *rvalp = 0;
2054 return 0;
2055 }
2056
2057
2058
2059 if ((error =
2060 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
2061 bp))) {
2062
2063
2064
2065
2066
2067 ASSERT(error != ENOSPC);
2068 xfs_da_brelse(tp, bp);
2069 return error;
2070 }
2071
2072
2073
2074 *rvalp = 1;
2075 return 0;
2076}
2077