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