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