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