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