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 59 : xfs_dir_startup(void)
42 : {
43 59 : xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
44 59 : xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
45 59 : }
46 :
47 : static xfs_failaddr_t
48 2656792 : xfs_dir3_block_verify(
49 : struct xfs_buf *bp)
50 : {
51 2656792 : struct xfs_mount *mp = bp->b_mount;
52 2656792 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
53 :
54 2656792 : if (!xfs_verify_magic(bp, hdr3->magic))
55 0 : return __this_address;
56 :
57 2656776 : if (xfs_has_crc(mp)) {
58 2656765 : if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
59 0 : return __this_address;
60 2656768 : if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
61 0 : return __this_address;
62 2656768 : if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
63 0 : return __this_address;
64 : }
65 2656779 : return __xfs_dir3_data_check(NULL, bp);
66 : }
67 :
68 : static void
69 47183 : xfs_dir3_block_read_verify(
70 : struct xfs_buf *bp)
71 : {
72 47183 : struct xfs_mount *mp = bp->b_mount;
73 47183 : xfs_failaddr_t fa;
74 :
75 94366 : if (xfs_has_crc(mp) &&
76 : !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
77 30 : xfs_verifier_error(bp, -EFSBADCRC, __this_address);
78 : else {
79 47153 : fa = xfs_dir3_block_verify(bp);
80 47153 : if (fa)
81 8 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
82 : }
83 47183 : }
84 :
85 : static void
86 2327467 : xfs_dir3_block_write_verify(
87 : struct xfs_buf *bp)
88 : {
89 2327467 : struct xfs_mount *mp = bp->b_mount;
90 2327467 : struct xfs_buf_log_item *bip = bp->b_log_item;
91 2327467 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
92 2327467 : xfs_failaddr_t fa;
93 :
94 2327467 : fa = xfs_dir3_block_verify(bp);
95 2327467 : if (fa) {
96 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
97 0 : return;
98 : }
99 :
100 2327467 : if (!xfs_has_crc(mp))
101 : return;
102 :
103 2327456 : if (bip)
104 2327456 : hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
105 :
106 2327456 : 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 239263561 : xfs_dir3_block_header_check(
120 : struct xfs_buf *bp,
121 : xfs_ino_t owner)
122 : {
123 239263561 : struct xfs_mount *mp = bp->b_mount;
124 :
125 239263561 : if (xfs_has_crc(mp)) {
126 239261680 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
127 :
128 239261680 : ASSERT(hdr3->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
129 :
130 239261680 : 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 239258764 : 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 239258764 : struct xfs_mount *mp = dp->i_mount;
148 239258764 : xfs_failaddr_t fa;
149 239258764 : int err;
150 :
151 239258764 : err = xfs_da_read_buf(tp, dp, mp->m_dir_geo->datablk, 0, bpp,
152 : XFS_DATA_FORK, &xfs_dir3_block_buf_ops);
153 239269883 : if (err || !*bpp)
154 : return err;
155 :
156 : /* Check things that we can't do in the verifier. */
157 239269804 : fa = xfs_dir3_block_header_check(*bpp, owner);
158 239271185 : 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 239271185 : xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF);
167 239271185 : return err;
168 : }
169 :
170 : static void
171 672490 : xfs_dir3_block_init(
172 : struct xfs_da_args *args,
173 : struct xfs_buf *bp)
174 : {
175 672490 : struct xfs_trans *tp = args->trans;
176 672490 : struct xfs_inode *dp = args->dp;
177 672490 : struct xfs_mount *mp = dp->i_mount;
178 672490 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
179 :
180 672490 : bp->b_ops = &xfs_dir3_block_buf_ops;
181 672490 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF);
182 :
183 672485 : if (xfs_has_crc(mp)) {
184 672463 : memset(hdr3, 0, sizeof(*hdr3));
185 672463 : hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
186 672463 : hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp));
187 672463 : hdr3->owner = cpu_to_be64(args->owner);
188 672463 : uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
189 672463 : return;
190 :
191 : }
192 22 : hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
193 : }
194 :
195 : static void
196 7799343 : 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 7799343 : struct xfs_dir2_data_free *bf;
208 7799343 : __be16 *tagp = NULL;
209 7799343 : struct xfs_dir2_data_unused *dup = NULL;
210 7799343 : struct xfs_dir2_data_unused *enddup = NULL;
211 :
212 7799343 : *compact = 0;
213 7799343 : 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 7798070 : if (btp->stale) {
219 2304490 : if (be16_to_cpu(bf[0].length) >= len) {
220 : /*
221 : * The biggest entry enough to avoid compaction.
222 : */
223 2304489 : dup = (xfs_dir2_data_unused_t *)
224 2304489 : ((char *)hdr + be16_to_cpu(bf[0].offset));
225 2304489 : 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 5493580 : tagp = (__be16 *)blp - 1;
258 :
259 : /* Data object just before the first leaf entry. */
260 5493580 : 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 5493580 : 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 5480072 : dup = (xfs_dir2_data_unused_t *)
272 5480072 : ((char *)hdr + be16_to_cpu(bf[0].offset));
273 5480072 : if (dup != enddup) {
274 : /*
275 : * Not the same free entry, just check its length.
276 : */
277 100909 : if (be16_to_cpu(dup->length) < len)
278 14 : dup = NULL;
279 100909 : goto out;
280 : }
281 :
282 : /*
283 : * It is the biggest freespace, can it hold the leaf too?
284 : */
285 5379163 : if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
286 : /*
287 : * Yes, use the second-largest entry instead if it works.
288 : */
289 16246 : 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 5376425 : out:
297 7798070 : *tagpp = tagp;
298 7798070 : *dupp = dup;
299 7798070 : *enddupp = enddup;
300 7798070 : }
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 7801172 : xfs_dir2_block_addname(
360 : xfs_da_args_t *args) /* directory op arguments */
361 : {
362 7801172 : xfs_dir2_data_hdr_t *hdr; /* block header */
363 7801172 : xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
364 7801172 : struct xfs_buf *bp; /* buffer for block */
365 7801172 : xfs_dir2_block_tail_t *btp; /* block tail */
366 7801172 : int compact; /* need to compact leaf ents */
367 7801172 : xfs_dir2_data_entry_t *dep; /* block data entry */
368 7801172 : xfs_inode_t *dp; /* directory inode */
369 7801172 : xfs_dir2_data_unused_t *dup; /* block unused entry */
370 7801172 : int error; /* error return value */
371 7801172 : xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
372 7801172 : xfs_dahash_t hash; /* hash value of found entry */
373 7801172 : int high; /* high index for binary srch */
374 7801172 : int highstale; /* high stale index */
375 7801172 : int lfloghigh=0; /* last final leaf to log */
376 7801172 : int lfloglow=0; /* first final leaf to log */
377 7801172 : int len; /* length of the new entry */
378 7801172 : int low; /* low index for binary srch */
379 7801172 : int lowstale; /* low stale index */
380 7801172 : int mid=0; /* midpoint for binary srch */
381 7801172 : int needlog; /* need to log header */
382 7801172 : int needscan; /* need to rescan freespace */
383 7801172 : __be16 *tagp; /* pointer to tag value */
384 7801172 : xfs_trans_t *tp; /* transaction structure */
385 :
386 7801172 : trace_xfs_dir2_block_addname(args);
387 :
388 7796423 : dp = args->dp;
389 7796423 : tp = args->trans;
390 :
391 : /* Read the (one and only) directory block into bp. */
392 7796423 : error = xfs_dir3_block_read(tp, dp, args->owner, &bp);
393 7800577 : if (error)
394 : return error;
395 :
396 7800577 : len = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
397 :
398 : /*
399 : * Set up pointers to parts of the block.
400 : */
401 7800577 : hdr = bp->b_addr;
402 7800577 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
403 7800577 : 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 7800577 : 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 7802435 : if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
416 40 : xfs_trans_brelse(tp, bp);
417 40 : if (!dup)
418 : return -ENOSPC;
419 40 : return 0;
420 : }
421 :
422 : /*
423 : * If we don't have space for the new entry & leaf ...
424 : */
425 7802395 : if (!dup) {
426 : /* Don't have a space reservation: return no-space. */
427 29769 : 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 29769 : error = xfs_dir2_block_to_leaf(args, bp);
434 29768 : if (error)
435 : return error;
436 29768 : return xfs_dir2_leaf_addname(args);
437 : }
438 :
439 7772626 : needlog = needscan = 0;
440 :
441 : /*
442 : * If need to compact the leaf entries, do it now.
443 : */
444 7772626 : 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 7772626 : } 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 2304811 : lfloglow = be32_to_cpu(btp->count);
455 2304811 : lfloghigh = -1;
456 : }
457 :
458 : /*
459 : * Find the slot that's first lower than our hash value, -1 if none.
460 : */
461 51558104 : for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
462 43983468 : mid = (low + high) >> 1;
463 43983468 : if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
464 : break;
465 43785478 : if (hash < args->hashval)
466 29509276 : low = mid + 1;
467 : else
468 14276202 : high = mid - 1;
469 : }
470 11856483 : while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
471 4083857 : mid--;
472 : }
473 : /*
474 : * No stale entries, will use enddup space to hold new leaf.
475 : */
476 7772626 : if (!btp->stale) {
477 5466765 : xfs_dir2_data_aoff_t aoff;
478 :
479 : /*
480 : * Mark the space needed for the new leaf entry, now in use.
481 : */
482 5466765 : aoff = (xfs_dir2_data_aoff_t)((char *)enddup - (char *)hdr +
483 5466765 : be16_to_cpu(enddup->length) - sizeof(*blp));
484 5466765 : error = xfs_dir2_data_use_free(args, bp, enddup, aoff,
485 : (xfs_dir2_data_aoff_t)sizeof(*blp), &needlog,
486 : &needscan);
487 5457821 : if (error)
488 : return error;
489 :
490 : /*
491 : * Update the tail (entry count).
492 : */
493 5457821 : 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 5457821 : if (needscan) {
499 21348 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
500 21348 : 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 5457821 : blp--;
508 5457821 : mid++;
509 5457821 : if (mid)
510 10915642 : memmove(blp, &blp[1], mid * sizeof(*blp));
511 5457821 : lfloglow = 0;
512 5457821 : lfloghigh = mid;
513 : }
514 : /*
515 : * Use a stale leaf for our new entry.
516 : */
517 : else {
518 : for (lowstale = mid;
519 52394464 : lowstale >= 0 &&
520 52018063 : blp[lowstale].address !=
521 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
522 50088603 : lowstale--)
523 50088603 : continue;
524 2305861 : for (highstale = mid + 1;
525 13677646 : highstale < be32_to_cpu(btp->count) &&
526 6281050 : blp[highstale].address !=
527 13014763 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
528 4779469 : (lowstale < 0 || mid - lowstale > highstale - mid);
529 5090735 : highstale++)
530 5090735 : continue;
531 : /*
532 : * Move entries toward the low-numbered stale entry.
533 : */
534 2305861 : if (lowstale >= 0 &&
535 814105 : (highstale == be32_to_cpu(btp->count) ||
536 814105 : mid - lowstale <= highstale - mid)) {
537 1726840 : if (mid - lowstale)
538 2902682 : memmove(&blp[lowstale], &blp[lowstale + 1],
539 : (mid - lowstale) * sizeof(*blp));
540 1726840 : lfloglow = min(lowstale, lfloglow);
541 1726840 : lfloghigh = max(mid, lfloghigh);
542 : }
543 : /*
544 : * Move entries toward the high-numbered stale entry.
545 : */
546 : else {
547 579021 : ASSERT(highstale < be32_to_cpu(btp->count));
548 579021 : mid++;
549 579021 : if (highstale - mid)
550 520306 : memmove(&blp[mid + 1], &blp[mid],
551 : (highstale - mid) * sizeof(*blp));
552 579021 : lfloglow = min(mid, lfloglow);
553 579021 : lfloghigh = max(highstale, lfloghigh);
554 : }
555 2305861 : be32_add_cpu(&btp->stale, -1);
556 : }
557 : /*
558 : * Point to the new data entry.
559 : */
560 7763682 : dep = (xfs_dir2_data_entry_t *)dup;
561 : /*
562 : * Fill in the leaf entry.
563 : */
564 7763682 : blp[mid].hashval = cpu_to_be32(args->hashval);
565 7763682 : blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
566 : (char *)dep - (char *)hdr));
567 7763682 : xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
568 : /*
569 : * Mark space for the data entry used.
570 : */
571 7773562 : 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 7770833 : if (error)
575 : return error;
576 : /*
577 : * Create the new data entry.
578 : */
579 7770833 : dep->inumber = cpu_to_be64(args->inumber);
580 7770833 : dep->namelen = args->namelen;
581 15541666 : memcpy(dep->name, args->name, args->namelen);
582 7770833 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
583 7764381 : tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
584 7762545 : *tagp = cpu_to_be16((char *)dep - (char *)hdr);
585 : /*
586 : * Clean up the bestfree array and log the header, tail, and entry.
587 : */
588 7762545 : if (needscan)
589 735481 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
590 7763085 : if (needlog)
591 7765194 : xfs_dir2_data_log_header(args, bp);
592 7770785 : xfs_dir2_block_log_tail(tp, bp);
593 7773623 : xfs_dir2_data_log_entry(args, bp, dep);
594 7773119 : xfs_dir3_data_check(dp, bp);
595 7773119 : return 0;
596 : }
597 :
598 : /*
599 : * Log leaf entries from the block.
600 : */
601 : static void
602 12617854 : 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 12617854 : xfs_dir2_data_hdr_t *hdr = bp->b_addr;
609 12617854 : xfs_dir2_leaf_entry_t *blp;
610 12617854 : xfs_dir2_block_tail_t *btp;
611 :
612 12617854 : btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
613 12617854 : blp = xfs_dir2_block_leaf_p(btp);
614 12617854 : xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
615 12617854 : (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
616 12619988 : }
617 :
618 : /*
619 : * Log the block tail.
620 : */
621 : static void
622 12619111 : xfs_dir2_block_log_tail(
623 : xfs_trans_t *tp, /* transaction structure */
624 : struct xfs_buf *bp) /* block buffer */
625 : {
626 12619111 : xfs_dir2_data_hdr_t *hdr = bp->b_addr;
627 12619111 : xfs_dir2_block_tail_t *btp;
628 :
629 12619111 : btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
630 12619111 : xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
631 12619111 : (uint)((char *)(btp + 1) - (char *)hdr - 1));
632 12619741 : }
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 28697204 : xfs_dir2_block_lookup(
640 : xfs_da_args_t *args) /* dir lookup arguments */
641 : {
642 28697204 : xfs_dir2_data_hdr_t *hdr; /* block header */
643 28697204 : xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
644 28697204 : struct xfs_buf *bp; /* block buffer */
645 28697204 : xfs_dir2_block_tail_t *btp; /* block tail */
646 28697204 : xfs_dir2_data_entry_t *dep; /* block data entry */
647 28697204 : xfs_inode_t *dp; /* incore inode */
648 28697204 : int ent; /* entry index */
649 28697204 : int error; /* error return value */
650 :
651 28697204 : 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 28680612 : if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
658 : return error;
659 18268943 : dp = args->dp;
660 18268943 : hdr = bp->b_addr;
661 18268943 : xfs_dir3_data_check(dp, bp);
662 18270627 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
663 18270627 : blp = xfs_dir2_block_leaf_p(btp);
664 : /*
665 : * Get the offset from the leaf entry, to point to the data.
666 : */
667 18270627 : dep = (xfs_dir2_data_entry_t *)((char *)hdr +
668 18270627 : xfs_dir2_dataptr_to_off(args->geo,
669 18270627 : be32_to_cpu(blp[ent].address)));
670 : /*
671 : * Fill in inode number, CI name if appropriate, release the block.
672 : */
673 18270627 : args->inumber = be64_to_cpu(dep->inumber);
674 18270627 : args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
675 18270419 : error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
676 18269965 : xfs_trans_brelse(args->trans, bp);
677 18269965 : return error;
678 : }
679 :
680 : /*
681 : * Internal block lookup routine.
682 : */
683 : static int /* error */
684 34638472 : 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 34638472 : xfs_dir2_dataptr_t addr; /* data entry address */
690 34638472 : xfs_dir2_data_hdr_t *hdr; /* block header */
691 34638472 : xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
692 34638472 : struct xfs_buf *bp; /* block buffer */
693 34638472 : xfs_dir2_block_tail_t *btp; /* block tail */
694 34638472 : xfs_dir2_data_entry_t *dep; /* block data entry */
695 34638472 : xfs_inode_t *dp; /* incore inode */
696 34638472 : int error; /* error return value */
697 34638472 : xfs_dahash_t hash; /* found hash value */
698 34638472 : int high; /* binary search high index */
699 34638472 : int low; /* binary search low index */
700 34638472 : int mid; /* binary search current idx */
701 34638472 : xfs_trans_t *tp; /* transaction pointer */
702 34638472 : enum xfs_dacmp cmp; /* comparison result */
703 :
704 34638472 : dp = args->dp;
705 34638472 : tp = args->trans;
706 :
707 34638472 : error = xfs_dir3_block_read(tp, dp, args->owner, &bp);
708 34637150 : if (error)
709 : return error;
710 :
711 34637631 : hdr = bp->b_addr;
712 34637631 : xfs_dir3_data_check(dp, bp);
713 34667747 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
714 34667747 : 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 34667747 : for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
720 158471834 : ASSERT(low <= high);
721 158471749 : mid = (low + high) >> 1;
722 158471749 : if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
723 : break;
724 134210790 : if (hash < args->hashval)
725 89345890 : low = mid + 1;
726 : else
727 44864900 : high = mid - 1;
728 134210790 : if (low > high) {
729 10406703 : ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
730 10406703 : xfs_trans_brelse(tp, bp);
731 10406703 : return -ENOENT;
732 : }
733 : }
734 : /*
735 : * Back up to the first one with the right hash value.
736 : */
737 25770132 : 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 27003918 : do {
745 27003918 : if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
746 310767 : continue;
747 : /*
748 : * Get pointer to the entry from the leaf.
749 : */
750 26693151 : dep = (xfs_dir2_data_entry_t *)
751 26693151 : ((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 26693151 : cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
758 26691434 : if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
759 24222935 : args->cmpresult = cmp;
760 24222935 : *bpp = bp;
761 24222935 : *entno = mid;
762 24222935 : if (cmp == XFS_CMP_EXACT)
763 : return 0;
764 : }
765 2806398 : } while (++mid < be32_to_cpu(btp->count) &&
766 2747191 : be32_to_cpu(blp[mid].hashval) == hash);
767 :
768 63439 : 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 63439 : if (args->cmpresult == XFS_CMP_CASE)
774 : return 0;
775 : /*
776 : * No match, release the buffer and return ENOENT.
777 : */
778 36412 : xfs_trans_brelse(tp, bp);
779 36412 : 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 4172767 : xfs_dir2_block_removename(
788 : xfs_da_args_t *args) /* directory operation args */
789 : {
790 4172767 : xfs_dir2_data_hdr_t *hdr; /* block header */
791 4172767 : xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
792 4172767 : struct xfs_buf *bp; /* block buffer */
793 4172767 : xfs_dir2_block_tail_t *btp; /* block tail */
794 4172767 : xfs_dir2_data_entry_t *dep; /* block data entry */
795 4172767 : xfs_inode_t *dp; /* incore inode */
796 4172767 : int ent; /* block leaf entry index */
797 4172767 : int error; /* error return value */
798 4172767 : int needlog; /* need to log block header */
799 4172767 : int needscan; /* need to fixup bestfree */
800 4172767 : xfs_dir2_sf_hdr_t sfh; /* shortform header */
801 4172767 : int size; /* shortform size */
802 4172767 : xfs_trans_t *tp; /* transaction pointer */
803 :
804 4172767 : 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 4172059 : if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
811 : return error;
812 : }
813 4173892 : dp = args->dp;
814 4173892 : tp = args->trans;
815 4173892 : hdr = bp->b_addr;
816 4173892 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
817 4173892 : blp = xfs_dir2_block_leaf_p(btp);
818 : /*
819 : * Point to the data entry using the leaf entry.
820 : */
821 4173892 : dep = (xfs_dir2_data_entry_t *)((char *)hdr +
822 4173892 : xfs_dir2_dataptr_to_off(args->geo,
823 4173892 : be32_to_cpu(blp[ent].address)));
824 : /*
825 : * Mark the data entry's space free.
826 : */
827 4173892 : needlog = needscan = 0;
828 4173892 : xfs_dir2_data_make_free(args, bp,
829 : (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
830 4173892 : xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
831 : &needscan);
832 : /*
833 : * Fix up the block tail.
834 : */
835 4173870 : be32_add_cpu(&btp->stale, 1);
836 4173870 : xfs_dir2_block_log_tail(tp, bp);
837 : /*
838 : * Remove the leaf entry by marking it stale.
839 : */
840 4173927 : blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
841 4173927 : xfs_dir2_block_log_leaf(tp, bp, ent, ent);
842 : /*
843 : * Fix up bestfree, log the header if necessary.
844 : */
845 4173905 : if (needscan)
846 751270 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
847 4173914 : if (needlog)
848 2493312 : xfs_dir2_data_log_header(args, bp);
849 4173913 : xfs_dir3_data_check(dp, bp);
850 : /*
851 : * See if the size as a shortform is good enough.
852 : */
853 4173955 : size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
854 4173928 : if (size > xfs_inode_data_fork_size(dp))
855 : return 0;
856 :
857 : /*
858 : * If it works, do the conversion.
859 : */
860 432470 : 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 1780643 : xfs_dir2_block_replace(
869 : xfs_da_args_t *args) /* directory operation args */
870 : {
871 1780643 : xfs_dir2_data_hdr_t *hdr; /* block header */
872 1780643 : xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
873 1780643 : struct xfs_buf *bp; /* block buffer */
874 1780643 : xfs_dir2_block_tail_t *btp; /* block tail */
875 1780643 : xfs_dir2_data_entry_t *dep; /* block data entry */
876 1780643 : xfs_inode_t *dp; /* incore inode */
877 1780643 : int ent; /* leaf entry index */
878 1780643 : int error; /* error return value */
879 :
880 1780643 : 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 1780626 : if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
887 : return error;
888 : }
889 1780647 : dp = args->dp;
890 1780647 : hdr = bp->b_addr;
891 1780647 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
892 1780647 : blp = xfs_dir2_block_leaf_p(btp);
893 : /*
894 : * Point to the data entry we need to change.
895 : */
896 1780647 : dep = (xfs_dir2_data_entry_t *)((char *)hdr +
897 1780647 : xfs_dir2_dataptr_to_off(args->geo,
898 1780647 : be32_to_cpu(blp[ent].address)));
899 1780647 : ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
900 : /*
901 : * Change the inode number to the new value.
902 : */
903 1780647 : dep->inumber = cpu_to_be64(args->inumber);
904 1780647 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
905 1780640 : xfs_dir2_data_log_entry(args, bp, dep);
906 1780650 : xfs_dir3_data_check(dp, bp);
907 1780650 : return 0;
908 : }
909 :
910 : /*
911 : * Qsort comparison routine for the block leaf entries.
912 : */
913 : static int /* sort order */
914 41464692 : xfs_dir2_block_sort(
915 : const void *a, /* first leaf entry */
916 : const void *b) /* second leaf entry */
917 : {
918 41464692 : const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
919 41464692 : const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
920 :
921 41464692 : la = a;
922 41464692 : lb = b;
923 41464692 : return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
924 16206372 : (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 11018327 : 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 11018327 : __be16 *bestsp; /* leaf bests table */
937 11018327 : xfs_dir2_data_hdr_t *hdr; /* block header */
938 11018327 : xfs_dir2_block_tail_t *btp; /* block tail */
939 11018327 : xfs_inode_t *dp; /* incore directory inode */
940 11018327 : xfs_dir2_data_unused_t *dup; /* unused data entry */
941 11018327 : int error; /* error return value */
942 11018327 : int from; /* leaf from index */
943 11018327 : xfs_dir2_leaf_t *leaf; /* leaf structure */
944 11018327 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
945 11018327 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
946 11018327 : xfs_mount_t *mp; /* file system mount point */
947 11018327 : int needlog; /* need to log data header */
948 11018327 : int needscan; /* need to scan for bestfree */
949 11018327 : xfs_dir2_sf_hdr_t sfh; /* shortform header */
950 11018327 : int size; /* bytes used */
951 11018327 : __be16 *tagp; /* end of entry (tag) */
952 11018327 : int to; /* block/leaf to index */
953 11018327 : xfs_trans_t *tp; /* transaction pointer */
954 11018327 : struct xfs_dir3_icleaf_hdr leafhdr;
955 :
956 11018327 : trace_xfs_dir2_leaf_to_block(args);
957 :
958 11017744 : dp = args->dp;
959 11017744 : tp = args->trans;
960 11017744 : mp = dp->i_mount;
961 11017744 : leaf = lbp->b_addr;
962 11017744 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
963 11017889 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
964 :
965 11017889 : 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 11018159 : while (dp->i_disk_size > args->geo->blksize) {
974 8718014 : int hdrsz;
975 :
976 8718014 : hdrsz = args->geo->data_entry_offset;
977 8718014 : bestsp = xfs_dir2_leaf_bests_p(ltp);
978 8718014 : if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
979 8718014 : args->geo->blksize - hdrsz) {
980 295 : if ((error =
981 0 : xfs_dir2_leaf_trim_data(args, lbp,
982 : (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 2300145 : if (!dbp) {
991 11132 : error = xfs_dir3_data_read(tp, dp, args->owner,
992 : args->geo->datablk, 0, &dbp);
993 11132 : if (error)
994 : return error;
995 : }
996 2300145 : hdr = dbp->b_addr;
997 2300145 : 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 2300145 : size = (uint)sizeof(xfs_dir2_block_tail_t) +
1004 2300145 : (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
1005 : /*
1006 : * Look at the last data entry.
1007 : */
1008 2300145 : tagp = (__be16 *)((char *)hdr + args->geo->blksize) - 1;
1009 2300145 : 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 2300145 : if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
1014 2299378 : be16_to_cpu(dup->length) < size)
1015 : return 0;
1016 :
1017 : /*
1018 : * Start converting it to block form.
1019 : */
1020 22891 : xfs_dir3_block_init(args, dbp);
1021 :
1022 22891 : needlog = 1;
1023 22891 : needscan = 0;
1024 : /*
1025 : * Use up the space at the end of the block (blp/btp).
1026 : */
1027 22891 : error = xfs_dir2_data_use_free(args, dbp, dup,
1028 22891 : args->geo->blksize - size, size, &needlog, &needscan);
1029 22891 : if (error)
1030 : return error;
1031 : /*
1032 : * Initialize the block tail.
1033 : */
1034 22891 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
1035 22891 : btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
1036 22891 : btp->stale = 0;
1037 22891 : xfs_dir2_block_log_tail(tp, dbp);
1038 : /*
1039 : * Initialize the block leaf area. We compact out stale entries.
1040 : */
1041 22891 : lep = xfs_dir2_block_leaf_p(btp);
1042 4817184 : for (from = to = 0; from < leafhdr.count; from++) {
1043 4794293 : if (leafhdr.ents[from].address ==
1044 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
1045 4313195 : continue;
1046 481098 : lep[to++] = leafhdr.ents[from];
1047 : }
1048 22891 : ASSERT(to == be32_to_cpu(btp->count));
1049 22891 : 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 22891 : if (needscan)
1054 5346 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1055 22891 : if (needlog)
1056 22891 : xfs_dir2_data_log_header(args, dbp);
1057 : /*
1058 : * Pitch the old leaf block.
1059 : */
1060 22891 : error = xfs_da_shrink_inode(args, args->geo->leafblk, lbp);
1061 22891 : if (error)
1062 : return error;
1063 :
1064 : /*
1065 : * Now see if the resulting block can be shrunken to shortform.
1066 : */
1067 22891 : size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
1068 22891 : if (size > xfs_inode_data_fork_size(dp))
1069 : return 0;
1070 :
1071 18605 : 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 649609 : xfs_dir2_sf_to_block(
1079 : struct xfs_da_args *args)
1080 : {
1081 649609 : struct xfs_trans *tp = args->trans;
1082 649609 : struct xfs_inode *dp = args->dp;
1083 649609 : struct xfs_mount *mp = dp->i_mount;
1084 649609 : struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
1085 649609 : struct xfs_da_geometry *geo = args->geo;
1086 649609 : xfs_dir2_db_t blkno; /* dir-relative block # (0) */
1087 649609 : xfs_dir2_data_hdr_t *hdr; /* block header */
1088 649609 : xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1089 649609 : struct xfs_buf *bp; /* block buffer */
1090 649609 : xfs_dir2_block_tail_t *btp; /* block tail pointer */
1091 649609 : xfs_dir2_data_entry_t *dep; /* data entry pointer */
1092 649609 : int dummy; /* trash */
1093 649609 : xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1094 649609 : int endoffset; /* end of data objects */
1095 649609 : int error; /* error return value */
1096 649609 : int i; /* index */
1097 649609 : int needlog; /* need to log block header */
1098 649609 : int needscan; /* need to scan block freespc */
1099 649609 : int newoffset; /* offset from current entry */
1100 649609 : unsigned int offset = geo->data_entry_offset;
1101 649609 : xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
1102 649609 : xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1103 649609 : xfs_dir2_sf_hdr_t *sfp; /* shortform header */
1104 649609 : __be16 *tagp; /* end of data entry */
1105 649609 : struct xfs_name name;
1106 :
1107 649609 : trace_xfs_dir2_sf_to_block(args);
1108 :
1109 649606 : ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
1110 649606 : ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
1111 :
1112 649606 : oldsfp = (xfs_dir2_sf_hdr_t *)ifp->if_u1.if_data;
1113 :
1114 649606 : ASSERT(ifp->if_bytes == dp->i_disk_size);
1115 649606 : ASSERT(ifp->if_u1.if_data != NULL);
1116 649606 : ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1117 649606 : 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 649606 : sfp = kmem_alloc(ifp->if_bytes, 0);
1124 1299222 : memcpy(sfp, oldsfp, ifp->if_bytes);
1125 :
1126 649611 : xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK);
1127 649609 : xfs_bmap_local_to_extents_empty(tp, dp, XFS_DATA_FORK);
1128 649612 : dp->i_disk_size = 0;
1129 :
1130 : /*
1131 : * Add block 0 to the inode.
1132 : */
1133 649612 : error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1134 649607 : if (error)
1135 0 : goto out_free;
1136 : /*
1137 : * Initialize the data block, then convert it to block format.
1138 : */
1139 649607 : error = xfs_dir3_data_init(args, blkno, &bp);
1140 649601 : if (error)
1141 0 : goto out_free;
1142 649601 : xfs_dir3_block_init(args, bp);
1143 649599 : hdr = bp->b_addr;
1144 :
1145 : /*
1146 : * Compute size of block "tail" area.
1147 : */
1148 649599 : i = (uint)sizeof(*btp) +
1149 649599 : (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 649599 : dup = bp->b_addr + offset;
1155 649599 : needlog = needscan = 0;
1156 649599 : error = xfs_dir2_data_use_free(args, bp, dup, args->geo->blksize - i,
1157 : i, &needlog, &needscan);
1158 649598 : if (error)
1159 0 : goto out_free;
1160 649598 : ASSERT(needscan == 0);
1161 : /*
1162 : * Fill in the tail.
1163 : */
1164 649598 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
1165 649598 : btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
1166 649598 : btp->stale = 0;
1167 649598 : blp = xfs_dir2_block_leaf_p(btp);
1168 649598 : endoffset = (uint)((char *)blp - (char *)hdr);
1169 : /*
1170 : * Remove the freespace, we'll manage it.
1171 : */
1172 649598 : error = xfs_dir2_data_use_free(args, bp, dup,
1173 649598 : (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
1174 649598 : be16_to_cpu(dup->length), &needlog, &needscan);
1175 649598 : if (error)
1176 0 : goto out_free;
1177 :
1178 : /*
1179 : * Create entry for .
1180 : */
1181 649598 : dep = bp->b_addr + offset;
1182 649598 : dep->inumber = cpu_to_be64(args->owner);
1183 649598 : dep->namelen = 1;
1184 649598 : dep->name[0] = '.';
1185 649598 : xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
1186 649585 : tagp = xfs_dir2_data_entry_tag_p(mp, dep);
1187 649589 : *tagp = cpu_to_be16(offset);
1188 649589 : xfs_dir2_data_log_entry(args, bp, dep);
1189 649603 : blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1190 649603 : blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset));
1191 649603 : offset += xfs_dir2_data_entsize(mp, dep->namelen);
1192 :
1193 : /*
1194 : * Create entry for ..
1195 : */
1196 649603 : dep = bp->b_addr + offset;
1197 649603 : dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
1198 649586 : dep->namelen = 2;
1199 649586 : dep->name[0] = dep->name[1] = '.';
1200 649586 : xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
1201 649585 : tagp = xfs_dir2_data_entry_tag_p(mp, dep);
1202 649589 : *tagp = cpu_to_be16(offset);
1203 649589 : xfs_dir2_data_log_entry(args, bp, dep);
1204 649602 : blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1205 649602 : blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset));
1206 649602 : offset += xfs_dir2_data_entsize(mp, dep->namelen);
1207 :
1208 : /*
1209 : * Loop over existing entries, stuff them in.
1210 : */
1211 649602 : i = 0;
1212 649602 : if (!sfp->count)
1213 : sfep = NULL;
1214 : else
1215 649120 : 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 12826295 : while (offset < endoffset) {
1222 : /*
1223 : * sfep is null when we reach the end of the list.
1224 : */
1225 12176682 : if (sfep == NULL)
1226 : newoffset = endoffset;
1227 : else
1228 11533485 : newoffset = xfs_dir2_sf_get_offset(sfep);
1229 : /*
1230 : * There should be a hole here, make one.
1231 : */
1232 12176682 : if (offset < newoffset) {
1233 3054955 : dup = bp->b_addr + offset;
1234 3054955 : dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1235 3054955 : dup->length = cpu_to_be16(newoffset - offset);
1236 3054955 : *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(offset);
1237 3054955 : xfs_dir2_data_log_unused(args, bp, dup);
1238 3054964 : xfs_dir2_data_freeinsert(hdr,
1239 : xfs_dir2_data_bestfree_p(mp, hdr),
1240 : dup, &dummy);
1241 3054956 : offset += be16_to_cpu(dup->length);
1242 3054956 : continue;
1243 : }
1244 : /*
1245 : * Copy a real entry.
1246 : */
1247 9121727 : dep = bp->b_addr + newoffset;
1248 9121727 : dep->inumber = cpu_to_be64(xfs_dir2_sf_get_ino(mp, sfp, sfep));
1249 9121733 : dep->namelen = sfep->namelen;
1250 9121729 : xfs_dir2_data_put_ftype(mp, dep,
1251 9121733 : xfs_dir2_sf_get_ftype(mp, sfep));
1252 18243496 : memcpy(dep->name, sfep->name, dep->namelen);
1253 9121748 : tagp = xfs_dir2_data_entry_tag_p(mp, dep);
1254 9121737 : *tagp = cpu_to_be16(newoffset);
1255 9121737 : xfs_dir2_data_log_entry(args, bp, dep);
1256 9121776 : name.name = sfep->name;
1257 9121776 : name.len = sfep->namelen;
1258 9121776 : blp[2 + i].hashval = cpu_to_be32(xfs_dir2_hashname(mp, &name));
1259 9121758 : blp[2 + i].address =
1260 9121758 : cpu_to_be32(xfs_dir2_byte_to_dataptr(newoffset));
1261 9121758 : offset = (int)((char *)(tagp + 1) - (char *)hdr);
1262 9121758 : if (++i == sfp->count)
1263 : sfep = NULL;
1264 : else
1265 8472633 : sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
1266 : }
1267 : /* Done with the temporary buffer */
1268 649613 : kmem_free(sfp);
1269 : /*
1270 : * Sort the leaf entries by hash value.
1271 : */
1272 649609 : 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 649606 : ASSERT(needscan == 0);
1278 649606 : xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1279 649613 : xfs_dir2_block_log_tail(tp, bp);
1280 649607 : xfs_dir3_data_check(dp, bp);
1281 649607 : return 0;
1282 0 : out_free:
1283 0 : kmem_free(sfp);
1284 0 : return error;
1285 : }
|