]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_dir2_block.c
82da0d3275e36632a21bd3e119eceedab44f7857
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_block.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_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_trans.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_health.h"
21
22 /*
23 * Local function prototypes.
24 */
25 static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp,
26 int first, int last);
27 static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp);
28 static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp,
29 int *entno);
30 static int xfs_dir2_block_sort(const void *a, const void *b);
31
32 static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
33
34 /*
35 * One-time startup routine called from xfs_init().
36 */
37 void
38 xfs_dir_startup(void)
39 {
40 xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
41 xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
42 }
43
44 static xfs_failaddr_t
45 xfs_dir3_block_verify(
46 struct xfs_buf *bp)
47 {
48 struct xfs_mount *mp = bp->b_mount;
49 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
50
51 if (!xfs_verify_magic(bp, hdr3->magic))
52 return __this_address;
53
54 if (xfs_has_crc(mp)) {
55 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
56 return __this_address;
57 if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
58 return __this_address;
59 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
60 return __this_address;
61 }
62 return __xfs_dir3_data_check(NULL, bp);
63 }
64
65 static void
66 xfs_dir3_block_read_verify(
67 struct xfs_buf *bp)
68 {
69 struct xfs_mount *mp = bp->b_mount;
70 xfs_failaddr_t fa;
71
72 if (xfs_has_crc(mp) &&
73 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
74 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
75 else {
76 fa = xfs_dir3_block_verify(bp);
77 if (fa)
78 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
79 }
80 }
81
82 static void
83 xfs_dir3_block_write_verify(
84 struct xfs_buf *bp)
85 {
86 struct xfs_mount *mp = bp->b_mount;
87 struct xfs_buf_log_item *bip = bp->b_log_item;
88 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
89 xfs_failaddr_t fa;
90
91 fa = xfs_dir3_block_verify(bp);
92 if (fa) {
93 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
94 return;
95 }
96
97 if (!xfs_has_crc(mp))
98 return;
99
100 if (bip)
101 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
102
103 xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
104 }
105
106 const struct xfs_buf_ops xfs_dir3_block_buf_ops = {
107 .name = "xfs_dir3_block",
108 .magic = { cpu_to_be32(XFS_DIR2_BLOCK_MAGIC),
109 cpu_to_be32(XFS_DIR3_BLOCK_MAGIC) },
110 .verify_read = xfs_dir3_block_read_verify,
111 .verify_write = xfs_dir3_block_write_verify,
112 .verify_struct = xfs_dir3_block_verify,
113 };
114
115 xfs_failaddr_t
116 xfs_dir3_block_header_check(
117 struct xfs_buf *bp,
118 xfs_ino_t owner)
119 {
120 struct xfs_mount *mp = bp->b_mount;
121
122 if (xfs_has_crc(mp)) {
123 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
124
125 if (hdr3->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
126 return __this_address;
127
128 if (be64_to_cpu(hdr3->owner) != owner)
129 return __this_address;
130 }
131
132 return NULL;
133 }
134
135 int
136 xfs_dir3_block_read(
137 struct xfs_trans *tp,
138 struct xfs_inode *dp,
139 xfs_ino_t owner,
140 struct xfs_buf **bpp)
141 {
142 struct xfs_mount *mp = dp->i_mount;
143 xfs_failaddr_t fa;
144 int err;
145
146 err = xfs_da_read_buf(tp, dp, mp->m_dir_geo->datablk, 0, bpp,
147 XFS_DATA_FORK, &xfs_dir3_block_buf_ops);
148 if (err || !*bpp)
149 return err;
150
151 /* Check things that we can't do in the verifier. */
152 fa = xfs_dir3_block_header_check(*bpp, owner);
153 if (fa) {
154 __xfs_buf_mark_corrupt(*bpp, fa);
155 xfs_trans_brelse(tp, *bpp);
156 *bpp = NULL;
157 xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
158 return -EFSCORRUPTED;
159 }
160
161 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF);
162 return err;
163 }
164
165 static void
166 xfs_dir3_block_init(
167 struct xfs_da_args *args,
168 struct xfs_buf *bp)
169 {
170 struct xfs_trans *tp = args->trans;
171 struct xfs_inode *dp = args->dp;
172 struct xfs_mount *mp = dp->i_mount;
173 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
174
175 bp->b_ops = &xfs_dir3_block_buf_ops;
176 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF);
177
178 if (xfs_has_crc(mp)) {
179 memset(hdr3, 0, sizeof(*hdr3));
180 hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
181 hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp));
182 hdr3->owner = cpu_to_be64(args->owner);
183 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
184 return;
185
186 }
187 hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
188 }
189
190 static void
191 xfs_dir2_block_need_space(
192 struct xfs_inode *dp,
193 struct xfs_dir2_data_hdr *hdr,
194 struct xfs_dir2_block_tail *btp,
195 struct xfs_dir2_leaf_entry *blp,
196 __be16 **tagpp,
197 struct xfs_dir2_data_unused **dupp,
198 struct xfs_dir2_data_unused **enddupp,
199 int *compact,
200 int len)
201 {
202 struct xfs_dir2_data_free *bf;
203 __be16 *tagp = NULL;
204 struct xfs_dir2_data_unused *dup = NULL;
205 struct xfs_dir2_data_unused *enddup = NULL;
206
207 *compact = 0;
208 bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
209
210 /*
211 * If there are stale entries we'll use one for the leaf.
212 */
213 if (btp->stale) {
214 if (be16_to_cpu(bf[0].length) >= len) {
215 /*
216 * The biggest entry enough to avoid compaction.
217 */
218 dup = (xfs_dir2_data_unused_t *)
219 ((char *)hdr + be16_to_cpu(bf[0].offset));
220 goto out;
221 }
222
223 /*
224 * Will need to compact to make this work.
225 * Tag just before the first leaf entry.
226 */
227 *compact = 1;
228 tagp = (__be16 *)blp - 1;
229
230 /* Data object just before the first leaf entry. */
231 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
232
233 /*
234 * If it's not free then the data will go where the
235 * leaf data starts now, if it works at all.
236 */
237 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
238 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
239 (uint)sizeof(*blp) < len)
240 dup = NULL;
241 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
242 dup = NULL;
243 else
244 dup = (xfs_dir2_data_unused_t *)blp;
245 goto out;
246 }
247
248 /*
249 * no stale entries, so just use free space.
250 * Tag just before the first leaf entry.
251 */
252 tagp = (__be16 *)blp - 1;
253
254 /* Data object just before the first leaf entry. */
255 enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
256
257 /*
258 * If it's not free then can't do this add without cleaning up:
259 * the space before the first leaf entry needs to be free so it
260 * can be expanded to hold the pointer to the new entry.
261 */
262 if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
263 /*
264 * Check out the biggest freespace and see if it's the same one.
265 */
266 dup = (xfs_dir2_data_unused_t *)
267 ((char *)hdr + be16_to_cpu(bf[0].offset));
268 if (dup != enddup) {
269 /*
270 * Not the same free entry, just check its length.
271 */
272 if (be16_to_cpu(dup->length) < len)
273 dup = NULL;
274 goto out;
275 }
276
277 /*
278 * It is the biggest freespace, can it hold the leaf too?
279 */
280 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
281 /*
282 * Yes, use the second-largest entry instead if it works.
283 */
284 if (be16_to_cpu(bf[1].length) >= len)
285 dup = (xfs_dir2_data_unused_t *)
286 ((char *)hdr + be16_to_cpu(bf[1].offset));
287 else
288 dup = NULL;
289 }
290 }
291 out:
292 *tagpp = tagp;
293 *dupp = dup;
294 *enddupp = enddup;
295 }
296
297 /*
298 * compact the leaf entries.
299 * Leave the highest-numbered stale entry stale.
300 * XXX should be the one closest to mid but mid is not yet computed.
301 */
302 static void
303 xfs_dir2_block_compact(
304 struct xfs_da_args *args,
305 struct xfs_buf *bp,
306 struct xfs_dir2_data_hdr *hdr,
307 struct xfs_dir2_block_tail *btp,
308 struct xfs_dir2_leaf_entry *blp,
309 int *needlog,
310 int *lfloghigh,
311 int *lfloglow)
312 {
313 int fromidx; /* source leaf index */
314 int toidx; /* target leaf index */
315 int needscan = 0;
316 int highstale; /* high stale index */
317
318 fromidx = toidx = be32_to_cpu(btp->count) - 1;
319 highstale = *lfloghigh = -1;
320 for (; fromidx >= 0; fromidx--) {
321 if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
322 if (highstale == -1)
323 highstale = toidx;
324 else {
325 if (*lfloghigh == -1)
326 *lfloghigh = toidx;
327 continue;
328 }
329 }
330 if (fromidx < toidx)
331 blp[toidx] = blp[fromidx];
332 toidx--;
333 }
334 *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
335 *lfloghigh -= be32_to_cpu(btp->stale) - 1;
336 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
337 xfs_dir2_data_make_free(args, bp,
338 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
339 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
340 needlog, &needscan);
341 btp->stale = cpu_to_be32(1);
342 /*
343 * If we now need to rebuild the bestfree map, do so.
344 * This needs to happen before the next call to use_free.
345 */
346 if (needscan)
347 xfs_dir2_data_freescan(args->dp->i_mount, hdr, needlog);
348 }
349
350 /*
351 * Add an entry to a block directory.
352 */
353 int /* error */
354 xfs_dir2_block_addname(
355 xfs_da_args_t *args) /* directory op arguments */
356 {
357 xfs_dir2_data_hdr_t *hdr; /* block header */
358 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
359 struct xfs_buf *bp; /* buffer for block */
360 xfs_dir2_block_tail_t *btp; /* block tail */
361 int compact; /* need to compact leaf ents */
362 xfs_dir2_data_entry_t *dep; /* block data entry */
363 xfs_inode_t *dp; /* directory inode */
364 xfs_dir2_data_unused_t *dup; /* block unused entry */
365 int error; /* error return value */
366 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
367 xfs_dahash_t hash; /* hash value of found entry */
368 int high; /* high index for binary srch */
369 int highstale; /* high stale index */
370 int lfloghigh=0; /* last final leaf to log */
371 int lfloglow=0; /* first final leaf to log */
372 int len; /* length of the new entry */
373 int low; /* low index for binary srch */
374 int lowstale; /* low stale index */
375 int mid=0; /* midpoint for binary srch */
376 int needlog; /* need to log header */
377 int needscan; /* need to rescan freespace */
378 __be16 *tagp; /* pointer to tag value */
379 xfs_trans_t *tp; /* transaction structure */
380
381 trace_xfs_dir2_block_addname(args);
382
383 dp = args->dp;
384 tp = args->trans;
385
386 /* Read the (one and only) directory block into bp. */
387 error = xfs_dir3_block_read(tp, dp, args->owner, &bp);
388 if (error)
389 return error;
390
391 len = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
392
393 /*
394 * Set up pointers to parts of the block.
395 */
396 hdr = bp->b_addr;
397 btp = xfs_dir2_block_tail_p(args->geo, hdr);
398 blp = xfs_dir2_block_leaf_p(btp);
399
400 /*
401 * Find out if we can reuse stale entries or whether we need extra
402 * space for entry and new leaf.
403 */
404 xfs_dir2_block_need_space(dp, hdr, btp, blp, &tagp, &dup,
405 &enddup, &compact, len);
406
407 /*
408 * Done everything we need for a space check now.
409 */
410 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
411 xfs_trans_brelse(tp, bp);
412 if (!dup)
413 return -ENOSPC;
414 return 0;
415 }
416
417 /*
418 * If we don't have space for the new entry & leaf ...
419 */
420 if (!dup) {
421 /* Don't have a space reservation: return no-space. */
422 if (args->total == 0)
423 return -ENOSPC;
424 /*
425 * Convert to the next larger format.
426 * Then add the new entry in that format.
427 */
428 error = xfs_dir2_block_to_leaf(args, bp);
429 if (error)
430 return error;
431 return xfs_dir2_leaf_addname(args);
432 }
433
434 needlog = needscan = 0;
435
436 /*
437 * If need to compact the leaf entries, do it now.
438 */
439 if (compact) {
440 xfs_dir2_block_compact(args, bp, hdr, btp, blp, &needlog,
441 &lfloghigh, &lfloglow);
442 /* recalculate blp post-compaction */
443 blp = xfs_dir2_block_leaf_p(btp);
444 } else if (btp->stale) {
445 /*
446 * Set leaf logging boundaries to impossible state.
447 * For the no-stale case they're set explicitly.
448 */
449 lfloglow = be32_to_cpu(btp->count);
450 lfloghigh = -1;
451 }
452
453 /*
454 * Find the slot that's first lower than our hash value, -1 if none.
455 */
456 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
457 mid = (low + high) >> 1;
458 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
459 break;
460 if (hash < args->hashval)
461 low = mid + 1;
462 else
463 high = mid - 1;
464 }
465 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
466 mid--;
467 }
468 /*
469 * No stale entries, will use enddup space to hold new leaf.
470 */
471 if (!btp->stale) {
472 xfs_dir2_data_aoff_t aoff;
473
474 /*
475 * Mark the space needed for the new leaf entry, now in use.
476 */
477 aoff = (xfs_dir2_data_aoff_t)((char *)enddup - (char *)hdr +
478 be16_to_cpu(enddup->length) - sizeof(*blp));
479 error = xfs_dir2_data_use_free(args, bp, enddup, aoff,
480 (xfs_dir2_data_aoff_t)sizeof(*blp), &needlog,
481 &needscan);
482 if (error)
483 return error;
484
485 /*
486 * Update the tail (entry count).
487 */
488 be32_add_cpu(&btp->count, 1);
489 /*
490 * If we now need to rebuild the bestfree map, do so.
491 * This needs to happen before the next call to use_free.
492 */
493 if (needscan) {
494 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
495 needscan = 0;
496 }
497 /*
498 * Adjust pointer to the first leaf entry, we're about to move
499 * the table up one to open up space for the new leaf entry.
500 * Then adjust our index to match.
501 */
502 blp--;
503 mid++;
504 if (mid)
505 memmove(blp, &blp[1], mid * sizeof(*blp));
506 lfloglow = 0;
507 lfloghigh = mid;
508 }
509 /*
510 * Use a stale leaf for our new entry.
511 */
512 else {
513 for (lowstale = mid;
514 lowstale >= 0 &&
515 blp[lowstale].address !=
516 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
517 lowstale--)
518 continue;
519 for (highstale = mid + 1;
520 highstale < be32_to_cpu(btp->count) &&
521 blp[highstale].address !=
522 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
523 (lowstale < 0 || mid - lowstale > highstale - mid);
524 highstale++)
525 continue;
526 /*
527 * Move entries toward the low-numbered stale entry.
528 */
529 if (lowstale >= 0 &&
530 (highstale == be32_to_cpu(btp->count) ||
531 mid - lowstale <= highstale - mid)) {
532 if (mid - lowstale)
533 memmove(&blp[lowstale], &blp[lowstale + 1],
534 (mid - lowstale) * sizeof(*blp));
535 lfloglow = min(lowstale, lfloglow);
536 lfloghigh = max(mid, lfloghigh);
537 }
538 /*
539 * Move entries toward the high-numbered stale entry.
540 */
541 else {
542 ASSERT(highstale < be32_to_cpu(btp->count));
543 mid++;
544 if (highstale - mid)
545 memmove(&blp[mid + 1], &blp[mid],
546 (highstale - mid) * sizeof(*blp));
547 lfloglow = min(mid, lfloglow);
548 lfloghigh = max(highstale, lfloghigh);
549 }
550 be32_add_cpu(&btp->stale, -1);
551 }
552 /*
553 * Point to the new data entry.
554 */
555 dep = (xfs_dir2_data_entry_t *)dup;
556 /*
557 * Fill in the leaf entry.
558 */
559 blp[mid].hashval = cpu_to_be32(args->hashval);
560 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
561 (char *)dep - (char *)hdr));
562 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
563 /*
564 * Mark space for the data entry used.
565 */
566 error = xfs_dir2_data_use_free(args, bp, dup,
567 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
568 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
569 if (error)
570 return error;
571 /*
572 * Create the new data entry.
573 */
574 dep->inumber = cpu_to_be64(args->inumber);
575 dep->namelen = args->namelen;
576 memcpy(dep->name, args->name, args->namelen);
577 xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
578 tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
579 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
580 /*
581 * Clean up the bestfree array and log the header, tail, and entry.
582 */
583 if (needscan)
584 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
585 if (needlog)
586 xfs_dir2_data_log_header(args, bp);
587 xfs_dir2_block_log_tail(tp, bp);
588 xfs_dir2_data_log_entry(args, bp, dep);
589 xfs_dir3_data_check(dp, bp);
590 return 0;
591 }
592
593 /*
594 * Log leaf entries from the block.
595 */
596 static void
597 xfs_dir2_block_log_leaf(
598 xfs_trans_t *tp, /* transaction structure */
599 struct xfs_buf *bp, /* block buffer */
600 int first, /* index of first logged leaf */
601 int last) /* index of last logged leaf */
602 {
603 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
604 xfs_dir2_leaf_entry_t *blp;
605 xfs_dir2_block_tail_t *btp;
606
607 btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
608 blp = xfs_dir2_block_leaf_p(btp);
609 xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
610 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
611 }
612
613 /*
614 * Log the block tail.
615 */
616 static void
617 xfs_dir2_block_log_tail(
618 xfs_trans_t *tp, /* transaction structure */
619 struct xfs_buf *bp) /* block buffer */
620 {
621 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
622 xfs_dir2_block_tail_t *btp;
623
624 btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
625 xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
626 (uint)((char *)(btp + 1) - (char *)hdr - 1));
627 }
628
629 /*
630 * Look up an entry in the block. This is the external routine,
631 * xfs_dir2_block_lookup_int does the real work.
632 */
633 int /* error */
634 xfs_dir2_block_lookup(
635 xfs_da_args_t *args) /* dir lookup arguments */
636 {
637 xfs_dir2_data_hdr_t *hdr; /* block header */
638 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
639 struct xfs_buf *bp; /* block buffer */
640 xfs_dir2_block_tail_t *btp; /* block tail */
641 xfs_dir2_data_entry_t *dep; /* block data entry */
642 xfs_inode_t *dp; /* incore inode */
643 int ent; /* entry index */
644 int error; /* error return value */
645
646 trace_xfs_dir2_block_lookup(args);
647
648 /*
649 * Get the buffer, look up the entry.
650 * If not found (ENOENT) then return, have no buffer.
651 */
652 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
653 return error;
654 dp = args->dp;
655 hdr = bp->b_addr;
656 xfs_dir3_data_check(dp, bp);
657 btp = xfs_dir2_block_tail_p(args->geo, hdr);
658 blp = xfs_dir2_block_leaf_p(btp);
659 /*
660 * Get the offset from the leaf entry, to point to the data.
661 */
662 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
663 xfs_dir2_dataptr_to_off(args->geo,
664 be32_to_cpu(blp[ent].address)));
665 /*
666 * Fill in inode number, CI name if appropriate, release the block.
667 */
668 args->inumber = be64_to_cpu(dep->inumber);
669 args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
670 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
671 xfs_trans_brelse(args->trans, bp);
672 return error;
673 }
674
675 /*
676 * Internal block lookup routine.
677 */
678 static int /* error */
679 xfs_dir2_block_lookup_int(
680 xfs_da_args_t *args, /* dir lookup arguments */
681 struct xfs_buf **bpp, /* returned block buffer */
682 int *entno) /* returned entry number */
683 {
684 xfs_dir2_dataptr_t addr; /* data entry address */
685 xfs_dir2_data_hdr_t *hdr; /* block header */
686 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
687 struct xfs_buf *bp; /* block buffer */
688 xfs_dir2_block_tail_t *btp; /* block tail */
689 xfs_dir2_data_entry_t *dep; /* block data entry */
690 xfs_inode_t *dp; /* incore inode */
691 int error; /* error return value */
692 xfs_dahash_t hash; /* found hash value */
693 int high; /* binary search high index */
694 int low; /* binary search low index */
695 int mid; /* binary search current idx */
696 xfs_trans_t *tp; /* transaction pointer */
697 enum xfs_dacmp cmp; /* comparison result */
698
699 dp = args->dp;
700 tp = args->trans;
701
702 error = xfs_dir3_block_read(tp, dp, args->owner, &bp);
703 if (error)
704 return error;
705
706 hdr = bp->b_addr;
707 xfs_dir3_data_check(dp, bp);
708 btp = xfs_dir2_block_tail_p(args->geo, hdr);
709 blp = xfs_dir2_block_leaf_p(btp);
710 /*
711 * Loop doing a binary search for our hash value.
712 * Find our entry, ENOENT if it's not there.
713 */
714 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
715 ASSERT(low <= high);
716 mid = (low + high) >> 1;
717 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
718 break;
719 if (hash < args->hashval)
720 low = mid + 1;
721 else
722 high = mid - 1;
723 if (low > high) {
724 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
725 xfs_trans_brelse(tp, bp);
726 return -ENOENT;
727 }
728 }
729 /*
730 * Back up to the first one with the right hash value.
731 */
732 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
733 mid--;
734 }
735 /*
736 * Now loop forward through all the entries with the
737 * right hash value looking for our name.
738 */
739 do {
740 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
741 continue;
742 /*
743 * Get pointer to the entry from the leaf.
744 */
745 dep = (xfs_dir2_data_entry_t *)
746 ((char *)hdr + xfs_dir2_dataptr_to_off(args->geo, addr));
747 /*
748 * Compare name and if it's an exact match, return the index
749 * and buffer. If it's the first case-insensitive match, store
750 * the index and buffer and continue looking for an exact match.
751 */
752 cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
753 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
754 args->cmpresult = cmp;
755 *bpp = bp;
756 *entno = mid;
757 if (cmp == XFS_CMP_EXACT)
758 return 0;
759 }
760 } while (++mid < be32_to_cpu(btp->count) &&
761 be32_to_cpu(blp[mid].hashval) == hash);
762
763 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
764 /*
765 * Here, we can only be doing a lookup (not a rename or replace).
766 * If a case-insensitive match was found earlier, return success.
767 */
768 if (args->cmpresult == XFS_CMP_CASE)
769 return 0;
770 /*
771 * No match, release the buffer and return ENOENT.
772 */
773 xfs_trans_brelse(tp, bp);
774 return -ENOENT;
775 }
776
777 /*
778 * Remove an entry from a block format directory.
779 * If that makes the block small enough to fit in shortform, transform it.
780 */
781 int /* error */
782 xfs_dir2_block_removename(
783 xfs_da_args_t *args) /* directory operation args */
784 {
785 xfs_dir2_data_hdr_t *hdr; /* block header */
786 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
787 struct xfs_buf *bp; /* block buffer */
788 xfs_dir2_block_tail_t *btp; /* block tail */
789 xfs_dir2_data_entry_t *dep; /* block data entry */
790 xfs_inode_t *dp; /* incore inode */
791 int ent; /* block leaf entry index */
792 int error; /* error return value */
793 int needlog; /* need to log block header */
794 int needscan; /* need to fixup bestfree */
795 xfs_dir2_sf_hdr_t sfh; /* shortform header */
796 int size; /* shortform size */
797 xfs_trans_t *tp; /* transaction pointer */
798
799 trace_xfs_dir2_block_removename(args);
800
801 /*
802 * Look up the entry in the block. Gets the buffer and entry index.
803 * It will always be there, the vnodeops level does a lookup first.
804 */
805 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
806 return error;
807 }
808 dp = args->dp;
809 tp = args->trans;
810 hdr = bp->b_addr;
811 btp = xfs_dir2_block_tail_p(args->geo, hdr);
812 blp = xfs_dir2_block_leaf_p(btp);
813 /*
814 * Point to the data entry using the leaf entry.
815 */
816 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
817 xfs_dir2_dataptr_to_off(args->geo,
818 be32_to_cpu(blp[ent].address)));
819 /*
820 * Mark the data entry's space free.
821 */
822 needlog = needscan = 0;
823 xfs_dir2_data_make_free(args, bp,
824 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
825 xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
826 &needscan);
827 /*
828 * Fix up the block tail.
829 */
830 be32_add_cpu(&btp->stale, 1);
831 xfs_dir2_block_log_tail(tp, bp);
832 /*
833 * Remove the leaf entry by marking it stale.
834 */
835 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
836 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
837 /*
838 * Fix up bestfree, log the header if necessary.
839 */
840 if (needscan)
841 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
842 if (needlog)
843 xfs_dir2_data_log_header(args, bp);
844 xfs_dir3_data_check(dp, bp);
845 /*
846 * See if the size as a shortform is good enough.
847 */
848 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
849 if (size > xfs_inode_data_fork_size(dp))
850 return 0;
851
852 /*
853 * If it works, do the conversion.
854 */
855 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
856 }
857
858 /*
859 * Replace an entry in a V2 block directory.
860 * Change the inode number to the new value.
861 */
862 int /* error */
863 xfs_dir2_block_replace(
864 xfs_da_args_t *args) /* directory operation args */
865 {
866 xfs_dir2_data_hdr_t *hdr; /* block header */
867 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
868 struct xfs_buf *bp; /* block buffer */
869 xfs_dir2_block_tail_t *btp; /* block tail */
870 xfs_dir2_data_entry_t *dep; /* block data entry */
871 xfs_inode_t *dp; /* incore inode */
872 int ent; /* leaf entry index */
873 int error; /* error return value */
874
875 trace_xfs_dir2_block_replace(args);
876
877 /*
878 * Lookup the entry in the directory. Get buffer and entry index.
879 * This will always succeed since the caller has already done a lookup.
880 */
881 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
882 return error;
883 }
884 dp = args->dp;
885 hdr = bp->b_addr;
886 btp = xfs_dir2_block_tail_p(args->geo, hdr);
887 blp = xfs_dir2_block_leaf_p(btp);
888 /*
889 * Point to the data entry we need to change.
890 */
891 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
892 xfs_dir2_dataptr_to_off(args->geo,
893 be32_to_cpu(blp[ent].address)));
894 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
895 /*
896 * Change the inode number to the new value.
897 */
898 dep->inumber = cpu_to_be64(args->inumber);
899 xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
900 xfs_dir2_data_log_entry(args, bp, dep);
901 xfs_dir3_data_check(dp, bp);
902 return 0;
903 }
904
905 /*
906 * Qsort comparison routine for the block leaf entries.
907 */
908 static int /* sort order */
909 xfs_dir2_block_sort(
910 const void *a, /* first leaf entry */
911 const void *b) /* second leaf entry */
912 {
913 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
914 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
915
916 la = a;
917 lb = b;
918 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
919 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
920 }
921
922 /*
923 * Convert a V2 leaf directory to a V2 block directory if possible.
924 */
925 int /* error */
926 xfs_dir2_leaf_to_block(
927 xfs_da_args_t *args, /* operation arguments */
928 struct xfs_buf *lbp, /* leaf buffer */
929 struct xfs_buf *dbp) /* data buffer */
930 {
931 __be16 *bestsp; /* leaf bests table */
932 xfs_dir2_data_hdr_t *hdr; /* block header */
933 xfs_dir2_block_tail_t *btp; /* block tail */
934 xfs_inode_t *dp; /* incore directory inode */
935 xfs_dir2_data_unused_t *dup; /* unused data entry */
936 int error; /* error return value */
937 int from; /* leaf from index */
938 xfs_dir2_leaf_t *leaf; /* leaf structure */
939 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
940 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
941 xfs_mount_t *mp; /* file system mount point */
942 int needlog; /* need to log data header */
943 int needscan; /* need to scan for bestfree */
944 xfs_dir2_sf_hdr_t sfh; /* shortform header */
945 int size; /* bytes used */
946 __be16 *tagp; /* end of entry (tag) */
947 int to; /* block/leaf to index */
948 xfs_trans_t *tp; /* transaction pointer */
949 struct xfs_dir3_icleaf_hdr leafhdr;
950
951 trace_xfs_dir2_leaf_to_block(args);
952
953 dp = args->dp;
954 tp = args->trans;
955 mp = dp->i_mount;
956 leaf = lbp->b_addr;
957 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
958 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
959
960 ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
961 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
962 /*
963 * If there are data blocks other than the first one, take this
964 * opportunity to remove trailing empty data blocks that may have
965 * been left behind during no-space-reservation operations.
966 * These will show up in the leaf bests table.
967 */
968 while (dp->i_disk_size > args->geo->blksize) {
969 int hdrsz;
970
971 hdrsz = args->geo->data_entry_offset;
972 bestsp = xfs_dir2_leaf_bests_p(ltp);
973 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
974 args->geo->blksize - hdrsz) {
975 if ((error =
976 xfs_dir2_leaf_trim_data(args, lbp,
977 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
978 return error;
979 } else
980 return 0;
981 }
982 /*
983 * Read the data block if we don't already have it, give up if it fails.
984 */
985 if (!dbp) {
986 error = xfs_dir3_data_read(tp, dp, args->owner,
987 args->geo->datablk, 0, &dbp);
988 if (error)
989 return error;
990 }
991 hdr = dbp->b_addr;
992 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
993 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
994
995 /*
996 * Size of the "leaf" area in the block.
997 */
998 size = (uint)sizeof(xfs_dir2_block_tail_t) +
999 (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
1000 /*
1001 * Look at the last data entry.
1002 */
1003 tagp = (__be16 *)((char *)hdr + args->geo->blksize) - 1;
1004 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
1005 /*
1006 * If it's not free or is too short we can't do it.
1007 */
1008 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
1009 be16_to_cpu(dup->length) < size)
1010 return 0;
1011
1012 /*
1013 * Start converting it to block form.
1014 */
1015 xfs_dir3_block_init(args, dbp);
1016
1017 needlog = 1;
1018 needscan = 0;
1019 /*
1020 * Use up the space at the end of the block (blp/btp).
1021 */
1022 error = xfs_dir2_data_use_free(args, dbp, dup,
1023 args->geo->blksize - size, size, &needlog, &needscan);
1024 if (error)
1025 return error;
1026 /*
1027 * Initialize the block tail.
1028 */
1029 btp = xfs_dir2_block_tail_p(args->geo, hdr);
1030 btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
1031 btp->stale = 0;
1032 xfs_dir2_block_log_tail(tp, dbp);
1033 /*
1034 * Initialize the block leaf area. We compact out stale entries.
1035 */
1036 lep = xfs_dir2_block_leaf_p(btp);
1037 for (from = to = 0; from < leafhdr.count; from++) {
1038 if (leafhdr.ents[from].address ==
1039 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
1040 continue;
1041 lep[to++] = leafhdr.ents[from];
1042 }
1043 ASSERT(to == be32_to_cpu(btp->count));
1044 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
1045 /*
1046 * Scan the bestfree if we need it and log the data block header.
1047 */
1048 if (needscan)
1049 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1050 if (needlog)
1051 xfs_dir2_data_log_header(args, dbp);
1052 /*
1053 * Pitch the old leaf block.
1054 */
1055 error = xfs_da_shrink_inode(args, args->geo->leafblk, lbp);
1056 if (error)
1057 return error;
1058
1059 /*
1060 * Now see if the resulting block can be shrunken to shortform.
1061 */
1062 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
1063 if (size > xfs_inode_data_fork_size(dp))
1064 return 0;
1065
1066 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1067 }
1068
1069 /*
1070 * Convert the shortform directory to block form.
1071 */
1072 int /* error */
1073 xfs_dir2_sf_to_block(
1074 struct xfs_da_args *args)
1075 {
1076 struct xfs_trans *tp = args->trans;
1077 struct xfs_inode *dp = args->dp;
1078 struct xfs_mount *mp = dp->i_mount;
1079 struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
1080 struct xfs_da_geometry *geo = args->geo;
1081 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
1082 xfs_dir2_data_hdr_t *hdr; /* block header */
1083 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1084 struct xfs_buf *bp; /* block buffer */
1085 xfs_dir2_block_tail_t *btp; /* block tail pointer */
1086 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1087 int dummy; /* trash */
1088 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1089 int endoffset; /* end of data objects */
1090 int error; /* error return value */
1091 int i; /* index */
1092 int needlog; /* need to log block header */
1093 int needscan; /* need to scan block freespc */
1094 int newoffset; /* offset from current entry */
1095 unsigned int offset = geo->data_entry_offset;
1096 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
1097 struct xfs_dir2_sf_hdr *oldsfp = ifp->if_data;
1098 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
1099 __be16 *tagp; /* end of data entry */
1100 struct xfs_name name;
1101
1102 trace_xfs_dir2_sf_to_block(args);
1103
1104 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
1105 ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
1106
1107 ASSERT(ifp->if_bytes == dp->i_disk_size);
1108 ASSERT(oldsfp != NULL);
1109 ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1110 ASSERT(dp->i_df.if_nextents == 0);
1111
1112 /*
1113 * Copy the directory into a temporary buffer.
1114 * Then pitch the incore inode data so we can make extents.
1115 */
1116 sfp = kmalloc(ifp->if_bytes, GFP_KERNEL | __GFP_NOFAIL);
1117 memcpy(sfp, oldsfp, ifp->if_bytes);
1118
1119 xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK);
1120 xfs_bmap_local_to_extents_empty(tp, dp, XFS_DATA_FORK);
1121 dp->i_disk_size = 0;
1122
1123 /*
1124 * Add block 0 to the inode.
1125 */
1126 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1127 if (error)
1128 goto out_free;
1129 /*
1130 * Initialize the data block, then convert it to block format.
1131 */
1132 error = xfs_dir3_data_init(args, blkno, &bp);
1133 if (error)
1134 goto out_free;
1135 xfs_dir3_block_init(args, bp);
1136 hdr = bp->b_addr;
1137
1138 /*
1139 * Compute size of block "tail" area.
1140 */
1141 i = (uint)sizeof(*btp) +
1142 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
1143 /*
1144 * The whole thing is initialized to free by the init routine.
1145 * Say we're using the leaf and tail area.
1146 */
1147 dup = bp->b_addr + offset;
1148 needlog = needscan = 0;
1149 error = xfs_dir2_data_use_free(args, bp, dup, args->geo->blksize - i,
1150 i, &needlog, &needscan);
1151 if (error)
1152 goto out_free;
1153 ASSERT(needscan == 0);
1154 /*
1155 * Fill in the tail.
1156 */
1157 btp = xfs_dir2_block_tail_p(args->geo, hdr);
1158 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
1159 btp->stale = 0;
1160 blp = xfs_dir2_block_leaf_p(btp);
1161 endoffset = (uint)((char *)blp - (char *)hdr);
1162 /*
1163 * Remove the freespace, we'll manage it.
1164 */
1165 error = xfs_dir2_data_use_free(args, bp, dup,
1166 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
1167 be16_to_cpu(dup->length), &needlog, &needscan);
1168 if (error)
1169 goto out_free;
1170
1171 /*
1172 * Create entry for .
1173 */
1174 dep = bp->b_addr + offset;
1175 dep->inumber = cpu_to_be64(args->owner);
1176 dep->namelen = 1;
1177 dep->name[0] = '.';
1178 xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
1179 tagp = xfs_dir2_data_entry_tag_p(mp, dep);
1180 *tagp = cpu_to_be16(offset);
1181 xfs_dir2_data_log_entry(args, bp, dep);
1182 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1183 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset));
1184 offset += xfs_dir2_data_entsize(mp, dep->namelen);
1185
1186 /*
1187 * Create entry for ..
1188 */
1189 dep = bp->b_addr + offset;
1190 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
1191 dep->namelen = 2;
1192 dep->name[0] = dep->name[1] = '.';
1193 xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
1194 tagp = xfs_dir2_data_entry_tag_p(mp, dep);
1195 *tagp = cpu_to_be16(offset);
1196 xfs_dir2_data_log_entry(args, bp, dep);
1197 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1198 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset));
1199 offset += xfs_dir2_data_entsize(mp, dep->namelen);
1200
1201 /*
1202 * Loop over existing entries, stuff them in.
1203 */
1204 i = 0;
1205 if (!sfp->count)
1206 sfep = NULL;
1207 else
1208 sfep = xfs_dir2_sf_firstentry(sfp);
1209
1210 /*
1211 * Need to preserve the existing offset values in the sf directory.
1212 * Insert holes (unused entries) where necessary.
1213 */
1214 while (offset < endoffset) {
1215 /*
1216 * sfep is null when we reach the end of the list.
1217 */
1218 if (sfep == NULL)
1219 newoffset = endoffset;
1220 else
1221 newoffset = xfs_dir2_sf_get_offset(sfep);
1222 /*
1223 * There should be a hole here, make one.
1224 */
1225 if (offset < newoffset) {
1226 dup = bp->b_addr + offset;
1227 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1228 dup->length = cpu_to_be16(newoffset - offset);
1229 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(offset);
1230 xfs_dir2_data_log_unused(args, bp, dup);
1231 xfs_dir2_data_freeinsert(hdr,
1232 xfs_dir2_data_bestfree_p(mp, hdr),
1233 dup, &dummy);
1234 offset += be16_to_cpu(dup->length);
1235 continue;
1236 }
1237 /*
1238 * Copy a real entry.
1239 */
1240 dep = bp->b_addr + newoffset;
1241 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_ino(mp, sfp, sfep));
1242 dep->namelen = sfep->namelen;
1243 xfs_dir2_data_put_ftype(mp, dep,
1244 xfs_dir2_sf_get_ftype(mp, sfep));
1245 memcpy(dep->name, sfep->name, dep->namelen);
1246 tagp = xfs_dir2_data_entry_tag_p(mp, dep);
1247 *tagp = cpu_to_be16(newoffset);
1248 xfs_dir2_data_log_entry(args, bp, dep);
1249 name.name = sfep->name;
1250 name.len = sfep->namelen;
1251 blp[2 + i].hashval = cpu_to_be32(xfs_dir2_hashname(mp, &name));
1252 blp[2 + i].address =
1253 cpu_to_be32(xfs_dir2_byte_to_dataptr(newoffset));
1254 offset = (int)((char *)(tagp + 1) - (char *)hdr);
1255 if (++i == sfp->count)
1256 sfep = NULL;
1257 else
1258 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
1259 }
1260 /* Done with the temporary buffer */
1261 kfree(sfp);
1262 /*
1263 * Sort the leaf entries by hash value.
1264 */
1265 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
1266 /*
1267 * Log the leaf entry area and tail.
1268 * Already logged the header in data_init, ignore needlog.
1269 */
1270 ASSERT(needscan == 0);
1271 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1272 xfs_dir2_block_log_tail(tp, bp);
1273 xfs_dir3_data_check(dp, bp);
1274 return 0;
1275 out_free:
1276 kfree(sfp);
1277 return error;
1278 }