Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2002,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_dir2.h"
16 : #include "xfs_dir2_priv.h"
17 : #include "xfs_error.h"
18 : #include "xfs_trans.h"
19 : #include "xfs_buf_item.h"
20 : #include "xfs_log.h"
21 : #include "xfs_health.h"
22 :
23 : static xfs_failaddr_t xfs_dir2_data_freefind_verify(
24 : struct xfs_dir2_data_hdr *hdr, struct xfs_dir2_data_free *bf,
25 : struct xfs_dir2_data_unused *dup,
26 : struct xfs_dir2_data_free **bf_ent);
27 :
28 : struct xfs_dir2_data_free *
29 126435641 : xfs_dir2_data_bestfree_p(
30 : struct xfs_mount *mp,
31 : struct xfs_dir2_data_hdr *hdr)
32 : {
33 126435641 : if (xfs_has_crc(mp))
34 842283637 : return ((struct xfs_dir3_data_hdr *)hdr)->best_free;
35 59717 : return hdr->bestfree;
36 : }
37 :
38 : /*
39 : * Pointer to an entry's tag word.
40 : */
41 : __be16 *
42 78359108718 : xfs_dir2_data_entry_tag_p(
43 : struct xfs_mount *mp,
44 : struct xfs_dir2_data_entry *dep)
45 : {
46 >15671*10^7 : return (__be16 *)((char *)dep +
47 78359108718 : xfs_dir2_data_entsize(mp, dep->namelen) - sizeof(__be16));
48 : }
49 :
50 : uint8_t
51 60411497609 : xfs_dir2_data_get_ftype(
52 : struct xfs_mount *mp,
53 : struct xfs_dir2_data_entry *dep)
54 : {
55 60411497609 : if (xfs_has_ftype(mp)) {
56 >13532*10^7 : uint8_t ftype = dep->name[dep->namelen];
57 :
58 >13532*10^7 : if (likely(ftype < XFS_DIR3_FT_MAX))
59 60491040778 : return ftype;
60 : }
61 :
62 : return XFS_DIR3_FT_UNKNOWN;
63 : }
64 :
65 : void
66 81972158 : xfs_dir2_data_put_ftype(
67 : struct xfs_mount *mp,
68 : struct xfs_dir2_data_entry *dep,
69 : uint8_t ftype)
70 : {
71 81972158 : ASSERT(ftype < XFS_DIR3_FT_MAX);
72 81972158 : ASSERT(dep->namelen != 0);
73 :
74 81972158 : if (xfs_has_ftype(mp))
75 81885583 : dep->name[dep->namelen] = ftype;
76 81972158 : }
77 :
78 : /*
79 : * The number of leaf entries is limited by the size of the block and the amount
80 : * of space used by the data entries. We don't know how much space is used by
81 : * the data entries yet, so just ensure that the count falls somewhere inside
82 : * the block right now.
83 : */
84 : static inline unsigned int
85 : xfs_dir2_data_max_leaf_entries(
86 : struct xfs_da_geometry *geo)
87 : {
88 71056103 : return (geo->blksize - sizeof(struct xfs_dir2_block_tail) -
89 71056103 : geo->data_entry_offset) /
90 : sizeof(struct xfs_dir2_leaf_entry);
91 : }
92 :
93 : /*
94 : * Check the consistency of the data block.
95 : * The input can also be a block-format directory.
96 : * Return NULL if the buffer is good, otherwise the address of the error.
97 : */
98 : xfs_failaddr_t
99 558130106 : __xfs_dir3_data_check(
100 : struct xfs_inode *dp, /* incore inode pointer */
101 : struct xfs_buf *bp) /* data block's buffer */
102 : {
103 558130106 : xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
104 558130106 : xfs_dir2_data_free_t *bf; /* bestfree table */
105 558130106 : xfs_dir2_block_tail_t *btp=NULL; /* block tail */
106 558130106 : int count; /* count of entries found */
107 558130106 : xfs_dir2_data_hdr_t *hdr; /* data block header */
108 558130106 : xfs_dir2_data_free_t *dfp; /* bestfree entry */
109 558130106 : int freeseen; /* mask of bestfrees seen */
110 558130106 : xfs_dahash_t hash; /* hash of current name */
111 558130106 : int i; /* leaf index */
112 558130106 : int lastfree; /* last entry was unused */
113 558130106 : xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
114 558130106 : struct xfs_mount *mp = bp->b_mount;
115 558130106 : int stale; /* count of stale leaves */
116 558130106 : struct xfs_name name;
117 558130106 : unsigned int offset;
118 558130106 : unsigned int end;
119 558130106 : struct xfs_da_geometry *geo = mp->m_dir_geo;
120 :
121 : /*
122 : * If this isn't a directory, something is seriously wrong. Bail out.
123 : */
124 558130106 : if (dp && !S_ISDIR(VFS_I(dp)->i_mode))
125 0 : return __this_address;
126 :
127 558130106 : hdr = bp->b_addr;
128 558130106 : offset = geo->data_entry_offset;
129 :
130 558130106 : switch (hdr->magic) {
131 : case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
132 : case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
133 71056103 : btp = xfs_dir2_block_tail_p(geo, hdr);
134 71056103 : lep = xfs_dir2_block_leaf_p(btp);
135 :
136 71056103 : if (be32_to_cpu(btp->count) >=
137 : xfs_dir2_data_max_leaf_entries(geo))
138 0 : return __this_address;
139 : break;
140 : case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
141 : case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
142 : break;
143 : default:
144 0 : return __this_address;
145 : }
146 558130106 : end = xfs_dir3_data_end_offset(geo, hdr);
147 558130106 : if (!end)
148 0 : return __this_address;
149 :
150 : /*
151 : * Account for zero bestfree entries.
152 : */
153 558130106 : bf = xfs_dir2_data_bestfree_p(mp, hdr);
154 558130106 : count = lastfree = freeseen = 0;
155 558130106 : if (!bf[0].length) {
156 149082110 : if (bf[0].offset)
157 0 : return __this_address;
158 : freeseen |= 1 << 0;
159 : }
160 558130106 : if (!bf[1].length) {
161 262223339 : if (bf[1].offset)
162 0 : return __this_address;
163 262223339 : freeseen |= 1 << 1;
164 : }
165 558130106 : if (!bf[2].length) {
166 307563969 : if (bf[2].offset)
167 0 : return __this_address;
168 307563969 : freeseen |= 1 << 2;
169 : }
170 :
171 558130106 : if (be16_to_cpu(bf[0].length) < be16_to_cpu(bf[1].length))
172 0 : return __this_address;
173 558130106 : if (be16_to_cpu(bf[1].length) < be16_to_cpu(bf[2].length))
174 0 : return __this_address;
175 : /*
176 : * Loop over the data/unused entries.
177 : */
178 80000997354 : while (offset < end) {
179 79441259658 : struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
180 79441259658 : struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
181 :
182 : /*
183 : * If it's unused, look for the space in the bestfree table.
184 : * If we find it, account for that, else make sure it
185 : * doesn't need to be there.
186 : */
187 79441259658 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
188 4599076603 : xfs_failaddr_t fa;
189 :
190 4599076603 : if (lastfree != 0)
191 0 : return __this_address;
192 4599076603 : if (offset + be16_to_cpu(dup->length) > end)
193 0 : return __this_address;
194 4599076603 : if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
195 : offset)
196 0 : return __this_address;
197 4599076603 : fa = xfs_dir2_data_freefind_verify(hdr, bf, dup, &dfp);
198 4612247131 : if (fa)
199 0 : return fa;
200 4612247131 : if (dfp) {
201 958538575 : i = (int)(dfp - bf);
202 958538575 : if ((freeseen & (1 << i)) != 0)
203 0 : return __this_address;
204 958538575 : freeseen |= 1 << i;
205 : } else {
206 3653708556 : if (be16_to_cpu(dup->length) >
207 3653708556 : be16_to_cpu(bf[2].length))
208 0 : return __this_address;
209 : }
210 4612247131 : offset += be16_to_cpu(dup->length);
211 4612247131 : lastfree = 1;
212 4612247131 : continue;
213 : }
214 : /*
215 : * It's a real entry. Validate the fields.
216 : * If this is a block directory then make sure it's
217 : * in the leaf section of the block.
218 : * The linear search is crude but this is DEBUG code.
219 : */
220 74842183055 : if (dep->namelen == 0)
221 0 : return __this_address;
222 74842183055 : if (!xfs_verify_dir_ino(mp, be64_to_cpu(dep->inumber)))
223 8 : return __this_address;
224 >15015*10^7 : if (offset + xfs_dir2_data_entsize(mp, dep->namelen) > end)
225 0 : return __this_address;
226 74834506919 : if (be16_to_cpu(*xfs_dir2_data_entry_tag_p(mp, dep)) != offset)
227 0 : return __this_address;
228 >14967*10^7 : if (xfs_dir2_data_get_ftype(mp, dep) >= XFS_DIR3_FT_MAX)
229 0 : return __this_address;
230 74834506919 : count++;
231 74834506919 : lastfree = 0;
232 74834506919 : if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
233 : hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
234 2638665605 : addr = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
235 : (xfs_dir2_data_aoff_t)
236 2638665605 : ((char *)dep - (char *)hdr));
237 2637484741 : name.name = dep->name;
238 2637484741 : name.len = dep->namelen;
239 2637484741 : hash = xfs_dir2_hashname(mp, &name);
240 >11384*10^7 : for (i = 0; i < be32_to_cpu(btp->count); i++) {
241 >11121*10^7 : if (be32_to_cpu(lep[i].address) == addr &&
242 2641080408 : be32_to_cpu(lep[i].hashval) == hash)
243 : break;
244 : }
245 2634778803 : if (i >= be32_to_cpu(btp->count))
246 0 : return __this_address;
247 : }
248 >14962*10^7 : offset += xfs_dir2_data_entsize(mp, dep->namelen);
249 : }
250 : /*
251 : * Need to have seen all the entries and all the bestfree slots.
252 : */
253 559737696 : if (freeseen != 7)
254 0 : return __this_address;
255 559737696 : if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
256 : hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
257 2964798867 : for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
258 2893681194 : if (lep[i].address ==
259 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
260 238759461 : stale++;
261 2893681194 : if (i > 0 && be32_to_cpu(lep[i].hashval) <
262 2822565581 : be32_to_cpu(lep[i - 1].hashval))
263 0 : return __this_address;
264 : }
265 71117673 : if (count != be32_to_cpu(btp->count) - be32_to_cpu(btp->stale))
266 0 : return __this_address;
267 71117673 : if (stale != be32_to_cpu(btp->stale))
268 0 : return __this_address;
269 : }
270 : return NULL;
271 : }
272 :
273 : #ifdef DEBUG
274 : void
275 483870279 : xfs_dir3_data_check(
276 : struct xfs_inode *dp,
277 : struct xfs_buf *bp)
278 : {
279 483870279 : xfs_failaddr_t fa;
280 :
281 483870279 : fa = __xfs_dir3_data_check(dp, bp);
282 484467458 : if (!fa)
283 : return;
284 0 : xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
285 0 : bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
286 : fa);
287 0 : ASSERT(0);
288 : }
289 : #endif
290 :
291 : static xfs_failaddr_t
292 72355890 : xfs_dir3_data_verify(
293 : struct xfs_buf *bp)
294 : {
295 72355890 : struct xfs_mount *mp = bp->b_mount;
296 72355890 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
297 :
298 72355890 : if (!xfs_verify_magic(bp, hdr3->magic))
299 0 : return __this_address;
300 :
301 72052587 : if (xfs_has_crc(mp)) {
302 72053449 : if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
303 0 : return __this_address;
304 72036386 : if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
305 0 : return __this_address;
306 72036386 : if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
307 0 : return __this_address;
308 : }
309 72243591 : return __xfs_dir3_data_check(NULL, bp);
310 : }
311 :
312 : /*
313 : * Readahead of the first block of the directory when it is opened is completely
314 : * oblivious to the format of the directory. Hence we can either get a block
315 : * format buffer or a data format buffer on readahead.
316 : */
317 : static void
318 124155 : xfs_dir3_data_reada_verify(
319 : struct xfs_buf *bp)
320 : {
321 124155 : struct xfs_dir2_data_hdr *hdr = bp->b_addr;
322 :
323 124155 : switch (hdr->magic) {
324 11341 : case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
325 : case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
326 11341 : bp->b_ops = &xfs_dir3_block_buf_ops;
327 11341 : bp->b_ops->verify_read(bp);
328 11341 : return;
329 112814 : case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
330 : case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
331 112814 : bp->b_ops = &xfs_dir3_data_buf_ops;
332 112814 : bp->b_ops->verify_read(bp);
333 112814 : return;
334 : default:
335 0 : xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
336 0 : break;
337 : }
338 : }
339 :
340 : static void
341 283767 : xfs_dir3_data_read_verify(
342 : struct xfs_buf *bp)
343 : {
344 283767 : struct xfs_mount *mp = bp->b_mount;
345 283767 : xfs_failaddr_t fa;
346 :
347 567534 : if (xfs_has_crc(mp) &&
348 : !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
349 90 : xfs_verifier_error(bp, -EFSBADCRC, __this_address);
350 : else {
351 283677 : fa = xfs_dir3_data_verify(bp);
352 283677 : if (fa)
353 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
354 : }
355 283767 : }
356 :
357 : static void
358 1327664 : xfs_dir3_data_write_verify(
359 : struct xfs_buf *bp)
360 : {
361 1327664 : struct xfs_mount *mp = bp->b_mount;
362 1327664 : struct xfs_buf_log_item *bip = bp->b_log_item;
363 1327664 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
364 1327664 : xfs_failaddr_t fa;
365 :
366 1327664 : fa = xfs_dir3_data_verify(bp);
367 1327664 : if (fa) {
368 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
369 0 : return;
370 : }
371 :
372 1327664 : if (!xfs_has_crc(mp))
373 : return;
374 :
375 1327664 : if (bip)
376 1327664 : hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
377 :
378 1327664 : xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
379 : }
380 :
381 : const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
382 : .name = "xfs_dir3_data",
383 : .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC),
384 : cpu_to_be32(XFS_DIR3_DATA_MAGIC) },
385 : .verify_read = xfs_dir3_data_read_verify,
386 : .verify_write = xfs_dir3_data_write_verify,
387 : .verify_struct = xfs_dir3_data_verify,
388 : };
389 :
390 : static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
391 : .name = "xfs_dir3_data_reada",
392 : .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC),
393 : cpu_to_be32(XFS_DIR3_DATA_MAGIC) },
394 : .verify_read = xfs_dir3_data_reada_verify,
395 : .verify_write = xfs_dir3_data_write_verify,
396 : };
397 :
398 : xfs_failaddr_t
399 752924432 : xfs_dir3_data_header_check(
400 : struct xfs_buf *bp,
401 : xfs_ino_t owner)
402 : {
403 752924432 : struct xfs_mount *mp = bp->b_mount;
404 :
405 752924432 : if (xfs_has_crc(mp)) {
406 752924432 : struct xfs_dir3_data_hdr *hdr3 = bp->b_addr;
407 :
408 752924432 : ASSERT(hdr3->hdr.magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
409 :
410 752924432 : if (be64_to_cpu(hdr3->hdr.owner) != owner)
411 0 : return __this_address;
412 : }
413 :
414 : return NULL;
415 : }
416 :
417 : int
418 752106325 : xfs_dir3_data_read(
419 : struct xfs_trans *tp,
420 : struct xfs_inode *dp,
421 : xfs_ino_t owner,
422 : xfs_dablk_t bno,
423 : unsigned int flags,
424 : struct xfs_buf **bpp)
425 : {
426 752106325 : xfs_failaddr_t fa;
427 752106325 : int err;
428 :
429 752106325 : err = xfs_da_read_buf(tp, dp, bno, flags, bpp, XFS_DATA_FORK,
430 : &xfs_dir3_data_buf_ops);
431 751649891 : if (err || !*bpp)
432 : return err;
433 :
434 : /* Check things that we can't do in the verifier. */
435 751648644 : fa = xfs_dir3_data_header_check(*bpp, owner);
436 752662974 : if (fa) {
437 0 : __xfs_buf_mark_corrupt(*bpp, fa);
438 0 : xfs_trans_brelse(tp, *bpp);
439 0 : *bpp = NULL;
440 0 : xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
441 0 : return -EFSCORRUPTED;
442 : }
443 :
444 752662974 : xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
445 752662974 : return err;
446 : }
447 :
448 : int
449 193078513 : xfs_dir3_data_readahead(
450 : struct xfs_inode *dp,
451 : xfs_dablk_t bno,
452 : unsigned int flags)
453 : {
454 193078513 : return xfs_da_reada_buf(dp, bno, flags, XFS_DATA_FORK,
455 : &xfs_dir3_data_reada_buf_ops);
456 : }
457 :
458 : /*
459 : * Find the bestfree entry that exactly coincides with unused directory space
460 : * or a verifier error because the bestfree data are bad.
461 : */
462 : static xfs_failaddr_t
463 4602967989 : xfs_dir2_data_freefind_verify(
464 : struct xfs_dir2_data_hdr *hdr,
465 : struct xfs_dir2_data_free *bf,
466 : struct xfs_dir2_data_unused *dup,
467 : struct xfs_dir2_data_free **bf_ent)
468 : {
469 4602967989 : struct xfs_dir2_data_free *dfp;
470 4602967989 : xfs_dir2_data_aoff_t off;
471 4602967989 : bool matched = false;
472 4602967989 : bool seenzero = false;
473 :
474 4602967989 : *bf_ent = NULL;
475 4602967989 : off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
476 :
477 : /*
478 : * Validate some consistency in the bestfree table.
479 : * Check order, non-overlapping entries, and if we find the
480 : * one we're looking for it has to be exact.
481 : */
482 18416761468 : for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
483 13813793479 : if (!dfp->offset) {
484 317616353 : if (dfp->length)
485 0 : return __this_address;
486 317616353 : seenzero = true;
487 317616353 : continue;
488 : }
489 13496177126 : if (seenzero)
490 0 : return __this_address;
491 13496177126 : if (be16_to_cpu(dfp->offset) == off) {
492 958443024 : matched = true;
493 958443024 : if (dfp->length != dup->length)
494 0 : return __this_address;
495 12537734102 : } else if (be16_to_cpu(dfp->offset) > off) {
496 4333155730 : if (off + be16_to_cpu(dup->length) >
497 : be16_to_cpu(dfp->offset))
498 0 : return __this_address;
499 : } else {
500 8204578372 : if (be16_to_cpu(dfp->offset) +
501 8204578372 : be16_to_cpu(dfp->length) > off)
502 0 : return __this_address;
503 : }
504 12537734102 : if (!matched &&
505 11728552867 : be16_to_cpu(dfp->length) < be16_to_cpu(dup->length))
506 0 : return __this_address;
507 13496177126 : if (dfp > &bf[0] &&
508 8881659985 : be16_to_cpu(dfp[-1].length) < be16_to_cpu(dfp[0].length))
509 0 : return __this_address;
510 : }
511 :
512 : /* Looks ok so far; now try to match up with a bestfree entry. */
513 4602967989 : *bf_ent = xfs_dir2_data_freefind(hdr, bf, dup);
514 4602967989 : return NULL;
515 : }
516 :
517 : /*
518 : * Given a data block and an unused entry from that block,
519 : * return the bestfree entry if any that corresponds to it.
520 : */
521 : xfs_dir2_data_free_t *
522 4714760811 : xfs_dir2_data_freefind(
523 : struct xfs_dir2_data_hdr *hdr, /* data block header */
524 : struct xfs_dir2_data_free *bf, /* bestfree table pointer */
525 : struct xfs_dir2_data_unused *dup) /* unused space */
526 : {
527 4714760811 : xfs_dir2_data_free_t *dfp; /* bestfree entry */
528 4714760811 : xfs_dir2_data_aoff_t off; /* offset value needed */
529 :
530 4714760811 : off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
531 :
532 : /*
533 : * If this is smaller than the smallest bestfree entry,
534 : * it can't be there since they're sorted.
535 : */
536 4714760811 : if (be16_to_cpu(dup->length) <
537 4714760811 : be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
538 : return NULL;
539 : /*
540 : * Look at the three bestfree entries for our guy.
541 : */
542 9174877649 : for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
543 7354134166 : if (!dfp->offset)
544 : return NULL;
545 7354134166 : if (be16_to_cpu(dfp->offset) == off)
546 1058110664 : return dfp;
547 : }
548 : /*
549 : * Didn't find it. This only happens if there are duplicate lengths.
550 : */
551 : return NULL;
552 : }
553 :
554 : /*
555 : * Insert an unused-space entry into the bestfree table.
556 : */
557 : xfs_dir2_data_free_t * /* entry inserted */
558 701149576 : xfs_dir2_data_freeinsert(
559 : struct xfs_dir2_data_hdr *hdr, /* data block pointer */
560 : struct xfs_dir2_data_free *dfp, /* bestfree table pointer */
561 : struct xfs_dir2_data_unused *dup, /* unused space */
562 : int *loghead) /* log the data header (out) */
563 : {
564 701149576 : xfs_dir2_data_free_t new; /* new bestfree entry */
565 :
566 701149576 : ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
567 : hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
568 : hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
569 : hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
570 :
571 701149576 : new.length = dup->length;
572 701149576 : new.offset = cpu_to_be16((char *)dup - (char *)hdr);
573 :
574 : /*
575 : * Insert at position 0, 1, or 2; or not at all.
576 : */
577 701149576 : if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
578 116370055 : dfp[2] = dfp[1];
579 116370055 : dfp[1] = dfp[0];
580 116370055 : dfp[0] = new;
581 116370055 : *loghead = 1;
582 116370055 : return &dfp[0];
583 : }
584 584779521 : if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
585 47065603 : dfp[2] = dfp[1];
586 47065603 : dfp[1] = new;
587 47065603 : *loghead = 1;
588 47065603 : return &dfp[1];
589 : }
590 537713918 : if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
591 44560559 : dfp[2] = new;
592 44560559 : *loghead = 1;
593 44560559 : return &dfp[2];
594 : }
595 : return NULL;
596 : }
597 :
598 : /*
599 : * Remove a bestfree entry from the table.
600 : */
601 : STATIC void
602 88408799 : xfs_dir2_data_freeremove(
603 : struct xfs_dir2_data_hdr *hdr, /* data block header */
604 : struct xfs_dir2_data_free *bf, /* bestfree table pointer */
605 : struct xfs_dir2_data_free *dfp, /* bestfree entry pointer */
606 : int *loghead) /* out: log data header */
607 : {
608 :
609 88408799 : ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
610 : hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
611 : hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
612 : hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
613 :
614 : /*
615 : * It's the first entry, slide the next 2 up.
616 : */
617 88408799 : if (dfp == &bf[0]) {
618 83261774 : bf[0] = bf[1];
619 83261774 : bf[1] = bf[2];
620 : }
621 : /*
622 : * It's the second entry, slide the 3rd entry up.
623 : */
624 5147025 : else if (dfp == &bf[1])
625 4439650 : bf[1] = bf[2];
626 : /*
627 : * Must be the last entry.
628 : */
629 : else
630 707375 : ASSERT(dfp == &bf[2]);
631 : /*
632 : * Clear the 3rd entry, must be zero now.
633 : */
634 88408799 : bf[2].length = 0;
635 88408799 : bf[2].offset = 0;
636 88408799 : *loghead = 1;
637 88408799 : }
638 :
639 : /*
640 : * Given a data block, reconstruct its bestfree map.
641 : */
642 : void
643 23983515 : xfs_dir2_data_freescan(
644 : struct xfs_mount *mp,
645 : struct xfs_dir2_data_hdr *hdr,
646 : int *loghead)
647 : {
648 23983515 : struct xfs_da_geometry *geo = mp->m_dir_geo;
649 23983515 : struct xfs_dir2_data_free *bf = xfs_dir2_data_bestfree_p(mp, hdr);
650 23983515 : void *addr = hdr;
651 23983515 : unsigned int offset = geo->data_entry_offset;
652 23983515 : unsigned int end;
653 :
654 23983515 : ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
655 : hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
656 : hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
657 : hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
658 :
659 : /*
660 : * Start by clearing the table.
661 : */
662 23983515 : memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
663 23983515 : *loghead = 1;
664 :
665 23983515 : end = xfs_dir3_data_end_offset(geo, addr);
666 3592355396 : while (offset < end) {
667 3568344302 : struct xfs_dir2_data_unused *dup = addr + offset;
668 3568344302 : struct xfs_dir2_data_entry *dep = addr + offset;
669 :
670 : /*
671 : * If it's a free entry, insert it.
672 : */
673 3568344302 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
674 596082121 : ASSERT(offset ==
675 : be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
676 596082121 : xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
677 596109700 : offset += be16_to_cpu(dup->length);
678 596109700 : continue;
679 : }
680 :
681 : /*
682 : * For active entries, check their tags and skip them.
683 : */
684 2972262181 : ASSERT(offset ==
685 : be16_to_cpu(*xfs_dir2_data_entry_tag_p(mp, dep)));
686 5944411701 : offset += xfs_dir2_data_entsize(mp, dep->namelen);
687 : }
688 24011094 : }
689 :
690 : /*
691 : * Initialize a data block at the given block number in the directory.
692 : * Give back the buffer for the created block.
693 : */
694 : int /* error */
695 855483 : xfs_dir3_data_init(
696 : struct xfs_da_args *args, /* directory operation args */
697 : xfs_dir2_db_t blkno, /* logical dir block number */
698 : struct xfs_buf **bpp) /* output block buffer */
699 : {
700 855483 : struct xfs_trans *tp = args->trans;
701 855483 : struct xfs_inode *dp = args->dp;
702 855483 : struct xfs_mount *mp = dp->i_mount;
703 855483 : struct xfs_da_geometry *geo = args->geo;
704 855483 : struct xfs_buf *bp;
705 855483 : struct xfs_dir2_data_hdr *hdr;
706 855483 : struct xfs_dir2_data_unused *dup;
707 855483 : struct xfs_dir2_data_free *bf;
708 855483 : int error;
709 855483 : int i;
710 :
711 : /*
712 : * Get the buffer set up for the block.
713 : */
714 855483 : error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
715 : &bp, XFS_DATA_FORK);
716 855510 : if (error)
717 : return error;
718 855503 : bp->b_ops = &xfs_dir3_data_buf_ops;
719 855503 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
720 :
721 : /*
722 : * Initialize the header.
723 : */
724 855483 : hdr = bp->b_addr;
725 855483 : if (xfs_has_crc(mp)) {
726 855461 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
727 :
728 855461 : memset(hdr3, 0, sizeof(*hdr3));
729 855461 : hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
730 855461 : hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp));
731 855461 : hdr3->owner = cpu_to_be64(args->owner);
732 855461 : uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
733 :
734 : } else
735 22 : hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
736 :
737 855481 : bf = xfs_dir2_data_bestfree_p(mp, hdr);
738 855481 : bf[0].offset = cpu_to_be16(geo->data_entry_offset);
739 855481 : bf[0].length = cpu_to_be16(geo->blksize - geo->data_entry_offset);
740 2566443 : for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
741 1710962 : bf[i].length = 0;
742 1710962 : bf[i].offset = 0;
743 : }
744 :
745 : /*
746 : * Set up an unused entry for the block's body.
747 : */
748 855481 : dup = bp->b_addr + geo->data_entry_offset;
749 855481 : dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
750 855481 : dup->length = bf[0].length;
751 855481 : *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
752 :
753 : /*
754 : * Log it and return it.
755 : */
756 855481 : xfs_dir2_data_log_header(args, bp);
757 855531 : xfs_dir2_data_log_unused(args, bp, dup);
758 855524 : *bpp = bp;
759 855524 : return 0;
760 : }
761 :
762 : /*
763 : * Log an active data entry from the block.
764 : */
765 : void
766 82037416 : xfs_dir2_data_log_entry(
767 : struct xfs_da_args *args,
768 : struct xfs_buf *bp,
769 : xfs_dir2_data_entry_t *dep) /* data entry pointer */
770 : {
771 82037416 : struct xfs_mount *mp = bp->b_mount;
772 82037416 : struct xfs_dir2_data_hdr *hdr = bp->b_addr;
773 :
774 82037416 : ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
775 : hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
776 : hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
777 : hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
778 :
779 82037416 : xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr),
780 82037416 : (uint)((char *)(xfs_dir2_data_entry_tag_p(mp, dep) + 1) -
781 82037416 : (char *)hdr - 1));
782 82268235 : }
783 :
784 : /*
785 : * Log a data block header.
786 : */
787 : void
788 111120415 : xfs_dir2_data_log_header(
789 : struct xfs_da_args *args,
790 : struct xfs_buf *bp)
791 : {
792 : #ifdef DEBUG
793 111120415 : struct xfs_dir2_data_hdr *hdr = bp->b_addr;
794 :
795 111120415 : ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
796 : hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
797 : hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
798 : hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
799 : #endif
800 :
801 111120415 : xfs_trans_log_buf(args->trans, bp, 0, args->geo->data_entry_offset - 1);
802 111310381 : }
803 :
804 : /*
805 : * Log a data unused entry.
806 : */
807 : void
808 114935432 : xfs_dir2_data_log_unused(
809 : struct xfs_da_args *args,
810 : struct xfs_buf *bp,
811 : xfs_dir2_data_unused_t *dup) /* data unused pointer */
812 : {
813 114935432 : xfs_dir2_data_hdr_t *hdr = bp->b_addr;
814 :
815 114935432 : ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
816 : hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
817 : hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
818 : hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
819 :
820 : /*
821 : * Log the first part of the unused entry.
822 : */
823 114935432 : xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
824 114935432 : (uint)((char *)&dup->length + sizeof(dup->length) -
825 114935432 : 1 - (char *)hdr));
826 : /*
827 : * Log the end (tag) of the unused entry.
828 : */
829 115113767 : xfs_trans_log_buf(args->trans, bp,
830 : (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
831 115113767 : (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
832 : sizeof(xfs_dir2_data_off_t) - 1));
833 115086041 : }
834 :
835 : /*
836 : * Make a byte range in the data block unused.
837 : * Its current contents are unimportant.
838 : */
839 : void
840 57301487 : xfs_dir2_data_make_free(
841 : struct xfs_da_args *args,
842 : struct xfs_buf *bp,
843 : xfs_dir2_data_aoff_t offset, /* starting byte offset */
844 : xfs_dir2_data_aoff_t len, /* length in bytes */
845 : int *needlogp, /* out: log header */
846 : int *needscanp) /* out: regen bestfree */
847 : {
848 57301487 : xfs_dir2_data_hdr_t *hdr; /* data block pointer */
849 57301487 : xfs_dir2_data_free_t *dfp; /* bestfree pointer */
850 57301487 : int needscan; /* need to regen bestfree */
851 57301487 : xfs_dir2_data_unused_t *newdup; /* new unused entry */
852 57301487 : xfs_dir2_data_unused_t *postdup; /* unused entry after us */
853 57301487 : xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
854 57301487 : unsigned int end;
855 57301487 : struct xfs_dir2_data_free *bf;
856 :
857 57301487 : hdr = bp->b_addr;
858 :
859 : /*
860 : * Figure out where the end of the data area is.
861 : */
862 57301487 : end = xfs_dir3_data_end_offset(args->geo, hdr);
863 57301487 : ASSERT(end != 0);
864 :
865 : /*
866 : * If this isn't the start of the block, then back up to
867 : * the previous entry and see if it's free.
868 : */
869 57301487 : if (offset > args->geo->data_entry_offset) {
870 57040717 : __be16 *tagp; /* tag just before us */
871 :
872 57040717 : tagp = (__be16 *)((char *)hdr + offset) - 1;
873 57040717 : prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
874 57040717 : if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
875 29500665 : prevdup = NULL;
876 : } else
877 : prevdup = NULL;
878 : /*
879 : * If this isn't the end of the block, see if the entry after
880 : * us is free.
881 : */
882 57301487 : if (offset + len < end) {
883 57085360 : postdup =
884 57085360 : (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
885 57085360 : if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
886 49250906 : postdup = NULL;
887 : } else
888 : postdup = NULL;
889 57301487 : ASSERT(*needscanp == 0);
890 57301487 : needscan = 0;
891 : /*
892 : * Previous and following entries are both free,
893 : * merge everything into a single free entry.
894 : */
895 57301487 : bf = xfs_dir2_data_bestfree_p(args->dp->i_mount, hdr);
896 57301487 : if (prevdup && postdup) {
897 3094649 : xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
898 :
899 : /*
900 : * See if prevdup and/or postdup are in bestfree table.
901 : */
902 3094649 : dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
903 3094649 : dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
904 : /*
905 : * We need a rescan unless there are exactly 2 free entries
906 : * namely our two. Then we know what's happening, otherwise
907 : * since the third bestfree is there, there might be more
908 : * entries.
909 : */
910 3094649 : needscan = (bf[2].length != 0);
911 : /*
912 : * Fix up the new big freespace.
913 : */
914 3094649 : be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
915 3094649 : *xfs_dir2_data_unused_tag_p(prevdup) =
916 3094649 : cpu_to_be16((char *)prevdup - (char *)hdr);
917 3094649 : xfs_dir2_data_log_unused(args, bp, prevdup);
918 3094755 : if (!needscan) {
919 : /*
920 : * Has to be the case that entries 0 and 1 are
921 : * dfp and dfp2 (don't know which is which), and
922 : * entry 2 is empty.
923 : * Remove entry 1 first then entry 0.
924 : */
925 357992 : ASSERT(dfp && dfp2);
926 357992 : if (dfp == &bf[1]) {
927 273206 : dfp = &bf[0];
928 273206 : ASSERT(dfp2 == dfp);
929 : dfp2 = &bf[1];
930 : }
931 357992 : xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
932 357992 : xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
933 : /*
934 : * Now insert the new entry.
935 : */
936 357992 : dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
937 : needlogp);
938 357993 : ASSERT(dfp == &bf[0]);
939 357993 : ASSERT(dfp->length == prevdup->length);
940 357993 : ASSERT(!dfp[1].length);
941 357993 : ASSERT(!dfp[2].length);
942 : }
943 : }
944 : /*
945 : * The entry before us is free, merge with it.
946 : */
947 54206838 : else if (prevdup) {
948 24444521 : dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
949 24444521 : be16_add_cpu(&prevdup->length, len);
950 24444521 : *xfs_dir2_data_unused_tag_p(prevdup) =
951 24444521 : cpu_to_be16((char *)prevdup - (char *)hdr);
952 24444521 : xfs_dir2_data_log_unused(args, bp, prevdup);
953 : /*
954 : * If the previous entry was in the table, the new entry
955 : * is longer, so it will be in the table too. Remove
956 : * the old one and add the new one.
957 : */
958 24447356 : if (dfp) {
959 21421456 : xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
960 21418610 : xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
961 : }
962 : /*
963 : * Otherwise we need a scan if the new entry is big enough.
964 : */
965 : else {
966 3025900 : needscan = be16_to_cpu(prevdup->length) >
967 3025900 : be16_to_cpu(bf[2].length);
968 : }
969 : }
970 : /*
971 : * The following entry is free, merge with it.
972 : */
973 29762317 : else if (postdup) {
974 4740270 : dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
975 4740270 : newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
976 4740270 : newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
977 4740270 : newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
978 4740270 : *xfs_dir2_data_unused_tag_p(newdup) =
979 4740270 : cpu_to_be16((char *)newdup - (char *)hdr);
980 4740270 : xfs_dir2_data_log_unused(args, bp, newdup);
981 : /*
982 : * If the following entry was in the table, the new entry
983 : * is longer, so it will be in the table too. Remove
984 : * the old one and add the new one.
985 : */
986 4740713 : if (dfp) {
987 1115011 : xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
988 1114994 : xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
989 : }
990 : /*
991 : * Otherwise we need a scan if the new entry is big enough.
992 : */
993 : else {
994 3625702 : needscan = be16_to_cpu(newdup->length) >
995 3625702 : be16_to_cpu(bf[2].length);
996 : }
997 : }
998 : /*
999 : * Neither neighbor is free. Make a new entry.
1000 : */
1001 : else {
1002 25022047 : newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
1003 25022047 : newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1004 25022047 : newdup->length = cpu_to_be16(len);
1005 25022047 : *xfs_dir2_data_unused_tag_p(newdup) =
1006 25022047 : cpu_to_be16((char *)newdup - (char *)hdr);
1007 25022047 : xfs_dir2_data_log_unused(args, bp, newdup);
1008 25061917 : xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
1009 : }
1010 57318388 : *needscanp = needscan;
1011 57318388 : }
1012 :
1013 : /* Check our free data for obvious signs of corruption. */
1014 : static inline xfs_failaddr_t
1015 75505472 : xfs_dir2_data_check_free(
1016 : struct xfs_dir2_data_hdr *hdr,
1017 : struct xfs_dir2_data_unused *dup,
1018 : xfs_dir2_data_aoff_t offset,
1019 : xfs_dir2_data_aoff_t len)
1020 : {
1021 75505472 : if (hdr->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC) &&
1022 14556624 : hdr->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC) &&
1023 14554688 : hdr->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) &&
1024 : hdr->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
1025 0 : return __this_address;
1026 75505472 : if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG)
1027 0 : return __this_address;
1028 75505472 : if (offset < (char *)dup - (char *)hdr)
1029 0 : return __this_address;
1030 75505472 : if (offset + len > (char *)dup + be16_to_cpu(dup->length) - (char *)hdr)
1031 0 : return __this_address;
1032 75505472 : if ((char *)dup - (char *)hdr !=
1033 75505472 : be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)))
1034 0 : return __this_address;
1035 : return NULL;
1036 : }
1037 :
1038 : /* Sanity-check a new bestfree entry. */
1039 : static inline xfs_failaddr_t
1040 53518004 : xfs_dir2_data_check_new_free(
1041 : struct xfs_dir2_data_hdr *hdr,
1042 : struct xfs_dir2_data_free *dfp,
1043 : struct xfs_dir2_data_unused *newdup)
1044 : {
1045 53518004 : if (dfp == NULL)
1046 0 : return __this_address;
1047 53518004 : if (dfp->length != newdup->length)
1048 0 : return __this_address;
1049 53518004 : if (be16_to_cpu(dfp->offset) != (char *)newdup - (char *)hdr)
1050 0 : return __this_address;
1051 : return NULL;
1052 : }
1053 :
1054 : /*
1055 : * Take a byte range out of an existing unused space and make it un-free.
1056 : */
1057 : int
1058 75489719 : xfs_dir2_data_use_free(
1059 : struct xfs_da_args *args,
1060 : struct xfs_buf *bp,
1061 : xfs_dir2_data_unused_t *dup, /* unused entry */
1062 : xfs_dir2_data_aoff_t offset, /* starting offset to use */
1063 : xfs_dir2_data_aoff_t len, /* length to use */
1064 : int *needlogp, /* out: need to log header */
1065 : int *needscanp) /* out: need regen bestfree */
1066 : {
1067 75489719 : xfs_dir2_data_hdr_t *hdr; /* data block header */
1068 75489719 : xfs_dir2_data_free_t *dfp; /* bestfree pointer */
1069 75489719 : xfs_dir2_data_unused_t *newdup; /* new unused entry */
1070 75489719 : xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
1071 75489719 : struct xfs_dir2_data_free *bf;
1072 75489719 : xfs_failaddr_t fa;
1073 75489719 : int matchback; /* matches end of freespace */
1074 75489719 : int matchfront; /* matches start of freespace */
1075 75489719 : int needscan; /* need to regen bestfree */
1076 75489719 : int oldlen; /* old unused entry's length */
1077 :
1078 75489719 : hdr = bp->b_addr;
1079 75489719 : fa = xfs_dir2_data_check_free(hdr, dup, offset, len);
1080 75482497 : if (fa)
1081 0 : goto corrupt;
1082 : /*
1083 : * Look up the entry in the bestfree table.
1084 : */
1085 75482497 : oldlen = be16_to_cpu(dup->length);
1086 75482497 : bf = xfs_dir2_data_bestfree_p(args->dp->i_mount, hdr);
1087 75482497 : dfp = xfs_dir2_data_freefind(hdr, bf, dup);
1088 75482497 : ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
1089 : /*
1090 : * Check for alignment with front and back of the entry.
1091 : */
1092 75482497 : matchfront = (char *)dup - (char *)hdr == offset;
1093 75482497 : matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
1094 75482497 : ASSERT(*needscanp == 0);
1095 75482497 : needscan = 0;
1096 : /*
1097 : * If we matched it exactly we just need to get rid of it from
1098 : * the bestfree table.
1099 : */
1100 75482497 : if (matchfront && matchback) {
1101 21693808 : if (dfp) {
1102 21690427 : needscan = (bf[2].offset != 0);
1103 21690427 : if (!needscan)
1104 11534049 : xfs_dir2_data_freeremove(hdr, bf, dfp,
1105 : needlogp);
1106 : }
1107 : }
1108 : /*
1109 : * We match the first part of the entry.
1110 : * Make a new entry with the remaining freespace.
1111 : */
1112 53788689 : else if (matchfront) {
1113 47663960 : newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1114 47663960 : newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1115 47663960 : newdup->length = cpu_to_be16(oldlen - len);
1116 47663960 : *xfs_dir2_data_unused_tag_p(newdup) =
1117 47663960 : cpu_to_be16((char *)newdup - (char *)hdr);
1118 47663960 : xfs_dir2_data_log_unused(args, bp, newdup);
1119 : /*
1120 : * If it was in the table, remove it and add the new one.
1121 : */
1122 47685487 : if (dfp) {
1123 47684765 : xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1124 47520942 : dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1125 : needlogp);
1126 47478330 : fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1127 47428590 : if (fa)
1128 0 : goto corrupt;
1129 : /*
1130 : * If we got inserted at the last slot,
1131 : * that means we don't know if there was a better
1132 : * choice for the last slot, or not. Rescan.
1133 : */
1134 47428590 : needscan = dfp == &bf[2];
1135 : }
1136 : }
1137 : /*
1138 : * We match the last part of the entry.
1139 : * Trim the allocated space off the tail of the entry.
1140 : */
1141 6124729 : else if (matchback) {
1142 6124729 : newdup = dup;
1143 6124729 : newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1144 6124729 : *xfs_dir2_data_unused_tag_p(newdup) =
1145 6124729 : cpu_to_be16((char *)newdup - (char *)hdr);
1146 6124729 : xfs_dir2_data_log_unused(args, bp, newdup);
1147 : /*
1148 : * If it was in the table, remove it and add the new one.
1149 : */
1150 6127021 : if (dfp) {
1151 6087383 : xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1152 6080893 : dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1153 : needlogp);
1154 6081495 : fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1155 6079922 : if (fa)
1156 0 : goto corrupt;
1157 : /*
1158 : * If we got inserted at the last slot,
1159 : * that means we don't know if there was a better
1160 : * choice for the last slot, or not. Rescan.
1161 : */
1162 6079922 : needscan = dfp == &bf[2];
1163 : }
1164 : }
1165 : /*
1166 : * Poking out the middle of an entry.
1167 : * Make two new entries.
1168 : */
1169 : else {
1170 0 : newdup = dup;
1171 0 : newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1172 0 : *xfs_dir2_data_unused_tag_p(newdup) =
1173 0 : cpu_to_be16((char *)newdup - (char *)hdr);
1174 0 : xfs_dir2_data_log_unused(args, bp, newdup);
1175 0 : newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1176 0 : newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1177 0 : newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
1178 0 : *xfs_dir2_data_unused_tag_p(newdup2) =
1179 0 : cpu_to_be16((char *)newdup2 - (char *)hdr);
1180 0 : xfs_dir2_data_log_unused(args, bp, newdup2);
1181 : /*
1182 : * If the old entry was in the table, we need to scan
1183 : * if the 3rd entry was valid, since these entries
1184 : * are smaller than the old one.
1185 : * If we don't need to scan that means there were 1 or 2
1186 : * entries in the table, and removing the old and adding
1187 : * the 2 new will work.
1188 : */
1189 0 : if (dfp) {
1190 0 : needscan = (bf[2].length != 0);
1191 0 : if (!needscan) {
1192 0 : xfs_dir2_data_freeremove(hdr, bf, dfp,
1193 : needlogp);
1194 0 : xfs_dir2_data_freeinsert(hdr, bf, newdup,
1195 : needlogp);
1196 0 : xfs_dir2_data_freeinsert(hdr, bf, newdup2,
1197 : needlogp);
1198 : }
1199 : }
1200 : }
1201 75238231 : *needscanp = needscan;
1202 75238231 : return 0;
1203 0 : corrupt:
1204 0 : xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, args->dp->i_mount,
1205 : hdr, sizeof(*hdr), __FILE__, __LINE__, fa);
1206 0 : xfs_da_mark_sick(args);
1207 0 : return -EFSCORRUPTED;
1208 : }
1209 :
1210 : /* Find the end of the entry data in a data/block format dir block. */
1211 : unsigned int
1212 908033651 : xfs_dir3_data_end_offset(
1213 : struct xfs_da_geometry *geo,
1214 : struct xfs_dir2_data_hdr *hdr)
1215 : {
1216 908033651 : void *p;
1217 :
1218 908033651 : switch (hdr->magic) {
1219 : case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
1220 : case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
1221 274056697 : p = xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo, hdr));
1222 274056697 : return p - (void *)hdr;
1223 633976954 : case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
1224 : case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
1225 633976954 : return geo->blksize;
1226 : default:
1227 : return 0;
1228 : }
1229 : }
|