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