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