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 2260219 : xfs_dir3_block_verify(
49 : struct xfs_buf *bp)
50 : {
51 2260219 : struct xfs_mount *mp = bp->b_mount;
52 2260219 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
53 :
54 2260219 : if (!xfs_verify_magic(bp, hdr3->magic))
55 0 : return __this_address;
56 :
57 2260218 : if (xfs_has_crc(mp)) {
58 2260217 : if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
59 0 : return __this_address;
60 2260219 : if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
61 0 : return __this_address;
62 2260220 : if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
63 0 : return __this_address;
64 : }
65 2260223 : return __xfs_dir3_data_check(NULL, bp);
66 : }
67 :
68 : static void
69 544366 : xfs_dir3_block_read_verify(
70 : struct xfs_buf *bp)
71 : {
72 544366 : struct xfs_mount *mp = bp->b_mount;
73 544366 : xfs_failaddr_t fa;
74 :
75 1088732 : 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 544360 : fa = xfs_dir3_block_verify(bp);
80 544360 : if (fa)
81 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
82 : }
83 544366 : }
84 :
85 : static void
86 1445114 : xfs_dir3_block_write_verify(
87 : struct xfs_buf *bp)
88 : {
89 1445114 : struct xfs_mount *mp = bp->b_mount;
90 1445114 : struct xfs_buf_log_item *bip = bp->b_log_item;
91 1445114 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
92 1445114 : xfs_failaddr_t fa;
93 :
94 1445114 : fa = xfs_dir3_block_verify(bp);
95 1445114 : if (fa) {
96 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
97 0 : return;
98 : }
99 :
100 1445114 : if (!xfs_has_crc(mp))
101 : return;
102 :
103 1445112 : if (bip)
104 1445112 : hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
105 :
106 1445112 : 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 47216220 : xfs_dir3_block_header_check(
120 : struct xfs_buf *bp,
121 : xfs_ino_t owner)
122 : {
123 47216220 : struct xfs_mount *mp = bp->b_mount;
124 :
125 47216220 : if (xfs_has_crc(mp)) {
126 47215878 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
127 :
128 47215878 : ASSERT(hdr3->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
129 :
130 47215878 : 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 47219062 : 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 47219062 : struct xfs_mount *mp = dp->i_mount;
148 47219062 : xfs_failaddr_t fa;
149 47219062 : int err;
150 :
151 47219062 : err = xfs_da_read_buf(tp, dp, mp->m_dir_geo->datablk, 0, bpp,
152 : XFS_DATA_FORK, &xfs_dir3_block_buf_ops);
153 47217470 : if (err || !*bpp)
154 : return err;
155 :
156 : /* Check things that we can't do in the verifier. */
157 47217392 : fa = xfs_dir3_block_header_check(*bpp, owner);
158 47218136 : 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 47218136 : xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF);
167 47218136 : return err;
168 : }
169 :
170 : static void
171 315812 : xfs_dir3_block_init(
172 : struct xfs_da_args *args,
173 : struct xfs_buf *bp)
174 : {
175 315812 : struct xfs_trans *tp = args->trans;
176 315812 : struct xfs_inode *dp = args->dp;
177 315812 : struct xfs_mount *mp = dp->i_mount;
178 315812 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
179 :
180 315812 : bp->b_ops = &xfs_dir3_block_buf_ops;
181 315812 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF);
182 :
183 315812 : if (xfs_has_crc(mp)) {
184 315808 : memset(hdr3, 0, sizeof(*hdr3));
185 315808 : hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
186 315808 : hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp));
187 315808 : hdr3->owner = cpu_to_be64(args->owner);
188 315808 : uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
189 315808 : return;
190 :
191 : }
192 4 : hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
193 : }
194 :
195 : static void
196 2586025 : 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 2586025 : struct xfs_dir2_data_free *bf;
208 2586025 : __be16 *tagp = NULL;
209 2586025 : struct xfs_dir2_data_unused *dup = NULL;
210 2586025 : struct xfs_dir2_data_unused *enddup = NULL;
211 :
212 2586025 : *compact = 0;
213 2586025 : 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 2585894 : if (btp->stale) {
219 1034889 : if (be16_to_cpu(bf[0].length) >= len) {
220 : /*
221 : * The biggest entry enough to avoid compaction.
222 : */
223 2069776 : dup = (xfs_dir2_data_unused_t *)
224 1034888 : ((char *)hdr + be16_to_cpu(bf[0].offset));
225 1034888 : 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 1551005 : tagp = (__be16 *)blp - 1;
258 :
259 : /* Data object just before the first leaf entry. */
260 1551005 : 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 1551005 : 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 3096958 : dup = (xfs_dir2_data_unused_t *)
272 1548479 : ((char *)hdr + be16_to_cpu(bf[0].offset));
273 1548479 : if (dup != enddup) {
274 : /*
275 : * Not the same free entry, just check its length.
276 : */
277 45343 : if (be16_to_cpu(dup->length) < len)
278 5 : dup = NULL;
279 45343 : goto out;
280 : }
281 :
282 : /*
283 : * It is the biggest freespace, can it hold the leaf too?
284 : */
285 1503136 : if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
286 : /*
287 : * Yes, use the second-largest entry instead if it works.
288 : */
289 2364 : 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 1503298 : out:
297 2585894 : *tagpp = tagp;
298 2585894 : *dupp = dup;
299 2585894 : *enddupp = enddup;
300 2585894 : }
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 2586011 : xfs_dir2_block_addname(
360 : xfs_da_args_t *args) /* directory op arguments */
361 : {
362 2586011 : xfs_dir2_data_hdr_t *hdr; /* block header */
363 2586011 : xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
364 2586011 : struct xfs_buf *bp; /* buffer for block */
365 2586011 : xfs_dir2_block_tail_t *btp; /* block tail */
366 2586011 : int compact; /* need to compact leaf ents */
367 2586011 : xfs_dir2_data_entry_t *dep; /* block data entry */
368 2586011 : xfs_inode_t *dp; /* directory inode */
369 2586011 : xfs_dir2_data_unused_t *dup; /* block unused entry */
370 2586011 : int error; /* error return value */
371 2586011 : xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
372 2586011 : xfs_dahash_t hash; /* hash value of found entry */
373 2586011 : int high; /* high index for binary srch */
374 2586011 : int highstale; /* high stale index */
375 2586011 : int lfloghigh=0; /* last final leaf to log */
376 2586011 : int lfloglow=0; /* first final leaf to log */
377 2586011 : int len; /* length of the new entry */
378 2586011 : int low; /* low index for binary srch */
379 2586011 : int lowstale; /* low stale index */
380 2586011 : int mid=0; /* midpoint for binary srch */
381 2586011 : int needlog; /* need to log header */
382 2586011 : int needscan; /* need to rescan freespace */
383 2586011 : __be16 *tagp; /* pointer to tag value */
384 2586011 : xfs_trans_t *tp; /* transaction structure */
385 :
386 2586011 : trace_xfs_dir2_block_addname(args);
387 :
388 2586005 : dp = args->dp;
389 2586005 : tp = args->trans;
390 :
391 : /* Read the (one and only) directory block into bp. */
392 2586005 : error = xfs_dir3_block_read(tp, dp, args->owner, &bp);
393 2586066 : if (error)
394 : return error;
395 :
396 2586064 : len = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
397 :
398 : /*
399 : * Set up pointers to parts of the block.
400 : */
401 2586064 : hdr = bp->b_addr;
402 2586064 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
403 2586064 : 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 2586064 : 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 2585956 : 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 2585956 : if (!dup) {
426 : /* Don't have a space reservation: return no-space. */
427 4951 : 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 4951 : error = xfs_dir2_block_to_leaf(args, bp);
434 4952 : if (error)
435 : return error;
436 4951 : return xfs_dir2_leaf_addname(args);
437 : }
438 :
439 2581005 : needlog = needscan = 0;
440 :
441 : /*
442 : * If need to compact the leaf entries, do it now.
443 : */
444 2581005 : 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 2581005 : } 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 1034885 : lfloglow = be32_to_cpu(btp->count);
455 1034885 : lfloghigh = -1;
456 : }
457 :
458 : /*
459 : * Find the slot that's first lower than our hash value, -1 if none.
460 : */
461 19366739 : for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
462 14243570 : mid = (low + high) >> 1;
463 28487140 : if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
464 : break;
465 14204729 : if (hash < args->hashval)
466 9854144 : low = mid + 1;
467 : else
468 4350585 : high = mid - 1;
469 : }
470 7869403 : while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
471 1353730 : mid--;
472 : }
473 : /*
474 : * No stale entries, will use enddup space to hold new leaf.
475 : */
476 2581005 : if (!btp->stale) {
477 1546118 : xfs_dir2_data_aoff_t aoff;
478 :
479 : /*
480 : * Mark the space needed for the new leaf entry, now in use.
481 : */
482 1546118 : aoff = (xfs_dir2_data_aoff_t)((char *)enddup - (char *)hdr +
483 1546118 : be16_to_cpu(enddup->length) - sizeof(*blp));
484 1546118 : error = xfs_dir2_data_use_free(args, bp, enddup, aoff,
485 : (xfs_dir2_data_aoff_t)sizeof(*blp), &needlog,
486 : &needscan);
487 1545997 : if (error)
488 : return error;
489 :
490 : /*
491 : * Update the tail (entry count).
492 : */
493 1545987 : 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 1545994 : if (needscan) {
499 9880 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
500 9880 : 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 1545994 : blp--;
508 1545994 : mid++;
509 1545994 : if (mid)
510 3092464 : memmove(blp, &blp[1], mid * sizeof(*blp));
511 1545994 : lfloglow = 0;
512 1545994 : lfloghigh = mid;
513 : }
514 : /*
515 : * Use a stale leaf for our new entry.
516 : */
517 : else {
518 : for (lowstale = mid;
519 17999677 : lowstale >= 0 &&
520 17892388 : blp[lowstale].address !=
521 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
522 16964790 : lowstale--)
523 16964790 : continue;
524 1034887 : for (highstale = mid + 1;
525 0 : highstale < be32_to_cpu(btp->count) &&
526 3445115 : blp[highstale].address !=
527 11168781 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
528 2792785 : (lowstale < 0 || mid - lowstale > highstale - mid);
529 2955585 : highstale++)
530 2955585 : continue;
531 : /*
532 : * Move entries toward the low-numbered stale entry.
533 : */
534 1034887 : if (lowstale >= 0 &&
535 382242 : (highstale == be32_to_cpu(btp->count) ||
536 382242 : mid - lowstale <= highstale - mid)) {
537 818794 : if (mid - lowstale)
538 1443830 : memmove(&blp[lowstale], &blp[lowstale + 1],
539 : (mid - lowstale) * sizeof(*blp));
540 818794 : lfloglow = min(lowstale, lfloglow);
541 818794 : lfloghigh = max(mid, lfloghigh);
542 : }
543 : /*
544 : * Move entries toward the high-numbered stale entry.
545 : */
546 : else {
547 216093 : ASSERT(highstale < be32_to_cpu(btp->count));
548 216093 : mid++;
549 216093 : if (highstale - mid)
550 302584 : memmove(&blp[mid + 1], &blp[mid],
551 : (highstale - mid) * sizeof(*blp));
552 216093 : lfloglow = min(mid, lfloglow);
553 216093 : lfloghigh = max(highstale, lfloghigh);
554 : }
555 1034887 : be32_add_cpu(&btp->stale, -1);
556 : }
557 : /*
558 : * Point to the new data entry.
559 : */
560 2580972 : dep = (xfs_dir2_data_entry_t *)dup;
561 : /*
562 : * Fill in the leaf entry.
563 : */
564 2580972 : blp[mid].hashval = cpu_to_be32(args->hashval);
565 2580972 : blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
566 : (char *)dep - (char *)hdr));
567 2580972 : xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
568 : /*
569 : * Mark space for the data entry used.
570 : */
571 2580789 : 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 2581046 : if (error)
575 : return error;
576 : /*
577 : * Create the new data entry.
578 : */
579 2581046 : dep->inumber = cpu_to_be64(args->inumber);
580 2581046 : dep->namelen = args->namelen;
581 5162092 : memcpy(dep->name, args->name, args->namelen);
582 2581046 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
583 2581055 : tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
584 2580893 : *tagp = cpu_to_be16((char *)dep - (char *)hdr);
585 : /*
586 : * Clean up the bestfree array and log the header, tail, and entry.
587 : */
588 2580893 : if (needscan)
589 324124 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
590 2580893 : if (needlog)
591 2580787 : xfs_dir2_data_log_header(args, bp);
592 2580724 : xfs_dir2_block_log_tail(tp, bp);
593 2580783 : xfs_dir2_data_log_entry(args, bp, dep);
594 2580767 : xfs_dir3_data_check(dp, bp);
595 2580767 : return 0;
596 : }
597 :
598 : /*
599 : * Log leaf entries from the block.
600 : */
601 : static void
602 4587340 : 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 4587340 : xfs_dir2_data_hdr_t *hdr = bp->b_addr;
609 4587340 : xfs_dir2_leaf_entry_t *blp;
610 4587340 : xfs_dir2_block_tail_t *btp;
611 :
612 4587340 : btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
613 4587340 : blp = xfs_dir2_block_leaf_p(btp);
614 4587340 : xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
615 4587340 : (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
616 4587257 : }
617 :
618 : /*
619 : * Log the block tail.
620 : */
621 : static void
622 4587067 : xfs_dir2_block_log_tail(
623 : xfs_trans_t *tp, /* transaction structure */
624 : struct xfs_buf *bp) /* block buffer */
625 : {
626 4587067 : xfs_dir2_data_hdr_t *hdr = bp->b_addr;
627 4587067 : xfs_dir2_block_tail_t *btp;
628 :
629 4587067 : btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
630 4587067 : xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
631 4587067 : (uint)((char *)(btp + 1) - (char *)hdr - 1));
632 4587117 : }
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 32165193 : xfs_dir2_block_lookup(
640 : xfs_da_args_t *args) /* dir lookup arguments */
641 : {
642 32165193 : xfs_dir2_data_hdr_t *hdr; /* block header */
643 32165193 : xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
644 32165193 : struct xfs_buf *bp; /* block buffer */
645 32165193 : xfs_dir2_block_tail_t *btp; /* block tail */
646 32165193 : xfs_dir2_data_entry_t *dep; /* block data entry */
647 32165193 : xfs_inode_t *dp; /* incore inode */
648 32165193 : int ent; /* entry index */
649 32165193 : int error; /* error return value */
650 :
651 32165193 : 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 32165163 : if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
658 : return error;
659 27959019 : dp = args->dp;
660 27959019 : hdr = bp->b_addr;
661 27959019 : xfs_dir3_data_check(dp, bp);
662 27959271 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
663 27959271 : blp = xfs_dir2_block_leaf_p(btp);
664 : /*
665 : * Get the offset from the leaf entry, to point to the data.
666 : */
667 55918542 : dep = (xfs_dir2_data_entry_t *)((char *)hdr +
668 55918542 : xfs_dir2_dataptr_to_off(args->geo,
669 27959271 : be32_to_cpu(blp[ent].address)));
670 : /*
671 : * Fill in inode number, CI name if appropriate, release the block.
672 : */
673 27959271 : args->inumber = be64_to_cpu(dep->inumber);
674 27959271 : args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
675 27959036 : error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
676 27959122 : xfs_trans_brelse(args->trans, bp);
677 27959122 : return error;
678 : }
679 :
680 : /*
681 : * Internal block lookup routine.
682 : */
683 : static int /* error */
684 34735189 : 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 34735189 : xfs_dir2_dataptr_t addr; /* data entry address */
690 34735189 : xfs_dir2_data_hdr_t *hdr; /* block header */
691 34735189 : xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
692 34735189 : struct xfs_buf *bp; /* block buffer */
693 34735189 : xfs_dir2_block_tail_t *btp; /* block tail */
694 34735189 : xfs_dir2_data_entry_t *dep; /* block data entry */
695 34735189 : xfs_inode_t *dp; /* incore inode */
696 34735189 : int error; /* error return value */
697 34735189 : xfs_dahash_t hash; /* found hash value */
698 34735189 : int high; /* binary search high index */
699 34735189 : int low; /* binary search low index */
700 34735189 : int mid; /* binary search current idx */
701 34735189 : xfs_trans_t *tp; /* transaction pointer */
702 34735189 : enum xfs_dacmp cmp; /* comparison result */
703 :
704 34735189 : dp = args->dp;
705 34735189 : tp = args->trans;
706 :
707 34735189 : error = xfs_dir3_block_read(tp, dp, args->owner, &bp);
708 34735412 : if (error)
709 : return error;
710 :
711 34735368 : hdr = bp->b_addr;
712 34735368 : xfs_dir3_data_check(dp, bp);
713 34736811 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
714 34736811 : 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 34736811 : for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
720 139606335 : ASSERT(low <= high);
721 139607043 : mid = (low + high) >> 1;
722 279214086 : if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
723 : break;
724 109067315 : if (hash < args->hashval)
725 73474179 : low = mid + 1;
726 : else
727 35593136 : high = mid - 1;
728 109067315 : if (low > high) {
729 4197791 : ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
730 4197791 : xfs_trans_brelse(tp, bp);
731 4197791 : return -ENOENT;
732 : }
733 : }
734 : /*
735 : * Back up to the first one with the right hash value.
736 : */
737 61178474 : 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 31207564 : do {
745 62415128 : if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
746 59180 : continue;
747 : /*
748 : * Get pointer to the entry from the leaf.
749 : */
750 31148384 : dep = (xfs_dir2_data_entry_t *)
751 31148384 : ((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 31148384 : cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
758 31148266 : if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
759 30530771 : args->cmpresult = cmp;
760 30530771 : *bpp = bp;
761 30530771 : *entno = mid;
762 30530771 : if (cmp == XFS_CMP_EXACT)
763 : return 0;
764 : }
765 2034364 : } while (++mid < be32_to_cpu(btp->count) &&
766 670024 : be32_to_cpu(blp[mid].hashval) == hash);
767 :
768 14334 : 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 14334 : if (args->cmpresult == XFS_CMP_CASE)
774 : return 0;
775 : /*
776 : * No match, release the buffer and return ENOENT.
777 : */
778 8869 : xfs_trans_brelse(tp, bp);
779 8869 : 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 1690683 : xfs_dir2_block_removename(
788 : xfs_da_args_t *args) /* directory operation args */
789 : {
790 1690683 : xfs_dir2_data_hdr_t *hdr; /* block header */
791 1690683 : xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
792 1690683 : struct xfs_buf *bp; /* block buffer */
793 1690683 : xfs_dir2_block_tail_t *btp; /* block tail */
794 1690683 : xfs_dir2_data_entry_t *dep; /* block data entry */
795 1690683 : xfs_inode_t *dp; /* incore inode */
796 1690683 : int ent; /* block leaf entry index */
797 1690683 : int error; /* error return value */
798 1690683 : int needlog; /* need to log block header */
799 1690683 : int needscan; /* need to fixup bestfree */
800 1690683 : xfs_dir2_sf_hdr_t sfh; /* shortform header */
801 1690683 : int size; /* shortform size */
802 1690683 : xfs_trans_t *tp; /* transaction pointer */
803 :
804 1690683 : 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 1690682 : if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
811 : return error;
812 : }
813 1690671 : dp = args->dp;
814 1690671 : tp = args->trans;
815 1690671 : hdr = bp->b_addr;
816 1690671 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
817 1690671 : blp = xfs_dir2_block_leaf_p(btp);
818 : /*
819 : * Point to the data entry using the leaf entry.
820 : */
821 3381342 : dep = (xfs_dir2_data_entry_t *)((char *)hdr +
822 3381342 : xfs_dir2_dataptr_to_off(args->geo,
823 1690671 : be32_to_cpu(blp[ent].address)));
824 : /*
825 : * Mark the data entry's space free.
826 : */
827 1690671 : needlog = needscan = 0;
828 1690671 : xfs_dir2_data_make_free(args, bp,
829 : (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
830 1690671 : xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
831 : &needscan);
832 : /*
833 : * Fix up the block tail.
834 : */
835 1690671 : be32_add_cpu(&btp->stale, 1);
836 1690671 : xfs_dir2_block_log_tail(tp, bp);
837 : /*
838 : * Remove the leaf entry by marking it stale.
839 : */
840 1690677 : blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
841 1690677 : xfs_dir2_block_log_leaf(tp, bp, ent, ent);
842 : /*
843 : * Fix up bestfree, log the header if necessary.
844 : */
845 1690649 : if (needscan)
846 372662 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
847 1690648 : if (needlog)
848 848303 : xfs_dir2_data_log_header(args, bp);
849 1690647 : xfs_dir3_data_check(dp, bp);
850 : /*
851 : * See if the size as a shortform is good enough.
852 : */
853 1690674 : size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
854 1690677 : if (size > xfs_inode_data_fork_size(dp))
855 : return 0;
856 :
857 : /*
858 : * If it works, do the conversion.
859 : */
860 214305 : 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 881053 : xfs_dir2_block_replace(
869 : xfs_da_args_t *args) /* directory operation args */
870 : {
871 881053 : xfs_dir2_data_hdr_t *hdr; /* block header */
872 881053 : xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
873 881053 : struct xfs_buf *bp; /* block buffer */
874 881053 : xfs_dir2_block_tail_t *btp; /* block tail */
875 881053 : xfs_dir2_data_entry_t *dep; /* block data entry */
876 881053 : xfs_inode_t *dp; /* incore inode */
877 881053 : int ent; /* leaf entry index */
878 881053 : int error; /* error return value */
879 :
880 881053 : 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 881053 : if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
887 : return error;
888 : }
889 881051 : dp = args->dp;
890 881051 : hdr = bp->b_addr;
891 881051 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
892 881051 : blp = xfs_dir2_block_leaf_p(btp);
893 : /*
894 : * Point to the data entry we need to change.
895 : */
896 1762102 : dep = (xfs_dir2_data_entry_t *)((char *)hdr +
897 1762102 : xfs_dir2_dataptr_to_off(args->geo,
898 881051 : be32_to_cpu(blp[ent].address)));
899 881051 : ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
900 : /*
901 : * Change the inode number to the new value.
902 : */
903 881051 : dep->inumber = cpu_to_be64(args->inumber);
904 881051 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
905 881051 : xfs_dir2_data_log_entry(args, bp, dep);
906 881051 : xfs_dir3_data_check(dp, bp);
907 881051 : return 0;
908 : }
909 :
910 : /*
911 : * Qsort comparison routine for the block leaf entries.
912 : */
913 : static int /* sort order */
914 19408183 : xfs_dir2_block_sort(
915 : const void *a, /* first leaf entry */
916 : const void *b) /* second leaf entry */
917 : {
918 19408183 : const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
919 19408183 : const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
920 :
921 19408183 : la = a;
922 19408183 : lb = b;
923 58224549 : return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
924 7500962 : (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 4612538 : 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 4612538 : __be16 *bestsp; /* leaf bests table */
937 4612538 : xfs_dir2_data_hdr_t *hdr; /* block header */
938 4612538 : xfs_dir2_block_tail_t *btp; /* block tail */
939 4612538 : xfs_inode_t *dp; /* incore directory inode */
940 4612538 : xfs_dir2_data_unused_t *dup; /* unused data entry */
941 4612538 : int error; /* error return value */
942 4612538 : int from; /* leaf from index */
943 4612538 : xfs_dir2_leaf_t *leaf; /* leaf structure */
944 4612538 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
945 4612538 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
946 4612538 : xfs_mount_t *mp; /* file system mount point */
947 4612538 : int needlog; /* need to log data header */
948 4612538 : int needscan; /* need to scan for bestfree */
949 4612538 : xfs_dir2_sf_hdr_t sfh; /* shortform header */
950 4612538 : int size; /* bytes used */
951 4612538 : __be16 *tagp; /* end of entry (tag) */
952 4612538 : int to; /* block/leaf to index */
953 4612538 : xfs_trans_t *tp; /* transaction pointer */
954 4612538 : struct xfs_dir3_icleaf_hdr leafhdr;
955 :
956 4612538 : trace_xfs_dir2_leaf_to_block(args);
957 :
958 4612694 : dp = args->dp;
959 4612694 : tp = args->trans;
960 4612694 : mp = dp->i_mount;
961 4612694 : leaf = lbp->b_addr;
962 4612694 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
963 4612607 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
964 :
965 4612607 : 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 4612728 : while (dp->i_disk_size > args->geo->blksize) {
974 3354143 : int hdrsz;
975 :
976 3354143 : hdrsz = args->geo->data_entry_offset;
977 3354143 : bestsp = xfs_dir2_leaf_bests_p(ltp);
978 3354143 : if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
979 3354143 : args->geo->blksize - hdrsz) {
980 121 : 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 1258585 : if (!dbp) {
991 2092 : error = xfs_dir3_data_read(tp, dp, args->owner,
992 : args->geo->datablk, 0, &dbp);
993 2092 : if (error)
994 : return error;
995 : }
996 1258585 : hdr = dbp->b_addr;
997 1258585 : 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 1258585 : size = (uint)sizeof(xfs_dir2_block_tail_t) +
1004 1258585 : (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
1005 : /*
1006 : * Look at the last data entry.
1007 : */
1008 1258585 : tagp = (__be16 *)((char *)hdr + args->geo->blksize) - 1;
1009 1258585 : 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 2516794 : if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
1014 1258209 : be16_to_cpu(dup->length) < size)
1015 : return 0;
1016 :
1017 : /*
1018 : * Start converting it to block form.
1019 : */
1020 4103 : xfs_dir3_block_init(args, dbp);
1021 :
1022 4103 : needlog = 1;
1023 4103 : needscan = 0;
1024 : /*
1025 : * Use up the space at the end of the block (blp/btp).
1026 : */
1027 4103 : error = xfs_dir2_data_use_free(args, dbp, dup,
1028 4103 : args->geo->blksize - size, size, &needlog, &needscan);
1029 4103 : if (error)
1030 : return error;
1031 : /*
1032 : * Initialize the block tail.
1033 : */
1034 4103 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
1035 4103 : btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
1036 4103 : btp->stale = 0;
1037 4103 : xfs_dir2_block_log_tail(tp, dbp);
1038 : /*
1039 : * Initialize the block leaf area. We compact out stale entries.
1040 : */
1041 4103 : lep = xfs_dir2_block_leaf_p(btp);
1042 1076011 : for (from = to = 0; from < leafhdr.count; from++) {
1043 1071908 : if (leafhdr.ents[from].address ==
1044 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
1045 999225 : continue;
1046 72683 : lep[to++] = leafhdr.ents[from];
1047 : }
1048 8206 : ASSERT(to == be32_to_cpu(btp->count));
1049 4103 : 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 4103 : if (needscan)
1054 1185 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1055 4103 : if (needlog)
1056 4103 : xfs_dir2_data_log_header(args, dbp);
1057 : /*
1058 : * Pitch the old leaf block.
1059 : */
1060 4103 : error = xfs_da_shrink_inode(args, args->geo->leafblk, lbp);
1061 4103 : if (error)
1062 : return error;
1063 :
1064 : /*
1065 : * Now see if the resulting block can be shrunken to shortform.
1066 : */
1067 4103 : size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
1068 4103 : if (size > xfs_inode_data_fork_size(dp))
1069 : return 0;
1070 :
1071 2973 : 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 311709 : xfs_dir2_sf_to_block(
1079 : struct xfs_da_args *args)
1080 : {
1081 311709 : struct xfs_trans *tp = args->trans;
1082 311709 : struct xfs_inode *dp = args->dp;
1083 311709 : struct xfs_mount *mp = dp->i_mount;
1084 311709 : struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
1085 311709 : struct xfs_da_geometry *geo = args->geo;
1086 311709 : xfs_dir2_db_t blkno; /* dir-relative block # (0) */
1087 311709 : xfs_dir2_data_hdr_t *hdr; /* block header */
1088 311709 : xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1089 311709 : struct xfs_buf *bp; /* block buffer */
1090 311709 : xfs_dir2_block_tail_t *btp; /* block tail pointer */
1091 311709 : xfs_dir2_data_entry_t *dep; /* data entry pointer */
1092 311709 : int dummy; /* trash */
1093 311709 : xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1094 311709 : int endoffset; /* end of data objects */
1095 311709 : int error; /* error return value */
1096 311709 : int i; /* index */
1097 311709 : int needlog; /* need to log block header */
1098 311709 : int needscan; /* need to scan block freespc */
1099 311709 : int newoffset; /* offset from current entry */
1100 311709 : unsigned int offset = geo->data_entry_offset;
1101 311709 : xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
1102 311709 : xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1103 311709 : xfs_dir2_sf_hdr_t *sfp; /* shortform header */
1104 311709 : __be16 *tagp; /* end of data entry */
1105 311709 : struct xfs_name name;
1106 :
1107 311709 : trace_xfs_dir2_sf_to_block(args);
1108 :
1109 311709 : ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
1110 311709 : ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
1111 :
1112 311709 : oldsfp = (xfs_dir2_sf_hdr_t *)ifp->if_u1.if_data;
1113 :
1114 311709 : ASSERT(ifp->if_bytes == dp->i_disk_size);
1115 311709 : ASSERT(ifp->if_u1.if_data != NULL);
1116 311709 : ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1117 311709 : 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 311709 : sfp = kmem_alloc(ifp->if_bytes, 0);
1124 623418 : memcpy(sfp, oldsfp, ifp->if_bytes);
1125 :
1126 311709 : xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK);
1127 311708 : xfs_bmap_local_to_extents_empty(tp, dp, XFS_DATA_FORK);
1128 311708 : dp->i_disk_size = 0;
1129 :
1130 : /*
1131 : * Add block 0 to the inode.
1132 : */
1133 311708 : error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1134 311709 : if (error)
1135 0 : goto out_free;
1136 : /*
1137 : * Initialize the data block, then convert it to block format.
1138 : */
1139 311709 : error = xfs_dir3_data_init(args, blkno, &bp);
1140 311709 : if (error)
1141 0 : goto out_free;
1142 311709 : xfs_dir3_block_init(args, bp);
1143 311708 : hdr = bp->b_addr;
1144 :
1145 : /*
1146 : * Compute size of block "tail" area.
1147 : */
1148 311708 : i = (uint)sizeof(*btp) +
1149 311708 : (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 311708 : dup = bp->b_addr + offset;
1155 311708 : needlog = needscan = 0;
1156 311708 : error = xfs_dir2_data_use_free(args, bp, dup, args->geo->blksize - i,
1157 : i, &needlog, &needscan);
1158 311709 : if (error)
1159 0 : goto out_free;
1160 311709 : ASSERT(needscan == 0);
1161 : /*
1162 : * Fill in the tail.
1163 : */
1164 311709 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
1165 311709 : btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
1166 311709 : btp->stale = 0;
1167 311709 : blp = xfs_dir2_block_leaf_p(btp);
1168 311709 : endoffset = (uint)((char *)blp - (char *)hdr);
1169 : /*
1170 : * Remove the freespace, we'll manage it.
1171 : */
1172 311709 : error = xfs_dir2_data_use_free(args, bp, dup,
1173 311709 : (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
1174 311709 : be16_to_cpu(dup->length), &needlog, &needscan);
1175 311708 : if (error)
1176 0 : goto out_free;
1177 :
1178 : /*
1179 : * Create entry for .
1180 : */
1181 311708 : dep = bp->b_addr + offset;
1182 311708 : dep->inumber = cpu_to_be64(args->owner);
1183 311708 : dep->namelen = 1;
1184 311708 : dep->name[0] = '.';
1185 311708 : xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
1186 311708 : tagp = xfs_dir2_data_entry_tag_p(mp, dep);
1187 311708 : *tagp = cpu_to_be16(offset);
1188 311708 : xfs_dir2_data_log_entry(args, bp, dep);
1189 311707 : blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1190 311707 : blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset));
1191 311707 : offset += xfs_dir2_data_entsize(mp, dep->namelen);
1192 :
1193 : /*
1194 : * Create entry for ..
1195 : */
1196 311707 : dep = bp->b_addr + offset;
1197 311707 : dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
1198 311707 : dep->namelen = 2;
1199 311707 : dep->name[0] = dep->name[1] = '.';
1200 311707 : xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
1201 311709 : tagp = xfs_dir2_data_entry_tag_p(mp, dep);
1202 311709 : *tagp = cpu_to_be16(offset);
1203 311709 : xfs_dir2_data_log_entry(args, bp, dep);
1204 311707 : blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1205 311707 : blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset));
1206 311707 : offset += xfs_dir2_data_entsize(mp, dep->namelen);
1207 :
1208 : /*
1209 : * Loop over existing entries, stuff them in.
1210 : */
1211 311707 : i = 0;
1212 311707 : if (!sfp->count)
1213 : sfep = NULL;
1214 : else
1215 311593 : 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 6185347 : while (offset < endoffset) {
1222 : /*
1223 : * sfep is null when we reach the end of the list.
1224 : */
1225 5873638 : if (sfep == NULL)
1226 : newoffset = endoffset;
1227 : else
1228 5562859 : newoffset = xfs_dir2_sf_get_offset(sfep);
1229 : /*
1230 : * There should be a hole here, make one.
1231 : */
1232 5873638 : if (offset < newoffset) {
1233 1565799 : dup = bp->b_addr + offset;
1234 1565799 : dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1235 1565799 : dup->length = cpu_to_be16(newoffset - offset);
1236 1565799 : *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(offset);
1237 1565799 : xfs_dir2_data_log_unused(args, bp, dup);
1238 1565799 : xfs_dir2_data_freeinsert(hdr,
1239 : xfs_dir2_data_bestfree_p(mp, hdr),
1240 : dup, &dummy);
1241 1565799 : offset += be16_to_cpu(dup->length);
1242 1565799 : continue;
1243 : }
1244 : /*
1245 : * Copy a real entry.
1246 : */
1247 4307839 : dep = bp->b_addr + newoffset;
1248 4307839 : dep->inumber = cpu_to_be64(xfs_dir2_sf_get_ino(mp, sfp, sfep));
1249 4307834 : dep->namelen = sfep->namelen;
1250 4307834 : xfs_dir2_data_put_ftype(mp, dep,
1251 : xfs_dir2_sf_get_ftype(mp, sfep));
1252 8615668 : memcpy(dep->name, sfep->name, dep->namelen);
1253 4307834 : tagp = xfs_dir2_data_entry_tag_p(mp, dep);
1254 4307823 : *tagp = cpu_to_be16(newoffset);
1255 4307823 : xfs_dir2_data_log_entry(args, bp, dep);
1256 4307833 : name.name = sfep->name;
1257 4307833 : name.len = sfep->namelen;
1258 4307833 : blp[2 + i].hashval = cpu_to_be32(xfs_dir2_hashname(mp, &name));
1259 4307840 : blp[2 + i].address =
1260 4307840 : cpu_to_be32(xfs_dir2_byte_to_dataptr(newoffset));
1261 4307840 : offset = (int)((char *)(tagp + 1) - (char *)hdr);
1262 4307840 : if (++i == sfp->count)
1263 : sfep = NULL;
1264 : else
1265 3996246 : sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
1266 : }
1267 : /* Done with the temporary buffer */
1268 311709 : kmem_free(sfp);
1269 : /*
1270 : * Sort the leaf entries by hash value.
1271 : */
1272 311709 : 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 311709 : ASSERT(needscan == 0);
1278 311709 : xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1279 311706 : xfs_dir2_block_log_tail(tp, bp);
1280 311709 : xfs_dir3_data_check(dp, bp);
1281 311709 : return 0;
1282 0 : out_free:
1283 0 : kmem_free(sfp);
1284 0 : return error;
1285 : }
|