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