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