]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_dir2_block.c
xfsprogs: Release v6.15.0
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_block.c
CommitLineData
37b3b4d6 1// SPDX-License-Identifier: GPL-2.0
2bd0ea18 2/*
5e656dbb 3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
693fc8f6 4 * Copyright (c) 2013 Red Hat, Inc.
da23017d 5 * All Rights Reserved.
2bd0ea18 6 */
9c799827 7#include "libxfs_priv.h"
b626fb59 8#include "xfs_fs.h"
27846843 9#include "xfs_shared.h"
b626fb59
DC
10#include "xfs_format.h"
11#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
13#include "xfs_mount.h"
b626fb59
DC
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"
bcf1c967 20#include "xfs_health.h"
5e656dbb 21
2bd0ea18 22/*
5e656dbb 23 * Local function prototypes.
2bd0ea18 24 */
a2ceac1f
DC
25static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp,
26 int first, int last);
27static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp);
28static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp,
5e656dbb
BN
29 int *entno);
30static int xfs_dir2_block_sort(const void *a, const void *b);
2bd0ea18 31
5e656dbb
BN
32static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
33
34/*
35 * One-time startup routine called from xfs_init().
36 */
37void
38xfs_dir_startup(void)
39{
56b2de80
DC
40 xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
41 xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
5e656dbb 42}
2bd0ea18 43
bc01119d 44static xfs_failaddr_t
693fc8f6 45xfs_dir3_block_verify(
a2ceac1f
DC
46 struct xfs_buf *bp)
47{
7861ef77 48 struct xfs_mount *mp = bp->b_mount;
693fc8f6
DC
49 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
50
68dbe77f
BF
51 if (!xfs_verify_magic(bp, hdr3->magic))
52 return __this_address;
53
94541a16 54 if (xfs_has_crc(mp)) {
9c4e12fb 55 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
bc01119d 56 return __this_address;
f1208396 57 if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
bc01119d 58 return __this_address;
a65d8d29 59 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
bc01119d 60 return __this_address;
a2ceac1f 61 }
a7e32f0d 62 return __xfs_dir3_data_check(NULL, bp);
a2ceac1f
DC
63}
64
65static void
693fc8f6 66xfs_dir3_block_read_verify(
a2ceac1f
DC
67 struct xfs_buf *bp)
68{
7861ef77 69 struct xfs_mount *mp = bp->b_mount;
1e697959 70 xfs_failaddr_t fa;
693fc8f6 71
b16a427a 72 if (xfs_has_crc(mp) &&
45922933 73 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
1e697959
DW
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 }
a2ceac1f
DC
80}
81
82static void
693fc8f6 83xfs_dir3_block_write_verify(
a2ceac1f
DC
84 struct xfs_buf *bp)
85{
7861ef77 86 struct xfs_mount *mp = bp->b_mount;
37d086ca 87 struct xfs_buf_log_item *bip = bp->b_log_item;
693fc8f6 88 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
1e697959 89 xfs_failaddr_t fa;
693fc8f6 90
1e697959
DW
91 fa = xfs_dir3_block_verify(bp);
92 if (fa) {
93 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
693fc8f6
DC
94 return;
95 }
96
b16a427a 97 if (!xfs_has_crc(mp))
693fc8f6
DC
98 return;
99
100 if (bip)
101 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
102
43b5aeed 103 xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
a2ceac1f
DC
104}
105
693fc8f6 106const struct xfs_buf_ops xfs_dir3_block_buf_ops = {
a3fac935 107 .name = "xfs_dir3_block",
68dbe77f
BF
108 .magic = { cpu_to_be32(XFS_DIR2_BLOCK_MAGIC),
109 cpu_to_be32(XFS_DIR3_BLOCK_MAGIC) },
693fc8f6
DC
110 .verify_read = xfs_dir3_block_read_verify,
111 .verify_write = xfs_dir3_block_write_verify,
95d9582b 112 .verify_struct = xfs_dir3_block_verify,
a2ceac1f
DC
113};
114
482abce5 115xfs_failaddr_t
f42e1ded 116xfs_dir3_block_header_check(
482abce5
DW
117 struct xfs_buf *bp,
118 xfs_ino_t owner)
f42e1ded 119{
482abce5 120 struct xfs_mount *mp = bp->b_mount;
f42e1ded 121
94541a16 122 if (xfs_has_crc(mp)) {
f42e1ded
DW
123 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
124
482abce5
DW
125 if (hdr3->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
126 return __this_address;
127
128 if (be64_to_cpu(hdr3->owner) != owner)
f42e1ded
DW
129 return __this_address;
130 }
131
132 return NULL;
133}
134
494434d7 135int
693fc8f6 136xfs_dir3_block_read(
a2ceac1f
DC
137 struct xfs_trans *tp,
138 struct xfs_inode *dp,
482abce5 139 xfs_ino_t owner,
a2ceac1f
DC
140 struct xfs_buf **bpp)
141{
142 struct xfs_mount *mp = dp->i_mount;
f42e1ded 143 xfs_failaddr_t fa;
8b4dc4a9 144 int err;
a2ceac1f 145
5f356ae6 146 err = xfs_da_read_buf(tp, dp, mp->m_dir_geo->datablk, 0, bpp,
693fc8f6 147 XFS_DATA_FORK, &xfs_dir3_block_buf_ops);
f42e1ded
DW
148 if (err || !*bpp)
149 return err;
150
151 /* Check things that we can't do in the verifier. */
482abce5 152 fa = xfs_dir3_block_header_check(*bpp, owner);
f42e1ded
DW
153 if (fa) {
154 __xfs_buf_mark_corrupt(*bpp, fa);
155 xfs_trans_brelse(tp, *bpp);
156 *bpp = NULL;
bcf1c967 157 xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
f42e1ded
DW
158 return -EFSCORRUPTED;
159 }
160
161 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF);
8b4dc4a9 162 return err;
693fc8f6
DC
163}
164
165static void
166xfs_dir3_block_init(
da833bae
DW
167 struct xfs_da_args *args,
168 struct xfs_buf *bp)
693fc8f6 169{
da833bae
DW
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;
693fc8f6
DC
174
175 bp->b_ops = &xfs_dir3_block_buf_ops;
bdc16ee5 176 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF);
693fc8f6 177
b16a427a 178 if (xfs_has_crc(mp)) {
693fc8f6
DC
179 memset(hdr3, 0, sizeof(*hdr3));
180 hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
f1208396 181 hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp));
da833bae 182 hdr3->owner = cpu_to_be64(args->owner);
9c4e12fb 183 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
693fc8f6
DC
184 return;
185
186 }
187 hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
a2ceac1f
DC
188}
189
190static void
191xfs_dir2_block_need_space(
ff105f75 192 struct xfs_inode *dp,
a2ceac1f
DC
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;
04f6f354 208 bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
a2ceac1f
DC
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 }
291out:
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 */
302static void
303xfs_dir2_block_compact(
ff105f75 304 struct xfs_da_args *args,
a2ceac1f
DC
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));
ff105f75 337 xfs_dir2_data_make_free(args, bp,
a2ceac1f
DC
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);
a2ceac1f
DC
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)
d85595d0 347 xfs_dir2_data_freescan(args->dp->i_mount, hdr, needlog);
a2ceac1f
DC
348}
349
2bd0ea18
NS
350/*
351 * Add an entry to a block directory.
352 */
353int /* error */
354xfs_dir2_block_addname(
355 xfs_da_args_t *args) /* directory op arguments */
356{
a2ceac1f 357 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 358 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
a2ceac1f 359 struct xfs_buf *bp; /* buffer for block */
2bd0ea18
NS
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 */
0e266570 366 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
2bd0ea18
NS
367 xfs_dahash_t hash; /* hash value of found entry */
368 int high; /* high index for binary srch */
369 int highstale; /* high stale index */
0e266570
NS
370 int lfloghigh=0; /* last final leaf to log */
371 int lfloglow=0; /* first final leaf to log */
2bd0ea18
NS
372 int len; /* length of the new entry */
373 int low; /* low index for binary srch */
374 int lowstale; /* low stale index */
0e266570 375 int mid=0; /* midpoint for binary srch */
2bd0ea18
NS
376 int needlog; /* need to log header */
377 int needscan; /* need to rescan freespace */
5e656dbb 378 __be16 *tagp; /* pointer to tag value */
2bd0ea18
NS
379 xfs_trans_t *tp; /* transaction structure */
380
56b2de80
DC
381 trace_xfs_dir2_block_addname(args);
382
2bd0ea18
NS
383 dp = args->dp;
384 tp = args->trans;
a2ceac1f
DC
385
386 /* Read the (one and only) directory block into bp. */
482abce5 387 error = xfs_dir3_block_read(tp, dp, args->owner, &bp);
a2ceac1f 388 if (error)
2bd0ea18 389 return error;
a2ceac1f 390
271a654f 391 len = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
a2ceac1f 392
2bd0ea18
NS
393 /*
394 * Set up pointers to parts of the block.
395 */
a2ceac1f 396 hdr = bp->b_addr;
ff105f75 397 btp = xfs_dir2_block_tail_p(args->geo, hdr);
5e656dbb 398 blp = xfs_dir2_block_leaf_p(btp);
a2ceac1f 399
2bd0ea18 400 /*
a2ceac1f
DC
401 * Find out if we can reuse stale entries or whether we need extra
402 * space for entry and new leaf.
2bd0ea18 403 */
ff105f75 404 xfs_dir2_block_need_space(dp, hdr, btp, blp, &tagp, &dup,
a2ceac1f
DC
405 &enddup, &compact, len);
406
2bd0ea18 407 /*
a2ceac1f 408 * Done everything we need for a space check now.
2bd0ea18 409 */
a2ceac1f
DC
410 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
411 xfs_trans_brelse(tp, bp);
412 if (!dup)
12b53197 413 return -ENOSPC;
a2ceac1f 414 return 0;
2bd0ea18 415 }
a2ceac1f 416
2bd0ea18
NS
417 /*
418 * If we don't have space for the new entry & leaf ...
419 */
420 if (!dup) {
a2ceac1f
DC
421 /* Don't have a space reservation: return no-space. */
422 if (args->total == 0)
12b53197 423 return -ENOSPC;
2bd0ea18
NS
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);
2bd0ea18
NS
429 if (error)
430 return error;
431 return xfs_dir2_leaf_addname(args);
432 }
a2ceac1f 433
2bd0ea18 434 needlog = needscan = 0;
a2ceac1f 435
2bd0ea18
NS
436 /*
437 * If need to compact the leaf entries, do it now.
2bd0ea18 438 */
49f693fa 439 if (compact) {
ff105f75 440 xfs_dir2_block_compact(args, bp, hdr, btp, blp, &needlog,
a2ceac1f 441 &lfloghigh, &lfloglow);
49f693fa
DC
442 /* recalculate blp post-compaction */
443 blp = xfs_dir2_block_leaf_p(btp);
444 } else if (btp->stale) {
2bd0ea18 445 /*
a2ceac1f
DC
446 * Set leaf logging boundaries to impossible state.
447 * For the no-stale case they're set explicitly.
2bd0ea18 448 */
5e656dbb 449 lfloglow = be32_to_cpu(btp->count);
2bd0ea18
NS
450 lfloghigh = -1;
451 }
a2ceac1f 452
2bd0ea18
NS
453 /*
454 * Find the slot that's first lower than our hash value, -1 if none.
455 */
5e656dbb 456 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
2bd0ea18 457 mid = (low + high) >> 1;
5e656dbb 458 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
2bd0ea18
NS
459 break;
460 if (hash < args->hashval)
461 low = mid + 1;
462 else
463 high = mid - 1;
464 }
5e656dbb 465 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
2bd0ea18
NS
466 mid--;
467 }
468 /*
469 * No stale entries, will use enddup space to hold new leaf.
470 */
46eca962 471 if (!btp->stale) {
15348e04
DW
472 xfs_dir2_data_aoff_t aoff;
473
2bd0ea18
NS
474 /*
475 * Mark the space needed for the new leaf entry, now in use.
476 */
15348e04
DW
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
2bd0ea18
NS
485 /*
486 * Update the tail (entry count).
487 */
5e656dbb 488 be32_add_cpu(&btp->count, 1);
2bd0ea18
NS
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) {
d85595d0 494 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
2bd0ea18
NS
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)
32181a02 505 memmove(blp, &blp[1], mid * sizeof(*blp));
2bd0ea18
NS
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 &&
a2ceac1f
DC
515 blp[lowstale].address !=
516 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
2bd0ea18
NS
517 lowstale--)
518 continue;
519 for (highstale = mid + 1;
5e656dbb 520 highstale < be32_to_cpu(btp->count) &&
a2ceac1f
DC
521 blp[highstale].address !=
522 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
2bd0ea18
NS
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 &&
5e656dbb 530 (highstale == be32_to_cpu(btp->count) ||
2bd0ea18
NS
531 mid - lowstale <= highstale - mid)) {
532 if (mid - lowstale)
32181a02 533 memmove(&blp[lowstale], &blp[lowstale + 1],
2bd0ea18 534 (mid - lowstale) * sizeof(*blp));
d7ff12b8
DC
535 lfloglow = min(lowstale, lfloglow);
536 lfloghigh = max(mid, lfloghigh);
2bd0ea18
NS
537 }
538 /*
539 * Move entries toward the high-numbered stale entry.
540 */
541 else {
5e656dbb 542 ASSERT(highstale < be32_to_cpu(btp->count));
2bd0ea18
NS
543 mid++;
544 if (highstale - mid)
32181a02 545 memmove(&blp[mid + 1], &blp[mid],
2bd0ea18 546 (highstale - mid) * sizeof(*blp));
d7ff12b8
DC
547 lfloglow = min(mid, lfloglow);
548 lfloghigh = max(highstale, lfloghigh);
2bd0ea18 549 }
5e656dbb 550 be32_add_cpu(&btp->stale, -1);
2bd0ea18
NS
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 */
5e656dbb 559 blp[mid].hashval = cpu_to_be32(args->hashval);
ff105f75 560 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
a2ceac1f 561 (char *)dep - (char *)hdr));
2bd0ea18
NS
562 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
563 /*
564 * Mark space for the data entry used.
565 */
15348e04
DW
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;
2bd0ea18
NS
571 /*
572 * Create the new data entry.
573 */
5e656dbb 574 dep->inumber = cpu_to_be64(args->inumber);
2bd0ea18 575 dep->namelen = args->namelen;
32181a02 576 memcpy(dep->name, args->name, args->namelen);
28e6e614 577 xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
823711f2 578 tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
a2ceac1f 579 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
2bd0ea18
NS
580 /*
581 * Clean up the bestfree array and log the header, tail, and entry.
582 */
583 if (needscan)
d85595d0 584 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
2bd0ea18 585 if (needlog)
ff105f75 586 xfs_dir2_data_log_header(args, bp);
2bd0ea18 587 xfs_dir2_block_log_tail(tp, bp);
ff105f75 588 xfs_dir2_data_log_entry(args, bp, dep);
90ea28c3 589 xfs_dir3_data_check(dp, bp);
2bd0ea18
NS
590 return 0;
591}
592
593/*
594 * Log leaf entries from the block.
595 */
5e656dbb 596static void
2bd0ea18
NS
597xfs_dir2_block_log_leaf(
598 xfs_trans_t *tp, /* transaction structure */
a2ceac1f 599 struct xfs_buf *bp, /* block buffer */
2bd0ea18
NS
600 int first, /* index of first logged leaf */
601 int last) /* index of last logged leaf */
602{
a2ceac1f
DC
603 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
604 xfs_dir2_leaf_entry_t *blp;
605 xfs_dir2_block_tail_t *btp;
2bd0ea18 606
ff105f75 607 btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
5e656dbb 608 blp = xfs_dir2_block_leaf_p(btp);
a2ceac1f
DC
609 xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
610 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
2bd0ea18
NS
611}
612
613/*
614 * Log the block tail.
615 */
5e656dbb 616static void
2bd0ea18
NS
617xfs_dir2_block_log_tail(
618 xfs_trans_t *tp, /* transaction structure */
a2ceac1f 619 struct xfs_buf *bp) /* block buffer */
2bd0ea18 620{
a2ceac1f
DC
621 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
622 xfs_dir2_block_tail_t *btp;
2bd0ea18 623
ff105f75 624 btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
a2ceac1f
DC
625 xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
626 (uint)((char *)(btp + 1) - (char *)hdr - 1));
2bd0ea18
NS
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 */
633int /* error */
634xfs_dir2_block_lookup(
635 xfs_da_args_t *args) /* dir lookup arguments */
636{
a2ceac1f 637 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 638 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
a2ceac1f 639 struct xfs_buf *bp; /* block buffer */
2bd0ea18
NS
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 */
2bd0ea18 645
56b2de80
DC
646 trace_xfs_dir2_block_lookup(args);
647
2bd0ea18
NS
648 /*
649 * Get the buffer, look up the entry.
650 * If not found (ENOENT) then return, have no buffer.
651 */
0e266570 652 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
2bd0ea18
NS
653 return error;
654 dp = args->dp;
a2ceac1f 655 hdr = bp->b_addr;
90ea28c3 656 xfs_dir3_data_check(dp, bp);
ff105f75 657 btp = xfs_dir2_block_tail_p(args->geo, hdr);
5e656dbb 658 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
659 /*
660 * Get the offset from the leaf entry, to point to the data.
661 */
a2ceac1f 662 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
ff105f75
DC
663 xfs_dir2_dataptr_to_off(args->geo,
664 be32_to_cpu(blp[ent].address)));
2bd0ea18 665 /*
5e656dbb 666 * Fill in inode number, CI name if appropriate, release the block.
2bd0ea18 667 */
5e656dbb 668 args->inumber = be64_to_cpu(dep->inumber);
28e6e614 669 args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
5e656dbb 670 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
a2ceac1f 671 xfs_trans_brelse(args->trans, bp);
1e68581b 672 return error;
2bd0ea18
NS
673}
674
675/*
676 * Internal block lookup routine.
677 */
5e656dbb 678static int /* error */
2bd0ea18
NS
679xfs_dir2_block_lookup_int(
680 xfs_da_args_t *args, /* dir lookup arguments */
a2ceac1f 681 struct xfs_buf **bpp, /* returned block buffer */
2bd0ea18
NS
682 int *entno) /* returned entry number */
683{
684 xfs_dir2_dataptr_t addr; /* data entry address */
a2ceac1f 685 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 686 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
a2ceac1f 687 struct xfs_buf *bp; /* block buffer */
2bd0ea18
NS
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 */
2bd0ea18 696 xfs_trans_t *tp; /* transaction pointer */
5e656dbb 697 enum xfs_dacmp cmp; /* comparison result */
2bd0ea18
NS
698
699 dp = args->dp;
700 tp = args->trans;
a2ceac1f 701
482abce5 702 error = xfs_dir3_block_read(tp, dp, args->owner, &bp);
a2ceac1f 703 if (error)
2bd0ea18 704 return error;
a2ceac1f
DC
705
706 hdr = bp->b_addr;
90ea28c3 707 xfs_dir3_data_check(dp, bp);
ff105f75 708 btp = xfs_dir2_block_tail_p(args->geo, hdr);
5e656dbb 709 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
710 /*
711 * Loop doing a binary search for our hash value.
712 * Find our entry, ENOENT if it's not there.
713 */
5e656dbb 714 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
2bd0ea18
NS
715 ASSERT(low <= high);
716 mid = (low + high) >> 1;
5e656dbb 717 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
2bd0ea18
NS
718 break;
719 if (hash < args->hashval)
720 low = mid + 1;
721 else
722 high = mid - 1;
723 if (low > high) {
5e656dbb 724 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
a2ceac1f 725 xfs_trans_brelse(tp, bp);
12b53197 726 return -ENOENT;
2bd0ea18
NS
727 }
728 }
729 /*
730 * Back up to the first one with the right hash value.
731 */
5e656dbb 732 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
2bd0ea18
NS
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 {
5e656dbb 740 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
2bd0ea18
NS
741 continue;
742 /*
743 * Get pointer to the entry from the leaf.
744 */
745 dep = (xfs_dir2_data_entry_t *)
ff105f75 746 ((char *)hdr + xfs_dir2_dataptr_to_off(args->geo, addr));
2bd0ea18 747 /*
5e656dbb
BN
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.
2bd0ea18 751 */
e169cc9b 752 cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
5e656dbb
BN
753 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
754 args->cmpresult = cmp;
2bd0ea18
NS
755 *bpp = bp;
756 *entno = mid;
5e656dbb
BN
757 if (cmp == XFS_CMP_EXACT)
758 return 0;
2bd0ea18 759 }
5e656dbb
BN
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;
2bd0ea18
NS
770 /*
771 * No match, release the buffer and return ENOENT.
772 */
a2ceac1f 773 xfs_trans_brelse(tp, bp);
12b53197 774 return -ENOENT;
2bd0ea18
NS
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 */
781int /* error */
782xfs_dir2_block_removename(
783 xfs_da_args_t *args) /* directory operation args */
784{
a2ceac1f 785 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 786 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
a2ceac1f 787 struct xfs_buf *bp; /* block buffer */
2bd0ea18
NS
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 */
2bd0ea18
NS
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
56b2de80
DC
799 trace_xfs_dir2_block_removename(args);
800
2bd0ea18
NS
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 */
0e266570 805 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
2bd0ea18
NS
806 return error;
807 }
808 dp = args->dp;
809 tp = args->trans;
a2ceac1f 810 hdr = bp->b_addr;
ff105f75 811 btp = xfs_dir2_block_tail_p(args->geo, hdr);
5e656dbb 812 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
813 /*
814 * Point to the data entry using the leaf entry.
815 */
ff105f75
DC
816 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
817 xfs_dir2_dataptr_to_off(args->geo,
818 be32_to_cpu(blp[ent].address)));
2bd0ea18
NS
819 /*
820 * Mark the data entry's space free.
821 */
822 needlog = needscan = 0;
ff105f75 823 xfs_dir2_data_make_free(args, bp,
a2ceac1f 824 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
271a654f
CH
825 xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
826 &needscan);
2bd0ea18
NS
827 /*
828 * Fix up the block tail.
829 */
5e656dbb 830 be32_add_cpu(&btp->stale, 1);
2bd0ea18
NS
831 xfs_dir2_block_log_tail(tp, bp);
832 /*
833 * Remove the leaf entry by marking it stale.
834 */
5e656dbb 835 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
2bd0ea18
NS
836 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
837 /*
838 * Fix up bestfree, log the header if necessary.
839 */
840 if (needscan)
d85595d0 841 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
2bd0ea18 842 if (needlog)
ff105f75 843 xfs_dir2_data_log_header(args, bp);
90ea28c3 844 xfs_dir3_data_check(dp, bp);
2bd0ea18
NS
845 /*
846 * See if the size as a shortform is good enough.
847 */
a2ceac1f 848 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
eae3e30d 849 if (size > xfs_inode_data_fork_size(dp))
2bd0ea18 850 return 0;
a2ceac1f 851
2bd0ea18
NS
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 */
862int /* error */
863xfs_dir2_block_replace(
864 xfs_da_args_t *args) /* directory operation args */
865{
a2ceac1f 866 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 867 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
a2ceac1f 868 struct xfs_buf *bp; /* block buffer */
2bd0ea18
NS
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 */
2bd0ea18 874
56b2de80
DC
875 trace_xfs_dir2_block_replace(args);
876
2bd0ea18
NS
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 */
0e266570 881 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
2bd0ea18
NS
882 return error;
883 }
884 dp = args->dp;
a2ceac1f 885 hdr = bp->b_addr;
ff105f75 886 btp = xfs_dir2_block_tail_p(args->geo, hdr);
5e656dbb 887 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
888 /*
889 * Point to the data entry we need to change.
890 */
ff105f75
DC
891 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
892 xfs_dir2_dataptr_to_off(args->geo,
893 be32_to_cpu(blp[ent].address)));
5e656dbb 894 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
2bd0ea18
NS
895 /*
896 * Change the inode number to the new value.
897 */
5e656dbb 898 dep->inumber = cpu_to_be64(args->inumber);
28e6e614 899 xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
ff105f75 900 xfs_dir2_data_log_entry(args, bp, dep);
90ea28c3 901 xfs_dir3_data_check(dp, bp);
2bd0ea18
NS
902 return 0;
903}
904
905/*
906 * Qsort comparison routine for the block leaf entries.
907 */
908static int /* sort order */
909xfs_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;
5e656dbb
BN
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);
2bd0ea18
NS
920}
921
922/*
923 * Convert a V2 leaf directory to a V2 block directory if possible.
924 */
925int /* error */
926xfs_dir2_leaf_to_block(
927 xfs_da_args_t *args, /* operation arguments */
a2ceac1f
DC
928 struct xfs_buf *lbp, /* leaf buffer */
929 struct xfs_buf *dbp) /* data buffer */
2bd0ea18 930{
5e656dbb 931 __be16 *bestsp; /* leaf bests table */
a2ceac1f 932 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18
NS
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 */
5e656dbb 946 __be16 *tagp; /* end of entry (tag) */
2bd0ea18
NS
947 int to; /* block/leaf to index */
948 xfs_trans_t *tp; /* transaction pointer */
65b80c98 949 struct xfs_dir3_icleaf_hdr leafhdr;
2bd0ea18 950
56b2de80
DC
951 trace_xfs_dir2_leaf_to_block(args);
952
2bd0ea18
NS
953 dp = args->dp;
954 tp = args->trans;
955 mp = dp->i_mount;
a2ceac1f 956 leaf = lbp->b_addr;
9db68faf 957 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
ff105f75 958 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
65b80c98
DC
959
960 ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
961 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
2bd0ea18
NS
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 */
509dcb4b 968 while (dp->i_disk_size > args->geo->blksize) {
693fc8f6
DC
969 int hdrsz;
970
58a1d356 971 hdrsz = args->geo->data_entry_offset;
5e656dbb
BN
972 bestsp = xfs_dir2_leaf_bests_p(ltp);
973 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
ff105f75 974 args->geo->blksize - hdrsz) {
0e266570 975 if ((error =
2bd0ea18 976 xfs_dir2_leaf_trim_data(args, lbp,
5e656dbb 977 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
a2ceac1f
DC
978 return error;
979 } else
980 return 0;
2bd0ea18
NS
981 }
982 /*
983 * Read the data block if we don't already have it, give up if it fails.
984 */
a2ceac1f 985 if (!dbp) {
72386ff0
DW
986 error = xfs_dir3_data_read(tp, dp, args->owner,
987 args->geo->datablk, 0, &dbp);
a2ceac1f
DC
988 if (error)
989 return error;
2bd0ea18 990 }
a2ceac1f 991 hdr = dbp->b_addr;
90ea28c3
DC
992 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
993 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
994
2bd0ea18
NS
995 /*
996 * Size of the "leaf" area in the block.
997 */
a2ceac1f 998 size = (uint)sizeof(xfs_dir2_block_tail_t) +
65b80c98 999 (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
2bd0ea18
NS
1000 /*
1001 * Look at the last data entry.
1002 */
ff105f75 1003 tagp = (__be16 *)((char *)hdr + args->geo->blksize) - 1;
a2ceac1f 1004 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
2bd0ea18
NS
1005 /*
1006 * If it's not free or is too short we can't do it.
1007 */
5e656dbb 1008 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
a2ceac1f
DC
1009 be16_to_cpu(dup->length) < size)
1010 return 0;
1011
2bd0ea18
NS
1012 /*
1013 * Start converting it to block form.
1014 */
da833bae 1015 xfs_dir3_block_init(args, dbp);
693fc8f6 1016
2bd0ea18
NS
1017 needlog = 1;
1018 needscan = 0;
1019 /*
1020 * Use up the space at the end of the block (blp/btp).
1021 */
15348e04
DW
1022 error = xfs_dir2_data_use_free(args, dbp, dup,
1023 args->geo->blksize - size, size, &needlog, &needscan);
1024 if (error)
1025 return error;
2bd0ea18
NS
1026 /*
1027 * Initialize the block tail.
1028 */
ff105f75 1029 btp = xfs_dir2_block_tail_p(args->geo, hdr);
65b80c98 1030 btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
46eca962 1031 btp->stale = 0;
2bd0ea18
NS
1032 xfs_dir2_block_log_tail(tp, dbp);
1033 /*
1034 * Initialize the block leaf area. We compact out stale entries.
1035 */
5e656dbb 1036 lep = xfs_dir2_block_leaf_p(btp);
65b80c98 1037 for (from = to = 0; from < leafhdr.count; from++) {
a2279497
CH
1038 if (leafhdr.ents[from].address ==
1039 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
2bd0ea18 1040 continue;
a2279497 1041 lep[to++] = leafhdr.ents[from];
2bd0ea18 1042 }
5e656dbb
BN
1043 ASSERT(to == be32_to_cpu(btp->count));
1044 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
2bd0ea18
NS
1045 /*
1046 * Scan the bestfree if we need it and log the data block header.
1047 */
1048 if (needscan)
d85595d0 1049 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
2bd0ea18 1050 if (needlog)
ff105f75 1051 xfs_dir2_data_log_header(args, dbp);
2bd0ea18
NS
1052 /*
1053 * Pitch the old leaf block.
1054 */
ff105f75 1055 error = xfs_da_shrink_inode(args, args->geo->leafblk, lbp);
a2ceac1f
DC
1056 if (error)
1057 return error;
1058
2bd0ea18
NS
1059 /*
1060 * Now see if the resulting block can be shrunken to shortform.
1061 */
a2ceac1f 1062 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
eae3e30d 1063 if (size > xfs_inode_data_fork_size(dp))
a2ceac1f
DC
1064 return 0;
1065
2bd0ea18 1066 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
2bd0ea18
NS
1067}
1068
1069/*
1070 * Convert the shortform directory to block form.
1071 */
1072int /* error */
1073xfs_dir2_sf_to_block(
26df2433 1074 struct xfs_da_args *args)
2bd0ea18 1075{
26df2433
CH
1076 struct xfs_trans *tp = args->trans;
1077 struct xfs_inode *dp = args->dp;
1078 struct xfs_mount *mp = dp->i_mount;
722e81c1 1079 struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
58a1d356 1080 struct xfs_da_geometry *geo = args->geo;
2bd0ea18 1081 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
a2ceac1f 1082 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 1083 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
a2ceac1f 1084 struct xfs_buf *bp; /* block buffer */
2bd0ea18 1085 xfs_dir2_block_tail_t *btp; /* block tail pointer */
2bd0ea18 1086 xfs_dir2_data_entry_t *dep; /* data entry pointer */
2bd0ea18
NS
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 */
2bd0ea18
NS
1092 int needlog; /* need to log block header */
1093 int needscan; /* need to scan block freespc */
1094 int newoffset; /* offset from current entry */
58a1d356 1095 unsigned int offset = geo->data_entry_offset;
2bd0ea18 1096 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
f8146197 1097 struct xfs_dir2_sf_hdr *oldsfp = ifp->if_data;
a2ceac1f 1098 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
5e656dbb 1099 __be16 *tagp; /* end of data entry */
5e656dbb 1100 struct xfs_name name;
2bd0ea18 1101
56b2de80
DC
1102 trace_xfs_dir2_sf_to_block(args);
1103
9b014ae4 1104 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
509dcb4b 1105 ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
a2ceac1f 1106
509dcb4b 1107 ASSERT(ifp->if_bytes == dp->i_disk_size);
f8146197 1108 ASSERT(oldsfp != NULL);
509dcb4b 1109 ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
87c472b7 1110 ASSERT(dp->i_df.if_nextents == 0);
a2ceac1f 1111
2bd0ea18 1112 /*
a2ceac1f 1113 * Copy the directory into a temporary buffer.
2bd0ea18
NS
1114 * Then pitch the incore inode data so we can make extents.
1115 */
52233984 1116 sfp = kmalloc(ifp->if_bytes, GFP_KERNEL | __GFP_NOFAIL);
3f17ed4b 1117 memcpy(sfp, oldsfp, ifp->if_bytes);
d663096d 1118
3f17ed4b 1119 xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK);
feee8e52 1120 xfs_bmap_local_to_extents_empty(tp, dp, XFS_DATA_FORK);
509dcb4b 1121 dp->i_disk_size = 0;
a2ceac1f 1122
2bd0ea18
NS
1123 /*
1124 * Add block 0 to the inode.
1125 */
1126 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
15348e04
DW
1127 if (error)
1128 goto out_free;
2bd0ea18 1129 /*
693fc8f6 1130 * Initialize the data block, then convert it to block format.
2bd0ea18 1131 */
693fc8f6 1132 error = xfs_dir3_data_init(args, blkno, &bp);
15348e04
DW
1133 if (error)
1134 goto out_free;
da833bae 1135 xfs_dir3_block_init(args, bp);
a2ceac1f 1136 hdr = bp->b_addr;
693fc8f6 1137
2bd0ea18
NS
1138 /*
1139 * Compute size of block "tail" area.
1140 */
1141 i = (uint)sizeof(*btp) +
a2ceac1f 1142 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
2bd0ea18
NS
1143 /*
1144 * The whole thing is initialized to free by the init routine.
1145 * Say we're using the leaf and tail area.
1146 */
9c036d76 1147 dup = bp->b_addr + offset;
2bd0ea18 1148 needlog = needscan = 0;
15348e04
DW
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;
2bd0ea18
NS
1153 ASSERT(needscan == 0);
1154 /*
1155 * Fill in the tail.
1156 */
ff105f75 1157 btp = xfs_dir2_block_tail_p(args->geo, hdr);
a2ceac1f 1158 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
46eca962 1159 btp->stale = 0;
5e656dbb 1160 blp = xfs_dir2_block_leaf_p(btp);
a2ceac1f 1161 endoffset = (uint)((char *)blp - (char *)hdr);
2bd0ea18
NS
1162 /*
1163 * Remove the freespace, we'll manage it.
1164 */
15348e04
DW
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;
26df2433 1170
2bd0ea18
NS
1171 /*
1172 * Create entry for .
1173 */
26df2433 1174 dep = bp->b_addr + offset;
da833bae 1175 dep->inumber = cpu_to_be64(args->owner);
2bd0ea18
NS
1176 dep->namelen = 1;
1177 dep->name[0] = '.';
28e6e614 1178 xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
823711f2 1179 tagp = xfs_dir2_data_entry_tag_p(mp, dep);
26df2433 1180 *tagp = cpu_to_be16(offset);
ff105f75 1181 xfs_dir2_data_log_entry(args, bp, dep);
5e656dbb 1182 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
26df2433 1183 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset));
271a654f 1184 offset += xfs_dir2_data_entsize(mp, dep->namelen);
26df2433 1185
2bd0ea18
NS
1186 /*
1187 * Create entry for ..
1188 */
26df2433 1189 dep = bp->b_addr + offset;
8a7190bd 1190 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
2bd0ea18
NS
1191 dep->namelen = 2;
1192 dep->name[0] = dep->name[1] = '.';
28e6e614 1193 xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
823711f2 1194 tagp = xfs_dir2_data_entry_tag_p(mp, dep);
26df2433 1195 *tagp = cpu_to_be16(offset);
ff105f75 1196 xfs_dir2_data_log_entry(args, bp, dep);
5e656dbb 1197 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
26df2433 1198 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset));
271a654f 1199 offset += xfs_dir2_data_entsize(mp, dep->namelen);
26df2433 1200
2bd0ea18
NS
1201 /*
1202 * Loop over existing entries, stuff them in.
1203 */
a2ceac1f
DC
1204 i = 0;
1205 if (!sfp->count)
2bd0ea18
NS
1206 sfep = NULL;
1207 else
5e656dbb 1208 sfep = xfs_dir2_sf_firstentry(sfp);
26df2433 1209
2bd0ea18
NS
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
5e656dbb 1221 newoffset = xfs_dir2_sf_get_offset(sfep);
2bd0ea18
NS
1222 /*
1223 * There should be a hole here, make one.
1224 */
1225 if (offset < newoffset) {
26df2433 1226 dup = bp->b_addr + offset;
5e656dbb
BN
1227 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1228 dup->length = cpu_to_be16(newoffset - offset);
26df2433 1229 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(offset);
ff105f75
DC
1230 xfs_dir2_data_log_unused(args, bp, dup);
1231 xfs_dir2_data_freeinsert(hdr,
04f6f354
CH
1232 xfs_dir2_data_bestfree_p(mp, hdr),
1233 dup, &dummy);
5e656dbb 1234 offset += be16_to_cpu(dup->length);
2bd0ea18
NS
1235 continue;
1236 }
1237 /*
1238 * Copy a real entry.
1239 */
26df2433 1240 dep = bp->b_addr + newoffset;
e96bd2d3 1241 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_ino(mp, sfp, sfep));
2bd0ea18 1242 dep->namelen = sfep->namelen;
28e6e614
CH
1243 xfs_dir2_data_put_ftype(mp, dep,
1244 xfs_dir2_sf_get_ftype(mp, sfep));
32181a02 1245 memcpy(dep->name, sfep->name, dep->namelen);
823711f2 1246 tagp = xfs_dir2_data_entry_tag_p(mp, dep);
26df2433 1247 *tagp = cpu_to_be16(newoffset);
ff105f75 1248 xfs_dir2_data_log_entry(args, bp, dep);
5e656dbb
BN
1249 name.name = sfep->name;
1250 name.len = sfep->namelen;
e169cc9b 1251 blp[2 + i].hashval = cpu_to_be32(xfs_dir2_hashname(mp, &name));
26df2433
CH
1252 blp[2 + i].address =
1253 cpu_to_be32(xfs_dir2_byte_to_dataptr(newoffset));
a2ceac1f
DC
1254 offset = (int)((char *)(tagp + 1) - (char *)hdr);
1255 if (++i == sfp->count)
2bd0ea18
NS
1256 sfep = NULL;
1257 else
660836c9 1258 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
2bd0ea18 1259 }
d663096d 1260 /* Done with the temporary buffer */
336b689a 1261 kfree(sfp);
2bd0ea18
NS
1262 /*
1263 * Sort the leaf entries by hash value.
1264 */
5e656dbb 1265 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
5000d01d 1266 /*
2bd0ea18
NS
1267 * Log the leaf entry area and tail.
1268 * Already logged the header in data_init, ignore needlog.
1269 */
1270 ASSERT(needscan == 0);
5e656dbb 1271 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
2bd0ea18 1272 xfs_dir2_block_log_tail(tp, bp);
90ea28c3 1273 xfs_dir3_data_check(dp, bp);
2bd0ea18 1274 return 0;
15348e04 1275out_free:
336b689a 1276 kfree(sfp);
15348e04 1277 return error;
2bd0ea18 1278}