]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_dir2_leaf.c
libxfs: refactor manage_zones()
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_leaf.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
5 * All Rights Reserved.
6 */
7 #include "libxfs_priv.h"
8 #include "xfs_fs.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_da_format.h"
14 #include "xfs_da_btree.h"
15 #include "xfs_inode.h"
16 #include "xfs_bmap.h"
17 #include "xfs_dir2.h"
18 #include "xfs_dir2_priv.h"
19 #include "xfs_trace.h"
20 #include "xfs_trans.h"
21 #include "xfs_cksum.h"
22
23 /*
24 * Local function declarations.
25 */
26 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
27 int *indexp, struct xfs_buf **dbpp);
28 static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args,
29 struct xfs_buf *bp, int first, int last);
30 static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args,
31 struct xfs_buf *bp);
32
33 /*
34 * Check the internal consistency of a leaf1 block.
35 * Pop an assert if something is wrong.
36 */
37 #ifdef DEBUG
38 static xfs_failaddr_t
39 xfs_dir3_leaf1_check(
40 struct xfs_inode *dp,
41 struct xfs_buf *bp)
42 {
43 struct xfs_dir2_leaf *leaf = bp->b_addr;
44 struct xfs_dir3_icleaf_hdr leafhdr;
45
46 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
47
48 if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
49 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
50 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
51 return __this_address;
52 } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
53 return __this_address;
54
55 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
56 }
57
58 static inline void
59 xfs_dir3_leaf_check(
60 struct xfs_inode *dp,
61 struct xfs_buf *bp)
62 {
63 xfs_failaddr_t fa;
64
65 fa = xfs_dir3_leaf1_check(dp, bp);
66 if (!fa)
67 return;
68 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
69 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
70 fa);
71 ASSERT(0);
72 }
73 #else
74 #define xfs_dir3_leaf_check(dp, bp)
75 #endif
76
77 xfs_failaddr_t
78 xfs_dir3_leaf_check_int(
79 struct xfs_mount *mp,
80 struct xfs_inode *dp,
81 struct xfs_dir3_icleaf_hdr *hdr,
82 struct xfs_dir2_leaf *leaf)
83 {
84 struct xfs_dir2_leaf_entry *ents;
85 xfs_dir2_leaf_tail_t *ltp;
86 int stale;
87 int i;
88 const struct xfs_dir_ops *ops;
89 struct xfs_dir3_icleaf_hdr leafhdr;
90 struct xfs_da_geometry *geo = mp->m_dir_geo;
91
92 /*
93 * we can be passed a null dp here from a verifier, so we need to go the
94 * hard way to get them.
95 */
96 ops = xfs_dir_get_ops(mp, dp);
97
98 if (!hdr) {
99 ops->leaf_hdr_from_disk(&leafhdr, leaf);
100 hdr = &leafhdr;
101 }
102
103 ents = ops->leaf_ents_p(leaf);
104 ltp = xfs_dir2_leaf_tail_p(geo, leaf);
105
106 /*
107 * XXX (dgc): This value is not restrictive enough.
108 * Should factor in the size of the bests table as well.
109 * We can deduce a value for that from di_size.
110 */
111 if (hdr->count > ops->leaf_max_ents(geo))
112 return __this_address;
113
114 /* Leaves and bests don't overlap in leaf format. */
115 if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
116 hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
117 (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
118 return __this_address;
119
120 /* Check hash value order, count stale entries. */
121 for (i = stale = 0; i < hdr->count; i++) {
122 if (i + 1 < hdr->count) {
123 if (be32_to_cpu(ents[i].hashval) >
124 be32_to_cpu(ents[i + 1].hashval))
125 return __this_address;
126 }
127 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
128 stale++;
129 }
130 if (hdr->stale != stale)
131 return __this_address;
132 return NULL;
133 }
134
135 /*
136 * We verify the magic numbers before decoding the leaf header so that on debug
137 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
138 * to incorrect magic numbers.
139 */
140 static xfs_failaddr_t
141 xfs_dir3_leaf_verify(
142 struct xfs_buf *bp,
143 uint16_t magic)
144 {
145 struct xfs_mount *mp = bp->b_target->bt_mount;
146 struct xfs_dir2_leaf *leaf = bp->b_addr;
147
148 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
149
150 if (xfs_sb_version_hascrc(&mp->m_sb)) {
151 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
152 uint16_t magic3;
153
154 magic3 = (magic == XFS_DIR2_LEAF1_MAGIC) ? XFS_DIR3_LEAF1_MAGIC
155 : XFS_DIR3_LEAFN_MAGIC;
156
157 if (leaf3->info.hdr.magic != cpu_to_be16(magic3))
158 return __this_address;
159 if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid))
160 return __this_address;
161 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
162 return __this_address;
163 if (!xfs_log_check_lsn(mp, be64_to_cpu(leaf3->info.lsn)))
164 return __this_address;
165 } else {
166 if (leaf->hdr.info.magic != cpu_to_be16(magic))
167 return __this_address;
168 }
169
170 return xfs_dir3_leaf_check_int(mp, NULL, NULL, leaf);
171 }
172
173 static void
174 __read_verify(
175 struct xfs_buf *bp,
176 uint16_t magic)
177 {
178 struct xfs_mount *mp = bp->b_target->bt_mount;
179 xfs_failaddr_t fa;
180
181 if (xfs_sb_version_hascrc(&mp->m_sb) &&
182 !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
183 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
184 else {
185 fa = xfs_dir3_leaf_verify(bp, magic);
186 if (fa)
187 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
188 }
189 }
190
191 static void
192 __write_verify(
193 struct xfs_buf *bp,
194 uint16_t magic)
195 {
196 struct xfs_mount *mp = bp->b_target->bt_mount;
197 struct xfs_buf_log_item *bip = bp->b_log_item;
198 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
199 xfs_failaddr_t fa;
200
201 fa = xfs_dir3_leaf_verify(bp, magic);
202 if (fa) {
203 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
204 return;
205 }
206
207 if (!xfs_sb_version_hascrc(&mp->m_sb))
208 return;
209
210 if (bip)
211 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
212
213 xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF);
214 }
215
216 static xfs_failaddr_t
217 xfs_dir3_leaf1_verify(
218 struct xfs_buf *bp)
219 {
220 return xfs_dir3_leaf_verify(bp, XFS_DIR2_LEAF1_MAGIC);
221 }
222
223 static void
224 xfs_dir3_leaf1_read_verify(
225 struct xfs_buf *bp)
226 {
227 __read_verify(bp, XFS_DIR2_LEAF1_MAGIC);
228 }
229
230 static void
231 xfs_dir3_leaf1_write_verify(
232 struct xfs_buf *bp)
233 {
234 __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
235 }
236
237 static xfs_failaddr_t
238 xfs_dir3_leafn_verify(
239 struct xfs_buf *bp)
240 {
241 return xfs_dir3_leaf_verify(bp, XFS_DIR2_LEAFN_MAGIC);
242 }
243
244 static void
245 xfs_dir3_leafn_read_verify(
246 struct xfs_buf *bp)
247 {
248 __read_verify(bp, XFS_DIR2_LEAFN_MAGIC);
249 }
250
251 static void
252 xfs_dir3_leafn_write_verify(
253 struct xfs_buf *bp)
254 {
255 __write_verify(bp, XFS_DIR2_LEAFN_MAGIC);
256 }
257
258 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
259 .name = "xfs_dir3_leaf1",
260 .verify_read = xfs_dir3_leaf1_read_verify,
261 .verify_write = xfs_dir3_leaf1_write_verify,
262 .verify_struct = xfs_dir3_leaf1_verify,
263 };
264
265 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
266 .name = "xfs_dir3_leafn",
267 .verify_read = xfs_dir3_leafn_read_verify,
268 .verify_write = xfs_dir3_leafn_write_verify,
269 .verify_struct = xfs_dir3_leafn_verify,
270 };
271
272 int
273 xfs_dir3_leaf_read(
274 struct xfs_trans *tp,
275 struct xfs_inode *dp,
276 xfs_dablk_t fbno,
277 xfs_daddr_t mappedbno,
278 struct xfs_buf **bpp)
279 {
280 int err;
281
282 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
283 XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
284 if (!err && tp && *bpp)
285 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
286 return err;
287 }
288
289 int
290 xfs_dir3_leafn_read(
291 struct xfs_trans *tp,
292 struct xfs_inode *dp,
293 xfs_dablk_t fbno,
294 xfs_daddr_t mappedbno,
295 struct xfs_buf **bpp)
296 {
297 int err;
298
299 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
300 XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
301 if (!err && tp && *bpp)
302 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
303 return err;
304 }
305
306 /*
307 * Initialize a new leaf block, leaf1 or leafn magic accepted.
308 */
309 static void
310 xfs_dir3_leaf_init(
311 struct xfs_mount *mp,
312 struct xfs_trans *tp,
313 struct xfs_buf *bp,
314 xfs_ino_t owner,
315 uint16_t type)
316 {
317 struct xfs_dir2_leaf *leaf = bp->b_addr;
318
319 ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
320
321 if (xfs_sb_version_hascrc(&mp->m_sb)) {
322 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
323
324 memset(leaf3, 0, sizeof(*leaf3));
325
326 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
327 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
328 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
329 leaf3->info.blkno = cpu_to_be64(bp->b_bn);
330 leaf3->info.owner = cpu_to_be64(owner);
331 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid);
332 } else {
333 memset(leaf, 0, sizeof(*leaf));
334 leaf->hdr.info.magic = cpu_to_be16(type);
335 }
336
337 /*
338 * If it's a leaf-format directory initialize the tail.
339 * Caller is responsible for initialising the bests table.
340 */
341 if (type == XFS_DIR2_LEAF1_MAGIC) {
342 struct xfs_dir2_leaf_tail *ltp;
343
344 ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
345 ltp->bestcount = 0;
346 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
347 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
348 } else {
349 bp->b_ops = &xfs_dir3_leafn_buf_ops;
350 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
351 }
352 }
353
354 int
355 xfs_dir3_leaf_get_buf(
356 xfs_da_args_t *args,
357 xfs_dir2_db_t bno,
358 struct xfs_buf **bpp,
359 uint16_t magic)
360 {
361 struct xfs_inode *dp = args->dp;
362 struct xfs_trans *tp = args->trans;
363 struct xfs_mount *mp = dp->i_mount;
364 struct xfs_buf *bp;
365 int error;
366
367 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
368 ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) &&
369 bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
370
371 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
372 -1, &bp, XFS_DATA_FORK);
373 if (error)
374 return error;
375
376 xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
377 xfs_dir3_leaf_log_header(args, bp);
378 if (magic == XFS_DIR2_LEAF1_MAGIC)
379 xfs_dir3_leaf_log_tail(args, bp);
380 *bpp = bp;
381 return 0;
382 }
383
384 /*
385 * Convert a block form directory to a leaf form directory.
386 */
387 int /* error */
388 xfs_dir2_block_to_leaf(
389 xfs_da_args_t *args, /* operation arguments */
390 struct xfs_buf *dbp) /* input block's buffer */
391 {
392 __be16 *bestsp; /* leaf's bestsp entries */
393 xfs_dablk_t blkno; /* leaf block's bno */
394 xfs_dir2_data_hdr_t *hdr; /* block header */
395 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
396 xfs_dir2_block_tail_t *btp; /* block's tail */
397 xfs_inode_t *dp; /* incore directory inode */
398 int error; /* error return code */
399 struct xfs_buf *lbp; /* leaf block's buffer */
400 xfs_dir2_db_t ldb; /* leaf block's bno */
401 xfs_dir2_leaf_t *leaf; /* leaf structure */
402 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
403 int needlog; /* need to log block header */
404 int needscan; /* need to rescan bestfree */
405 xfs_trans_t *tp; /* transaction pointer */
406 struct xfs_dir2_data_free *bf;
407 struct xfs_dir2_leaf_entry *ents;
408 struct xfs_dir3_icleaf_hdr leafhdr;
409
410 trace_xfs_dir2_block_to_leaf(args);
411
412 dp = args->dp;
413 tp = args->trans;
414 /*
415 * Add the leaf block to the inode.
416 * This interface will only put blocks in the leaf/node range.
417 * Since that's empty now, we'll get the root (block 0 in range).
418 */
419 if ((error = xfs_da_grow_inode(args, &blkno))) {
420 return error;
421 }
422 ldb = xfs_dir2_da_to_db(args->geo, blkno);
423 ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET));
424 /*
425 * Initialize the leaf block, get a buffer for it.
426 */
427 error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
428 if (error)
429 return error;
430
431 leaf = lbp->b_addr;
432 hdr = dbp->b_addr;
433 xfs_dir3_data_check(dp, dbp);
434 btp = xfs_dir2_block_tail_p(args->geo, hdr);
435 blp = xfs_dir2_block_leaf_p(btp);
436 bf = dp->d_ops->data_bestfree_p(hdr);
437 ents = dp->d_ops->leaf_ents_p(leaf);
438
439 /*
440 * Set the counts in the leaf header.
441 */
442 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
443 leafhdr.count = be32_to_cpu(btp->count);
444 leafhdr.stale = be32_to_cpu(btp->stale);
445 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
446 xfs_dir3_leaf_log_header(args, lbp);
447
448 /*
449 * Could compact these but I think we always do the conversion
450 * after squeezing out stale entries.
451 */
452 memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
453 xfs_dir3_leaf_log_ents(args, lbp, 0, leafhdr.count - 1);
454 needscan = 0;
455 needlog = 1;
456 /*
457 * Make the space formerly occupied by the leaf entries and block
458 * tail be free.
459 */
460 xfs_dir2_data_make_free(args, dbp,
461 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
462 (xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize -
463 (char *)blp),
464 &needlog, &needscan);
465 /*
466 * Fix up the block header, make it a data block.
467 */
468 dbp->b_ops = &xfs_dir3_data_buf_ops;
469 xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
470 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
471 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
472 else
473 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
474
475 if (needscan)
476 xfs_dir2_data_freescan(dp, hdr, &needlog);
477 /*
478 * Set up leaf tail and bests table.
479 */
480 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
481 ltp->bestcount = cpu_to_be32(1);
482 bestsp = xfs_dir2_leaf_bests_p(ltp);
483 bestsp[0] = bf[0].length;
484 /*
485 * Log the data header and leaf bests table.
486 */
487 if (needlog)
488 xfs_dir2_data_log_header(args, dbp);
489 xfs_dir3_leaf_check(dp, lbp);
490 xfs_dir3_data_check(dp, dbp);
491 xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
492 return 0;
493 }
494
495 STATIC void
496 xfs_dir3_leaf_find_stale(
497 struct xfs_dir3_icleaf_hdr *leafhdr,
498 struct xfs_dir2_leaf_entry *ents,
499 int index,
500 int *lowstale,
501 int *highstale)
502 {
503 /*
504 * Find the first stale entry before our index, if any.
505 */
506 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
507 if (ents[*lowstale].address ==
508 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
509 break;
510 }
511
512 /*
513 * Find the first stale entry at or after our index, if any.
514 * Stop if the result would require moving more entries than using
515 * lowstale.
516 */
517 for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
518 if (ents[*highstale].address ==
519 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
520 break;
521 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
522 break;
523 }
524 }
525
526 struct xfs_dir2_leaf_entry *
527 xfs_dir3_leaf_find_entry(
528 struct xfs_dir3_icleaf_hdr *leafhdr,
529 struct xfs_dir2_leaf_entry *ents,
530 int index, /* leaf table position */
531 int compact, /* need to compact leaves */
532 int lowstale, /* index of prev stale leaf */
533 int highstale, /* index of next stale leaf */
534 int *lfloglow, /* low leaf logging index */
535 int *lfloghigh) /* high leaf logging index */
536 {
537 if (!leafhdr->stale) {
538 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
539
540 /*
541 * Now we need to make room to insert the leaf entry.
542 *
543 * If there are no stale entries, just insert a hole at index.
544 */
545 lep = &ents[index];
546 if (index < leafhdr->count)
547 memmove(lep + 1, lep,
548 (leafhdr->count - index) * sizeof(*lep));
549
550 /*
551 * Record low and high logging indices for the leaf.
552 */
553 *lfloglow = index;
554 *lfloghigh = leafhdr->count++;
555 return lep;
556 }
557
558 /*
559 * There are stale entries.
560 *
561 * We will use one of them for the new entry. It's probably not at
562 * the right location, so we'll have to shift some up or down first.
563 *
564 * If we didn't compact before, we need to find the nearest stale
565 * entries before and after our insertion point.
566 */
567 if (compact == 0)
568 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
569 &lowstale, &highstale);
570
571 /*
572 * If the low one is better, use it.
573 */
574 if (lowstale >= 0 &&
575 (highstale == leafhdr->count ||
576 index - lowstale - 1 < highstale - index)) {
577 ASSERT(index - lowstale - 1 >= 0);
578 ASSERT(ents[lowstale].address ==
579 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
580
581 /*
582 * Copy entries up to cover the stale entry and make room
583 * for the new entry.
584 */
585 if (index - lowstale - 1 > 0) {
586 memmove(&ents[lowstale], &ents[lowstale + 1],
587 (index - lowstale - 1) *
588 sizeof(xfs_dir2_leaf_entry_t));
589 }
590 *lfloglow = min(lowstale, *lfloglow);
591 *lfloghigh = max(index - 1, *lfloghigh);
592 leafhdr->stale--;
593 return &ents[index - 1];
594 }
595
596 /*
597 * The high one is better, so use that one.
598 */
599 ASSERT(highstale - index >= 0);
600 ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
601
602 /*
603 * Copy entries down to cover the stale entry and make room for the
604 * new entry.
605 */
606 if (highstale - index > 0) {
607 memmove(&ents[index + 1], &ents[index],
608 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
609 }
610 *lfloglow = min(index, *lfloglow);
611 *lfloghigh = max(highstale, *lfloghigh);
612 leafhdr->stale--;
613 return &ents[index];
614 }
615
616 /*
617 * Add an entry to a leaf form directory.
618 */
619 int /* error */
620 xfs_dir2_leaf_addname(
621 xfs_da_args_t *args) /* operation arguments */
622 {
623 __be16 *bestsp; /* freespace table in leaf */
624 int compact; /* need to compact leaves */
625 xfs_dir2_data_hdr_t *hdr; /* data block header */
626 struct xfs_buf *dbp; /* data block buffer */
627 xfs_dir2_data_entry_t *dep; /* data block entry */
628 xfs_inode_t *dp; /* incore directory inode */
629 xfs_dir2_data_unused_t *dup; /* data unused entry */
630 int error; /* error return value */
631 int grown; /* allocated new data block */
632 int highstale; /* index of next stale leaf */
633 int i; /* temporary, index */
634 int index; /* leaf table position */
635 struct xfs_buf *lbp; /* leaf's buffer */
636 xfs_dir2_leaf_t *leaf; /* leaf structure */
637 int length; /* length of new entry */
638 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
639 int lfloglow; /* low leaf logging index */
640 int lfloghigh; /* high leaf logging index */
641 int lowstale; /* index of prev stale leaf */
642 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
643 int needbytes; /* leaf block bytes needed */
644 int needlog; /* need to log data header */
645 int needscan; /* need to rescan data free */
646 __be16 *tagp; /* end of data entry */
647 xfs_trans_t *tp; /* transaction pointer */
648 xfs_dir2_db_t use_block; /* data block number */
649 struct xfs_dir2_data_free *bf; /* bestfree table */
650 struct xfs_dir2_leaf_entry *ents;
651 struct xfs_dir3_icleaf_hdr leafhdr;
652
653 trace_xfs_dir2_leaf_addname(args);
654
655 dp = args->dp;
656 tp = args->trans;
657
658 error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp);
659 if (error)
660 return error;
661
662 /*
663 * Look up the entry by hash value and name.
664 * We know it's not there, our caller has already done a lookup.
665 * So the index is of the entry to insert in front of.
666 * But if there are dup hash values the index is of the first of those.
667 */
668 index = xfs_dir2_leaf_search_hash(args, lbp);
669 leaf = lbp->b_addr;
670 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
671 ents = dp->d_ops->leaf_ents_p(leaf);
672 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
673 bestsp = xfs_dir2_leaf_bests_p(ltp);
674 length = dp->d_ops->data_entsize(args->namelen);
675
676 /*
677 * See if there are any entries with the same hash value
678 * and space in their block for the new entry.
679 * This is good because it puts multiple same-hash value entries
680 * in a data block, improving the lookup of those entries.
681 */
682 for (use_block = -1, lep = &ents[index];
683 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
684 index++, lep++) {
685 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
686 continue;
687 i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
688 ASSERT(i < be32_to_cpu(ltp->bestcount));
689 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
690 if (be16_to_cpu(bestsp[i]) >= length) {
691 use_block = i;
692 break;
693 }
694 }
695 /*
696 * Didn't find a block yet, linear search all the data blocks.
697 */
698 if (use_block == -1) {
699 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
700 /*
701 * Remember a block we see that's missing.
702 */
703 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
704 use_block == -1)
705 use_block = i;
706 else if (be16_to_cpu(bestsp[i]) >= length) {
707 use_block = i;
708 break;
709 }
710 }
711 }
712 /*
713 * How many bytes do we need in the leaf block?
714 */
715 needbytes = 0;
716 if (!leafhdr.stale)
717 needbytes += sizeof(xfs_dir2_leaf_entry_t);
718 if (use_block == -1)
719 needbytes += sizeof(xfs_dir2_data_off_t);
720
721 /*
722 * Now kill use_block if it refers to a missing block, so we
723 * can use it as an indication of allocation needed.
724 */
725 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
726 use_block = -1;
727 /*
728 * If we don't have enough free bytes but we can make enough
729 * by compacting out stale entries, we'll do that.
730 */
731 if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
732 leafhdr.stale > 1)
733 compact = 1;
734
735 /*
736 * Otherwise if we don't have enough free bytes we need to
737 * convert to node form.
738 */
739 else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
740 /*
741 * Just checking or no space reservation, give up.
742 */
743 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
744 args->total == 0) {
745 xfs_trans_brelse(tp, lbp);
746 return -ENOSPC;
747 }
748 /*
749 * Convert to node form.
750 */
751 error = xfs_dir2_leaf_to_node(args, lbp);
752 if (error)
753 return error;
754 /*
755 * Then add the new entry.
756 */
757 return xfs_dir2_node_addname(args);
758 }
759 /*
760 * Otherwise it will fit without compaction.
761 */
762 else
763 compact = 0;
764 /*
765 * If just checking, then it will fit unless we needed to allocate
766 * a new data block.
767 */
768 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
769 xfs_trans_brelse(tp, lbp);
770 return use_block == -1 ? -ENOSPC : 0;
771 }
772 /*
773 * If no allocations are allowed, return now before we've
774 * changed anything.
775 */
776 if (args->total == 0 && use_block == -1) {
777 xfs_trans_brelse(tp, lbp);
778 return -ENOSPC;
779 }
780 /*
781 * Need to compact the leaf entries, removing stale ones.
782 * Leave one stale entry behind - the one closest to our
783 * insertion index - and we'll shift that one to our insertion
784 * point later.
785 */
786 if (compact) {
787 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
788 &highstale, &lfloglow, &lfloghigh);
789 }
790 /*
791 * There are stale entries, so we'll need log-low and log-high
792 * impossibly bad values later.
793 */
794 else if (leafhdr.stale) {
795 lfloglow = leafhdr.count;
796 lfloghigh = -1;
797 }
798 /*
799 * If there was no data block space found, we need to allocate
800 * a new one.
801 */
802 if (use_block == -1) {
803 /*
804 * Add the new data block.
805 */
806 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
807 &use_block))) {
808 xfs_trans_brelse(tp, lbp);
809 return error;
810 }
811 /*
812 * Initialize the block.
813 */
814 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
815 xfs_trans_brelse(tp, lbp);
816 return error;
817 }
818 /*
819 * If we're adding a new data block on the end we need to
820 * extend the bests table. Copy it up one entry.
821 */
822 if (use_block >= be32_to_cpu(ltp->bestcount)) {
823 bestsp--;
824 memmove(&bestsp[0], &bestsp[1],
825 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
826 be32_add_cpu(&ltp->bestcount, 1);
827 xfs_dir3_leaf_log_tail(args, lbp);
828 xfs_dir3_leaf_log_bests(args, lbp, 0,
829 be32_to_cpu(ltp->bestcount) - 1);
830 }
831 /*
832 * If we're filling in a previously empty block just log it.
833 */
834 else
835 xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
836 hdr = dbp->b_addr;
837 bf = dp->d_ops->data_bestfree_p(hdr);
838 bestsp[use_block] = bf[0].length;
839 grown = 1;
840 } else {
841 /*
842 * Already had space in some data block.
843 * Just read that one in.
844 */
845 error = xfs_dir3_data_read(tp, dp,
846 xfs_dir2_db_to_da(args->geo, use_block),
847 -1, &dbp);
848 if (error) {
849 xfs_trans_brelse(tp, lbp);
850 return error;
851 }
852 hdr = dbp->b_addr;
853 bf = dp->d_ops->data_bestfree_p(hdr);
854 grown = 0;
855 }
856 /*
857 * Point to the biggest freespace in our data block.
858 */
859 dup = (xfs_dir2_data_unused_t *)
860 ((char *)hdr + be16_to_cpu(bf[0].offset));
861 needscan = needlog = 0;
862 /*
863 * Mark the initial part of our freespace in use for the new entry.
864 */
865 error = xfs_dir2_data_use_free(args, dbp, dup,
866 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
867 length, &needlog, &needscan);
868 if (error) {
869 xfs_trans_brelse(tp, lbp);
870 return error;
871 }
872 /*
873 * Initialize our new entry (at last).
874 */
875 dep = (xfs_dir2_data_entry_t *)dup;
876 dep->inumber = cpu_to_be64(args->inumber);
877 dep->namelen = args->namelen;
878 memcpy(dep->name, args->name, dep->namelen);
879 dp->d_ops->data_put_ftype(dep, args->filetype);
880 tagp = dp->d_ops->data_entry_tag_p(dep);
881 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
882 /*
883 * Need to scan fix up the bestfree table.
884 */
885 if (needscan)
886 xfs_dir2_data_freescan(dp, hdr, &needlog);
887 /*
888 * Need to log the data block's header.
889 */
890 if (needlog)
891 xfs_dir2_data_log_header(args, dbp);
892 xfs_dir2_data_log_entry(args, dbp, dep);
893 /*
894 * If the bests table needs to be changed, do it.
895 * Log the change unless we've already done that.
896 */
897 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
898 bestsp[use_block] = bf[0].length;
899 if (!grown)
900 xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
901 }
902
903 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
904 highstale, &lfloglow, &lfloghigh);
905
906 /*
907 * Fill in the new leaf entry.
908 */
909 lep->hashval = cpu_to_be32(args->hashval);
910 lep->address = cpu_to_be32(
911 xfs_dir2_db_off_to_dataptr(args->geo, use_block,
912 be16_to_cpu(*tagp)));
913 /*
914 * Log the leaf fields and give up the buffers.
915 */
916 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
917 xfs_dir3_leaf_log_header(args, lbp);
918 xfs_dir3_leaf_log_ents(args, lbp, lfloglow, lfloghigh);
919 xfs_dir3_leaf_check(dp, lbp);
920 xfs_dir3_data_check(dp, dbp);
921 return 0;
922 }
923
924 /*
925 * Compact out any stale entries in the leaf.
926 * Log the header and changed leaf entries, if any.
927 */
928 void
929 xfs_dir3_leaf_compact(
930 xfs_da_args_t *args, /* operation arguments */
931 struct xfs_dir3_icleaf_hdr *leafhdr,
932 struct xfs_buf *bp) /* leaf buffer */
933 {
934 int from; /* source leaf index */
935 xfs_dir2_leaf_t *leaf; /* leaf structure */
936 int loglow; /* first leaf entry to log */
937 int to; /* target leaf index */
938 struct xfs_dir2_leaf_entry *ents;
939 struct xfs_inode *dp = args->dp;
940
941 leaf = bp->b_addr;
942 if (!leafhdr->stale)
943 return;
944
945 /*
946 * Compress out the stale entries in place.
947 */
948 ents = dp->d_ops->leaf_ents_p(leaf);
949 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
950 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
951 continue;
952 /*
953 * Only actually copy the entries that are different.
954 */
955 if (from > to) {
956 if (loglow == -1)
957 loglow = to;
958 ents[to] = ents[from];
959 }
960 to++;
961 }
962 /*
963 * Update and log the header, log the leaf entries.
964 */
965 ASSERT(leafhdr->stale == from - to);
966 leafhdr->count -= leafhdr->stale;
967 leafhdr->stale = 0;
968
969 dp->d_ops->leaf_hdr_to_disk(leaf, leafhdr);
970 xfs_dir3_leaf_log_header(args, bp);
971 if (loglow != -1)
972 xfs_dir3_leaf_log_ents(args, bp, loglow, to - 1);
973 }
974
975 /*
976 * Compact the leaf entries, removing stale ones.
977 * Leave one stale entry behind - the one closest to our
978 * insertion index - and the caller will shift that one to our insertion
979 * point later.
980 * Return new insertion index, where the remaining stale entry is,
981 * and leaf logging indices.
982 */
983 void
984 xfs_dir3_leaf_compact_x1(
985 struct xfs_dir3_icleaf_hdr *leafhdr,
986 struct xfs_dir2_leaf_entry *ents,
987 int *indexp, /* insertion index */
988 int *lowstalep, /* out: stale entry before us */
989 int *highstalep, /* out: stale entry after us */
990 int *lowlogp, /* out: low log index */
991 int *highlogp) /* out: high log index */
992 {
993 int from; /* source copy index */
994 int highstale; /* stale entry at/after index */
995 int index; /* insertion index */
996 int keepstale; /* source index of kept stale */
997 int lowstale; /* stale entry before index */
998 int newindex=0; /* new insertion index */
999 int to; /* destination copy index */
1000
1001 ASSERT(leafhdr->stale > 1);
1002 index = *indexp;
1003
1004 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
1005
1006 /*
1007 * Pick the better of lowstale and highstale.
1008 */
1009 if (lowstale >= 0 &&
1010 (highstale == leafhdr->count ||
1011 index - lowstale <= highstale - index))
1012 keepstale = lowstale;
1013 else
1014 keepstale = highstale;
1015 /*
1016 * Copy the entries in place, removing all the stale entries
1017 * except keepstale.
1018 */
1019 for (from = to = 0; from < leafhdr->count; from++) {
1020 /*
1021 * Notice the new value of index.
1022 */
1023 if (index == from)
1024 newindex = to;
1025 if (from != keepstale &&
1026 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
1027 if (from == to)
1028 *lowlogp = to;
1029 continue;
1030 }
1031 /*
1032 * Record the new keepstale value for the insertion.
1033 */
1034 if (from == keepstale)
1035 lowstale = highstale = to;
1036 /*
1037 * Copy only the entries that have moved.
1038 */
1039 if (from > to)
1040 ents[to] = ents[from];
1041 to++;
1042 }
1043 ASSERT(from > to);
1044 /*
1045 * If the insertion point was past the last entry,
1046 * set the new insertion point accordingly.
1047 */
1048 if (index == from)
1049 newindex = to;
1050 *indexp = newindex;
1051 /*
1052 * Adjust the leaf header values.
1053 */
1054 leafhdr->count -= from - to;
1055 leafhdr->stale = 1;
1056 /*
1057 * Remember the low/high stale value only in the "right"
1058 * direction.
1059 */
1060 if (lowstale >= newindex)
1061 lowstale = -1;
1062 else
1063 highstale = leafhdr->count;
1064 *highlogp = leafhdr->count - 1;
1065 *lowstalep = lowstale;
1066 *highstalep = highstale;
1067 }
1068
1069 /*
1070 * Log the bests entries indicated from a leaf1 block.
1071 */
1072 static void
1073 xfs_dir3_leaf_log_bests(
1074 struct xfs_da_args *args,
1075 struct xfs_buf *bp, /* leaf buffer */
1076 int first, /* first entry to log */
1077 int last) /* last entry to log */
1078 {
1079 __be16 *firstb; /* pointer to first entry */
1080 __be16 *lastb; /* pointer to last entry */
1081 struct xfs_dir2_leaf *leaf = bp->b_addr;
1082 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1083
1084 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1085 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1086
1087 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1088 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1089 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1090 xfs_trans_log_buf(args->trans, bp,
1091 (uint)((char *)firstb - (char *)leaf),
1092 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1093 }
1094
1095 /*
1096 * Log the leaf entries indicated from a leaf1 or leafn block.
1097 */
1098 void
1099 xfs_dir3_leaf_log_ents(
1100 struct xfs_da_args *args,
1101 struct xfs_buf *bp,
1102 int first,
1103 int last)
1104 {
1105 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1106 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1107 struct xfs_dir2_leaf *leaf = bp->b_addr;
1108 struct xfs_dir2_leaf_entry *ents;
1109
1110 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1111 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1112 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1113 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1114
1115 ents = args->dp->d_ops->leaf_ents_p(leaf);
1116 firstlep = &ents[first];
1117 lastlep = &ents[last];
1118 xfs_trans_log_buf(args->trans, bp,
1119 (uint)((char *)firstlep - (char *)leaf),
1120 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1121 }
1122
1123 /*
1124 * Log the header of the leaf1 or leafn block.
1125 */
1126 void
1127 xfs_dir3_leaf_log_header(
1128 struct xfs_da_args *args,
1129 struct xfs_buf *bp)
1130 {
1131 struct xfs_dir2_leaf *leaf = bp->b_addr;
1132
1133 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1134 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1135 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1136 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1137
1138 xfs_trans_log_buf(args->trans, bp,
1139 (uint)((char *)&leaf->hdr - (char *)leaf),
1140 args->dp->d_ops->leaf_hdr_size - 1);
1141 }
1142
1143 /*
1144 * Log the tail of the leaf1 block.
1145 */
1146 STATIC void
1147 xfs_dir3_leaf_log_tail(
1148 struct xfs_da_args *args,
1149 struct xfs_buf *bp)
1150 {
1151 struct xfs_dir2_leaf *leaf = bp->b_addr;
1152 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1153
1154 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1155 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1156 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1157 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1158
1159 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1160 xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
1161 (uint)(args->geo->blksize - 1));
1162 }
1163
1164 /*
1165 * Look up the entry referred to by args in the leaf format directory.
1166 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1167 * is also used by the node-format code.
1168 */
1169 int
1170 xfs_dir2_leaf_lookup(
1171 xfs_da_args_t *args) /* operation arguments */
1172 {
1173 struct xfs_buf *dbp; /* data block buffer */
1174 xfs_dir2_data_entry_t *dep; /* data block entry */
1175 xfs_inode_t *dp; /* incore directory inode */
1176 int error; /* error return code */
1177 int index; /* found entry index */
1178 struct xfs_buf *lbp; /* leaf buffer */
1179 xfs_dir2_leaf_t *leaf; /* leaf structure */
1180 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1181 xfs_trans_t *tp; /* transaction pointer */
1182 struct xfs_dir2_leaf_entry *ents;
1183
1184 trace_xfs_dir2_leaf_lookup(args);
1185
1186 /*
1187 * Look up name in the leaf block, returning both buffers and index.
1188 */
1189 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1190 return error;
1191 }
1192 tp = args->trans;
1193 dp = args->dp;
1194 xfs_dir3_leaf_check(dp, lbp);
1195 leaf = lbp->b_addr;
1196 ents = dp->d_ops->leaf_ents_p(leaf);
1197 /*
1198 * Get to the leaf entry and contained data entry address.
1199 */
1200 lep = &ents[index];
1201
1202 /*
1203 * Point to the data entry.
1204 */
1205 dep = (xfs_dir2_data_entry_t *)
1206 ((char *)dbp->b_addr +
1207 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1208 /*
1209 * Return the found inode number & CI name if appropriate
1210 */
1211 args->inumber = be64_to_cpu(dep->inumber);
1212 args->filetype = dp->d_ops->data_get_ftype(dep);
1213 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1214 xfs_trans_brelse(tp, dbp);
1215 xfs_trans_brelse(tp, lbp);
1216 return error;
1217 }
1218
1219 /*
1220 * Look up name/hash in the leaf block.
1221 * Fill in indexp with the found index, and dbpp with the data buffer.
1222 * If not found dbpp will be NULL, and ENOENT comes back.
1223 * lbpp will always be filled in with the leaf buffer unless there's an error.
1224 */
1225 static int /* error */
1226 xfs_dir2_leaf_lookup_int(
1227 xfs_da_args_t *args, /* operation arguments */
1228 struct xfs_buf **lbpp, /* out: leaf buffer */
1229 int *indexp, /* out: index in leaf block */
1230 struct xfs_buf **dbpp) /* out: data buffer */
1231 {
1232 xfs_dir2_db_t curdb = -1; /* current data block number */
1233 struct xfs_buf *dbp = NULL; /* data buffer */
1234 xfs_dir2_data_entry_t *dep; /* data entry */
1235 xfs_inode_t *dp; /* incore directory inode */
1236 int error; /* error return code */
1237 int index; /* index in leaf block */
1238 struct xfs_buf *lbp; /* leaf buffer */
1239 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1240 xfs_dir2_leaf_t *leaf; /* leaf structure */
1241 xfs_mount_t *mp; /* filesystem mount point */
1242 xfs_dir2_db_t newdb; /* new data block number */
1243 xfs_trans_t *tp; /* transaction pointer */
1244 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1245 enum xfs_dacmp cmp; /* name compare result */
1246 struct xfs_dir2_leaf_entry *ents;
1247 struct xfs_dir3_icleaf_hdr leafhdr;
1248
1249 dp = args->dp;
1250 tp = args->trans;
1251 mp = dp->i_mount;
1252
1253 error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp);
1254 if (error)
1255 return error;
1256
1257 *lbpp = lbp;
1258 leaf = lbp->b_addr;
1259 xfs_dir3_leaf_check(dp, lbp);
1260 ents = dp->d_ops->leaf_ents_p(leaf);
1261 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1262
1263 /*
1264 * Look for the first leaf entry with our hash value.
1265 */
1266 index = xfs_dir2_leaf_search_hash(args, lbp);
1267 /*
1268 * Loop over all the entries with the right hash value
1269 * looking to match the name.
1270 */
1271 for (lep = &ents[index];
1272 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1273 lep++, index++) {
1274 /*
1275 * Skip over stale leaf entries.
1276 */
1277 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1278 continue;
1279 /*
1280 * Get the new data block number.
1281 */
1282 newdb = xfs_dir2_dataptr_to_db(args->geo,
1283 be32_to_cpu(lep->address));
1284 /*
1285 * If it's not the same as the old data block number,
1286 * need to pitch the old one and read the new one.
1287 */
1288 if (newdb != curdb) {
1289 if (dbp)
1290 xfs_trans_brelse(tp, dbp);
1291 error = xfs_dir3_data_read(tp, dp,
1292 xfs_dir2_db_to_da(args->geo, newdb),
1293 -1, &dbp);
1294 if (error) {
1295 xfs_trans_brelse(tp, lbp);
1296 return error;
1297 }
1298 curdb = newdb;
1299 }
1300 /*
1301 * Point to the data entry.
1302 */
1303 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1304 xfs_dir2_dataptr_to_off(args->geo,
1305 be32_to_cpu(lep->address)));
1306 /*
1307 * Compare name and if it's an exact match, return the index
1308 * and buffer. If it's the first case-insensitive match, store
1309 * the index and buffer and continue looking for an exact match.
1310 */
1311 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1312 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1313 args->cmpresult = cmp;
1314 *indexp = index;
1315 /* case exact match: return the current buffer. */
1316 if (cmp == XFS_CMP_EXACT) {
1317 *dbpp = dbp;
1318 return 0;
1319 }
1320 cidb = curdb;
1321 }
1322 }
1323 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1324 /*
1325 * Here, we can only be doing a lookup (not a rename or remove).
1326 * If a case-insensitive match was found earlier, re-read the
1327 * appropriate data block if required and return it.
1328 */
1329 if (args->cmpresult == XFS_CMP_CASE) {
1330 ASSERT(cidb != -1);
1331 if (cidb != curdb) {
1332 xfs_trans_brelse(tp, dbp);
1333 error = xfs_dir3_data_read(tp, dp,
1334 xfs_dir2_db_to_da(args->geo, cidb),
1335 -1, &dbp);
1336 if (error) {
1337 xfs_trans_brelse(tp, lbp);
1338 return error;
1339 }
1340 }
1341 *dbpp = dbp;
1342 return 0;
1343 }
1344 /*
1345 * No match found, return -ENOENT.
1346 */
1347 ASSERT(cidb == -1);
1348 if (dbp)
1349 xfs_trans_brelse(tp, dbp);
1350 xfs_trans_brelse(tp, lbp);
1351 return -ENOENT;
1352 }
1353
1354 /*
1355 * Remove an entry from a leaf format directory.
1356 */
1357 int /* error */
1358 xfs_dir2_leaf_removename(
1359 xfs_da_args_t *args) /* operation arguments */
1360 {
1361 __be16 *bestsp; /* leaf block best freespace */
1362 xfs_dir2_data_hdr_t *hdr; /* data block header */
1363 xfs_dir2_db_t db; /* data block number */
1364 struct xfs_buf *dbp; /* data block buffer */
1365 xfs_dir2_data_entry_t *dep; /* data entry structure */
1366 xfs_inode_t *dp; /* incore directory inode */
1367 int error; /* error return code */
1368 xfs_dir2_db_t i; /* temporary data block # */
1369 int index; /* index into leaf entries */
1370 struct xfs_buf *lbp; /* leaf buffer */
1371 xfs_dir2_leaf_t *leaf; /* leaf structure */
1372 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1373 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1374 int needlog; /* need to log data header */
1375 int needscan; /* need to rescan data frees */
1376 xfs_dir2_data_off_t oldbest; /* old value of best free */
1377 struct xfs_dir2_data_free *bf; /* bestfree table */
1378 struct xfs_dir2_leaf_entry *ents;
1379 struct xfs_dir3_icleaf_hdr leafhdr;
1380
1381 trace_xfs_dir2_leaf_removename(args);
1382
1383 /*
1384 * Lookup the leaf entry, get the leaf and data blocks read in.
1385 */
1386 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1387 return error;
1388 }
1389 dp = args->dp;
1390 leaf = lbp->b_addr;
1391 hdr = dbp->b_addr;
1392 xfs_dir3_data_check(dp, dbp);
1393 bf = dp->d_ops->data_bestfree_p(hdr);
1394 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1395 ents = dp->d_ops->leaf_ents_p(leaf);
1396 /*
1397 * Point to the leaf entry, use that to point to the data entry.
1398 */
1399 lep = &ents[index];
1400 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
1401 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
1402 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1403 needscan = needlog = 0;
1404 oldbest = be16_to_cpu(bf[0].length);
1405 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1406 bestsp = xfs_dir2_leaf_bests_p(ltp);
1407 if (be16_to_cpu(bestsp[db]) != oldbest)
1408 return -EFSCORRUPTED;
1409 /*
1410 * Mark the former data entry unused.
1411 */
1412 xfs_dir2_data_make_free(args, dbp,
1413 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1414 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
1415 /*
1416 * We just mark the leaf entry stale by putting a null in it.
1417 */
1418 leafhdr.stale++;
1419 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1420 xfs_dir3_leaf_log_header(args, lbp);
1421
1422 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1423 xfs_dir3_leaf_log_ents(args, lbp, index, index);
1424
1425 /*
1426 * Scan the freespace in the data block again if necessary,
1427 * log the data block header if necessary.
1428 */
1429 if (needscan)
1430 xfs_dir2_data_freescan(dp, hdr, &needlog);
1431 if (needlog)
1432 xfs_dir2_data_log_header(args, dbp);
1433 /*
1434 * If the longest freespace in the data block has changed,
1435 * put the new value in the bests table and log that.
1436 */
1437 if (be16_to_cpu(bf[0].length) != oldbest) {
1438 bestsp[db] = bf[0].length;
1439 xfs_dir3_leaf_log_bests(args, lbp, db, db);
1440 }
1441 xfs_dir3_data_check(dp, dbp);
1442 /*
1443 * If the data block is now empty then get rid of the data block.
1444 */
1445 if (be16_to_cpu(bf[0].length) ==
1446 args->geo->blksize - dp->d_ops->data_entry_offset) {
1447 ASSERT(db != args->geo->datablk);
1448 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1449 /*
1450 * Nope, can't get rid of it because it caused
1451 * allocation of a bmap btree block to do so.
1452 * Just go on, returning success, leaving the
1453 * empty block in place.
1454 */
1455 if (error == -ENOSPC && args->total == 0)
1456 error = 0;
1457 xfs_dir3_leaf_check(dp, lbp);
1458 return error;
1459 }
1460 dbp = NULL;
1461 /*
1462 * If this is the last data block then compact the
1463 * bests table by getting rid of entries.
1464 */
1465 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1466 /*
1467 * Look for the last active entry (i).
1468 */
1469 for (i = db - 1; i > 0; i--) {
1470 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1471 break;
1472 }
1473 /*
1474 * Copy the table down so inactive entries at the
1475 * end are removed.
1476 */
1477 memmove(&bestsp[db - i], bestsp,
1478 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1479 be32_add_cpu(&ltp->bestcount, -(db - i));
1480 xfs_dir3_leaf_log_tail(args, lbp);
1481 xfs_dir3_leaf_log_bests(args, lbp, 0,
1482 be32_to_cpu(ltp->bestcount) - 1);
1483 } else
1484 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1485 }
1486 /*
1487 * If the data block was not the first one, drop it.
1488 */
1489 else if (db != args->geo->datablk)
1490 dbp = NULL;
1491
1492 xfs_dir3_leaf_check(dp, lbp);
1493 /*
1494 * See if we can convert to block form.
1495 */
1496 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1497 }
1498
1499 /*
1500 * Replace the inode number in a leaf format directory entry.
1501 */
1502 int /* error */
1503 xfs_dir2_leaf_replace(
1504 xfs_da_args_t *args) /* operation arguments */
1505 {
1506 struct xfs_buf *dbp; /* data block buffer */
1507 xfs_dir2_data_entry_t *dep; /* data block entry */
1508 xfs_inode_t *dp; /* incore directory inode */
1509 int error; /* error return code */
1510 int index; /* index of leaf entry */
1511 struct xfs_buf *lbp; /* leaf buffer */
1512 xfs_dir2_leaf_t *leaf; /* leaf structure */
1513 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1514 xfs_trans_t *tp; /* transaction pointer */
1515 struct xfs_dir2_leaf_entry *ents;
1516
1517 trace_xfs_dir2_leaf_replace(args);
1518
1519 /*
1520 * Look up the entry.
1521 */
1522 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1523 return error;
1524 }
1525 dp = args->dp;
1526 leaf = lbp->b_addr;
1527 ents = dp->d_ops->leaf_ents_p(leaf);
1528 /*
1529 * Point to the leaf entry, get data address from it.
1530 */
1531 lep = &ents[index];
1532 /*
1533 * Point to the data entry.
1534 */
1535 dep = (xfs_dir2_data_entry_t *)
1536 ((char *)dbp->b_addr +
1537 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1538 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1539 /*
1540 * Put the new inode number in, log it.
1541 */
1542 dep->inumber = cpu_to_be64(args->inumber);
1543 dp->d_ops->data_put_ftype(dep, args->filetype);
1544 tp = args->trans;
1545 xfs_dir2_data_log_entry(args, dbp, dep);
1546 xfs_dir3_leaf_check(dp, lbp);
1547 xfs_trans_brelse(tp, lbp);
1548 return 0;
1549 }
1550
1551 /*
1552 * Return index in the leaf block (lbp) which is either the first
1553 * one with this hash value, or if there are none, the insert point
1554 * for that hash value.
1555 */
1556 int /* index value */
1557 xfs_dir2_leaf_search_hash(
1558 xfs_da_args_t *args, /* operation arguments */
1559 struct xfs_buf *lbp) /* leaf buffer */
1560 {
1561 xfs_dahash_t hash=0; /* hash from this entry */
1562 xfs_dahash_t hashwant; /* hash value looking for */
1563 int high; /* high leaf index */
1564 int low; /* low leaf index */
1565 xfs_dir2_leaf_t *leaf; /* leaf structure */
1566 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1567 int mid=0; /* current leaf index */
1568 struct xfs_dir2_leaf_entry *ents;
1569 struct xfs_dir3_icleaf_hdr leafhdr;
1570
1571 leaf = lbp->b_addr;
1572 ents = args->dp->d_ops->leaf_ents_p(leaf);
1573 args->dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1574
1575 /*
1576 * Note, the table cannot be empty, so we have to go through the loop.
1577 * Binary search the leaf entries looking for our hash value.
1578 */
1579 for (lep = ents, low = 0, high = leafhdr.count - 1,
1580 hashwant = args->hashval;
1581 low <= high; ) {
1582 mid = (low + high) >> 1;
1583 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1584 break;
1585 if (hash < hashwant)
1586 low = mid + 1;
1587 else
1588 high = mid - 1;
1589 }
1590 /*
1591 * Found one, back up through all the equal hash values.
1592 */
1593 if (hash == hashwant) {
1594 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1595 mid--;
1596 }
1597 }
1598 /*
1599 * Need to point to an entry higher than ours.
1600 */
1601 else if (hash < hashwant)
1602 mid++;
1603 return mid;
1604 }
1605
1606 /*
1607 * Trim off a trailing data block. We know it's empty since the leaf
1608 * freespace table says so.
1609 */
1610 int /* error */
1611 xfs_dir2_leaf_trim_data(
1612 xfs_da_args_t *args, /* operation arguments */
1613 struct xfs_buf *lbp, /* leaf buffer */
1614 xfs_dir2_db_t db) /* data block number */
1615 {
1616 __be16 *bestsp; /* leaf bests table */
1617 struct xfs_buf *dbp; /* data block buffer */
1618 xfs_inode_t *dp; /* incore directory inode */
1619 int error; /* error return value */
1620 xfs_dir2_leaf_t *leaf; /* leaf structure */
1621 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1622 xfs_trans_t *tp; /* transaction pointer */
1623
1624 dp = args->dp;
1625 tp = args->trans;
1626 /*
1627 * Read the offending data block. We need its buffer.
1628 */
1629 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(args->geo, db),
1630 -1, &dbp);
1631 if (error)
1632 return error;
1633
1634 leaf = lbp->b_addr;
1635 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1636
1637 #ifdef DEBUG
1638 {
1639 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
1640 struct xfs_dir2_data_free *bf = dp->d_ops->data_bestfree_p(hdr);
1641
1642 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1643 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1644 ASSERT(be16_to_cpu(bf[0].length) ==
1645 args->geo->blksize - dp->d_ops->data_entry_offset);
1646 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1647 }
1648 #endif
1649
1650 /*
1651 * Get rid of the data block.
1652 */
1653 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1654 ASSERT(error != -ENOSPC);
1655 xfs_trans_brelse(tp, dbp);
1656 return error;
1657 }
1658 /*
1659 * Eliminate the last bests entry from the table.
1660 */
1661 bestsp = xfs_dir2_leaf_bests_p(ltp);
1662 be32_add_cpu(&ltp->bestcount, -1);
1663 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1664 xfs_dir3_leaf_log_tail(args, lbp);
1665 xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1666 return 0;
1667 }
1668
1669 static inline size_t
1670 xfs_dir3_leaf_size(
1671 struct xfs_dir3_icleaf_hdr *hdr,
1672 int counts)
1673 {
1674 int entries;
1675 int hdrsize;
1676
1677 entries = hdr->count - hdr->stale;
1678 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1679 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1680 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1681 else
1682 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1683
1684 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1685 + counts * sizeof(xfs_dir2_data_off_t)
1686 + sizeof(xfs_dir2_leaf_tail_t);
1687 }
1688
1689 /*
1690 * Convert node form directory to leaf form directory.
1691 * The root of the node form dir needs to already be a LEAFN block.
1692 * Just return if we can't do anything.
1693 */
1694 int /* error */
1695 xfs_dir2_node_to_leaf(
1696 xfs_da_state_t *state) /* directory operation state */
1697 {
1698 xfs_da_args_t *args; /* operation arguments */
1699 xfs_inode_t *dp; /* incore directory inode */
1700 int error; /* error return code */
1701 struct xfs_buf *fbp; /* buffer for freespace block */
1702 xfs_fileoff_t fo; /* freespace file offset */
1703 xfs_dir2_free_t *free; /* freespace structure */
1704 struct xfs_buf *lbp; /* buffer for leaf block */
1705 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1706 xfs_dir2_leaf_t *leaf; /* leaf structure */
1707 xfs_mount_t *mp; /* filesystem mount point */
1708 int rval; /* successful free trim? */
1709 xfs_trans_t *tp; /* transaction pointer */
1710 struct xfs_dir3_icleaf_hdr leafhdr;
1711 struct xfs_dir3_icfree_hdr freehdr;
1712
1713 /*
1714 * There's more than a leaf level in the btree, so there must
1715 * be multiple leafn blocks. Give up.
1716 */
1717 if (state->path.active > 1)
1718 return 0;
1719 args = state->args;
1720
1721 trace_xfs_dir2_node_to_leaf(args);
1722
1723 mp = state->mp;
1724 dp = args->dp;
1725 tp = args->trans;
1726 /*
1727 * Get the last offset in the file.
1728 */
1729 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
1730 return error;
1731 }
1732 fo -= args->geo->fsbcount;
1733 /*
1734 * If there are freespace blocks other than the first one,
1735 * take this opportunity to remove trailing empty freespace blocks
1736 * that may have been left behind during no-space-reservation
1737 * operations.
1738 */
1739 while (fo > args->geo->freeblk) {
1740 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1741 return error;
1742 }
1743 if (rval)
1744 fo -= args->geo->fsbcount;
1745 else
1746 return 0;
1747 }
1748 /*
1749 * Now find the block just before the freespace block.
1750 */
1751 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1752 return error;
1753 }
1754 /*
1755 * If it's not the single leaf block, give up.
1756 */
1757 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
1758 return 0;
1759 lbp = state->path.blk[0].bp;
1760 leaf = lbp->b_addr;
1761 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1762
1763 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1764 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1765
1766 /*
1767 * Read the freespace block.
1768 */
1769 error = xfs_dir2_free_read(tp, dp, args->geo->freeblk, &fbp);
1770 if (error)
1771 return error;
1772 free = fbp->b_addr;
1773 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1774
1775 ASSERT(!freehdr.firstdb);
1776
1777 /*
1778 * Now see if the leafn and free data will fit in a leaf1.
1779 * If not, release the buffer and give up.
1780 */
1781 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
1782 xfs_trans_brelse(tp, fbp);
1783 return 0;
1784 }
1785
1786 /*
1787 * If the leaf has any stale entries in it, compress them out.
1788 */
1789 if (leafhdr.stale)
1790 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
1791
1792 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
1793 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
1794 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1795 ? XFS_DIR2_LEAF1_MAGIC
1796 : XFS_DIR3_LEAF1_MAGIC;
1797
1798 /*
1799 * Set up the leaf tail from the freespace block.
1800 */
1801 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1802 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
1803
1804 /*
1805 * Set up the leaf bests table.
1806 */
1807 memcpy(xfs_dir2_leaf_bests_p(ltp), dp->d_ops->free_bests_p(free),
1808 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
1809
1810 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1811 xfs_dir3_leaf_log_header(args, lbp);
1812 xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1813 xfs_dir3_leaf_log_tail(args, lbp);
1814 xfs_dir3_leaf_check(dp, lbp);
1815
1816 /*
1817 * Get rid of the freespace block.
1818 */
1819 error = xfs_dir2_shrink_inode(args,
1820 xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
1821 fbp);
1822 if (error) {
1823 /*
1824 * This can't fail here because it can only happen when
1825 * punching out the middle of an extent, and this is an
1826 * isolated block.
1827 */
1828 ASSERT(error != -ENOSPC);
1829 return error;
1830 }
1831 fbp = NULL;
1832 /*
1833 * Now see if we can convert the single-leaf directory
1834 * down to a block form directory.
1835 * This routine always kills the dabuf for the leaf, so
1836 * eliminate it from the path.
1837 */
1838 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1839 state->path.blk[0].bp = NULL;
1840 return error;
1841 }