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 76989652 : xfs_dir2_data_bestfree_p(
30 : struct xfs_mount *mp,
31 : struct xfs_dir2_data_hdr *hdr)
32 : {
33 76989652 : if (xfs_has_crc(mp))
34 596242156 : return ((struct xfs_dir3_data_hdr *)hdr)->best_free;
35 20945 : return hdr->bestfree;
36 : }
37 :
38 : /*
39 : * Pointer to an entry's tag word.
40 : */
41 : __be16 *
42 60477854948 : xfs_dir2_data_entry_tag_p(
43 : struct xfs_mount *mp,
44 : struct xfs_dir2_data_entry *dep)
45 : {
46 >12095*10^7 : return (__be16 *)((char *)dep +
47 60477854948 : xfs_dir2_data_entsize(mp, dep->namelen) - sizeof(__be16));
48 : }
49 :
50 : uint8_t
51 25220500931 : xfs_dir2_data_get_ftype(
52 : struct xfs_mount *mp,
53 : struct xfs_dir2_data_entry *dep)
54 : {
55 25220500931 : if (xfs_has_ftype(mp)) {
56 85747605807 : uint8_t ftype = dep->name[dep->namelen];
57 :
58 85747605807 : if (likely(ftype < XFS_DIR3_FT_MAX))
59 25900481382 : return ftype;
60 : }
61 :
62 : return XFS_DIR3_FT_UNKNOWN;
63 : }
64 :
65 : void
66 46128036 : xfs_dir2_data_put_ftype(
67 : struct xfs_mount *mp,
68 : struct xfs_dir2_data_entry *dep,
69 : uint8_t ftype)
70 : {
71 46128036 : ASSERT(ftype < XFS_DIR3_FT_MAX);
72 46128036 : ASSERT(dep->namelen != 0);
73 :
74 46128036 : if (xfs_has_ftype(mp))
75 46127127 : dep->name[dep->namelen] = ftype;
76 46128036 : }
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 71243408 : return (geo->blksize - sizeof(struct xfs_dir2_block_tail) -
89 71243408 : 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 417931117 : __xfs_dir3_data_check(
100 : struct xfs_inode *dp, /* incore inode pointer */
101 : struct xfs_buf *bp) /* data block's buffer */
102 : {
103 417931117 : xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
104 417931117 : xfs_dir2_data_free_t *bf; /* bestfree table */
105 417931117 : xfs_dir2_block_tail_t *btp=NULL; /* block tail */
106 417931117 : int count; /* count of entries found */
107 417931117 : xfs_dir2_data_hdr_t *hdr; /* data block header */
108 417931117 : xfs_dir2_data_free_t *dfp; /* bestfree entry */
109 417931117 : int freeseen; /* mask of bestfrees seen */
110 417931117 : xfs_dahash_t hash; /* hash of current name */
111 417931117 : int i; /* leaf index */
112 417931117 : int lastfree; /* last entry was unused */
113 417931117 : xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
114 417931117 : struct xfs_mount *mp = bp->b_mount;
115 417931117 : int stale; /* count of stale leaves */
116 417931117 : struct xfs_name name;
117 417931117 : unsigned int offset;
118 417931117 : unsigned int end;
119 417931117 : 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 417931117 : if (dp && !S_ISDIR(VFS_I(dp)->i_mode))
125 0 : return __this_address;
126 :
127 417931117 : hdr = bp->b_addr;
128 417931117 : offset = geo->data_entry_offset;
129 :
130 417931117 : switch (hdr->magic) {
131 : case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
132 : case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
133 71243408 : btp = xfs_dir2_block_tail_p(geo, hdr);
134 71243408 : lep = xfs_dir2_block_leaf_p(btp);
135 :
136 142486816 : 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 417931117 : end = xfs_dir3_data_end_offset(geo, hdr);
147 417931117 : if (!end)
148 0 : return __this_address;
149 :
150 : /*
151 : * Account for zero bestfree entries.
152 : */
153 417931117 : bf = xfs_dir2_data_bestfree_p(mp, hdr);
154 417931117 : count = lastfree = freeseen = 0;
155 417931117 : if (!bf[0].length) {
156 112883052 : if (bf[0].offset)
157 0 : return __this_address;
158 : freeseen |= 1 << 0;
159 : }
160 417931117 : if (!bf[1].length) {
161 172671912 : if (bf[1].offset)
162 0 : return __this_address;
163 172671912 : freeseen |= 1 << 1;
164 : }
165 417931117 : if (!bf[2].length) {
166 199240727 : if (bf[2].offset)
167 0 : return __this_address;
168 199240727 : freeseen |= 1 << 2;
169 : }
170 :
171 417931117 : if (be16_to_cpu(bf[0].length) < be16_to_cpu(bf[1].length))
172 0 : return __this_address;
173 417931117 : 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 64610072952 : while (offset < end) {
179 64192162707 : struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
180 64192162707 : 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 64192162707 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
188 4603040699 : xfs_failaddr_t fa;
189 :
190 4603040699 : if (lastfree != 0)
191 0 : return __this_address;
192 4603040699 : if (offset + be16_to_cpu(dup->length) > end)
193 0 : return __this_address;
194 4603040699 : if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
195 : offset)
196 0 : return __this_address;
197 4603040699 : fa = xfs_dir2_data_freefind_verify(hdr, bf, dup, &dfp);
198 4600550718 : if (fa)
199 0 : return fa;
200 4600550718 : if (dfp) {
201 755849885 : i = (int)(dfp - bf);
202 755849885 : if ((freeseen & (1 << i)) != 0)
203 0 : return __this_address;
204 755849885 : freeseen |= 1 << i;
205 : } else {
206 7689401666 : if (be16_to_cpu(dup->length) >
207 3844700833 : be16_to_cpu(bf[2].length))
208 0 : return __this_address;
209 : }
210 4600550718 : offset += be16_to_cpu(dup->length);
211 4600550718 : lastfree = 1;
212 4600550718 : 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 59589122008 : if (dep->namelen == 0)
221 0 : return __this_address;
222 59589122008 : if (!xfs_verify_dir_ino(mp, be64_to_cpu(dep->inumber)))
223 0 : return __this_address;
224 >11921*10^7 : if (offset + xfs_dir2_data_entsize(mp, dep->namelen) > end)
225 0 : return __this_address;
226 59588048017 : if (be16_to_cpu(*xfs_dir2_data_entry_tag_p(mp, dep)) != offset)
227 0 : return __this_address;
228 >11943*10^7 : if (xfs_dir2_data_get_ftype(mp, dep) >= XFS_DIR3_FT_MAX)
229 0 : return __this_address;
230 59588048017 : count++;
231 59588048017 : lastfree = 0;
232 59588048017 : if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
233 : hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
234 1727957664 : addr = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
235 : (xfs_dir2_data_aoff_t)
236 1727957664 : ((char *)dep - (char *)hdr));
237 1727957664 : name.name = dep->name;
238 1727957664 : name.len = dep->namelen;
239 1727957664 : hash = xfs_dir2_hashname(mp, &name);
240 89015158168 : for (i = 0; i < be32_to_cpu(btp->count); i++) {
241 89033841504 : if (be32_to_cpu(lep[i].address) == addr &&
242 1735541546 : be32_to_cpu(lep[i].hashval) == hash)
243 : break;
244 : }
245 1731500764 : if (i >= be32_to_cpu(btp->count))
246 0 : return __this_address;
247 : }
248 >11929*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 417910245 : if (freeseen != 7)
254 0 : return __this_address;
255 417910245 : if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
256 : hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
257 3923429478 : for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
258 1890461809 : if (lep[i].address ==
259 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
260 150259664 : stale++;
261 3709575516 : if (i > 0 && be32_to_cpu(lep[i].hashval) <
262 1819113707 : be32_to_cpu(lep[i - 1].hashval))
263 0 : return __this_address;
264 : }
265 142505860 : if (count != be32_to_cpu(btp->count) - be32_to_cpu(btp->stale))
266 918 : return __this_address;
267 71252621 : if (stale != be32_to_cpu(btp->stale))
268 0 : return __this_address;
269 : }
270 : return NULL;
271 : }
272 :
273 : #ifdef DEBUG
274 : void
275 364128851 : xfs_dir3_data_check(
276 : struct xfs_inode *dp,
277 : struct xfs_buf *bp)
278 : {
279 364128851 : xfs_failaddr_t fa;
280 :
281 364128851 : fa = __xfs_dir3_data_check(dp, bp);
282 364106420 : 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 51535036 : xfs_dir3_data_verify(
293 : struct xfs_buf *bp)
294 : {
295 51535036 : struct xfs_mount *mp = bp->b_mount;
296 51535036 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
297 :
298 51535036 : if (!xfs_verify_magic(bp, hdr3->magic))
299 0 : return __this_address;
300 :
301 51536071 : if (xfs_has_crc(mp)) {
302 51536085 : if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
303 0 : return __this_address;
304 51534753 : if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
305 0 : return __this_address;
306 51536072 : if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
307 0 : return __this_address;
308 : }
309 51536326 : 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 53510 : xfs_dir3_data_reada_verify(
319 : struct xfs_buf *bp)
320 : {
321 53510 : struct xfs_dir2_data_hdr *hdr = bp->b_addr;
322 :
323 53510 : switch (hdr->magic) {
324 8757 : case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
325 : case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
326 8757 : bp->b_ops = &xfs_dir3_block_buf_ops;
327 8757 : bp->b_ops->verify_read(bp);
328 8757 : return;
329 44753 : case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
330 : case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
331 44753 : bp->b_ops = &xfs_dir3_data_buf_ops;
332 44753 : bp->b_ops->verify_read(bp);
333 44753 : return;
334 : default:
335 0 : xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
336 0 : break;
337 : }
338 : }
339 :
340 : static void
341 133872 : xfs_dir3_data_read_verify(
342 : struct xfs_buf *bp)
343 : {
344 133872 : struct xfs_mount *mp = bp->b_mount;
345 133872 : xfs_failaddr_t fa;
346 :
347 267744 : if (xfs_has_crc(mp) &&
348 : !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
349 18 : xfs_verifier_error(bp, -EFSBADCRC, __this_address);
350 : else {
351 133854 : fa = xfs_dir3_data_verify(bp);
352 133854 : if (fa)
353 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
354 : }
355 133872 : }
356 :
357 : static void
358 830560 : xfs_dir3_data_write_verify(
359 : struct xfs_buf *bp)
360 : {
361 830560 : struct xfs_mount *mp = bp->b_mount;
362 830560 : struct xfs_buf_log_item *bip = bp->b_log_item;
363 830560 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
364 830560 : xfs_failaddr_t fa;
365 :
366 830560 : fa = xfs_dir3_data_verify(bp);
367 830560 : if (fa) {
368 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
369 0 : return;
370 : }
371 :
372 830560 : if (!xfs_has_crc(mp))
373 : return;
374 :
375 830560 : if (bip)
376 830560 : hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
377 :
378 830560 : 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 350173545 : xfs_dir3_data_header_check(
400 : struct xfs_buf *bp,
401 : xfs_ino_t owner)
402 : {
403 350173545 : struct xfs_mount *mp = bp->b_mount;
404 :
405 350173545 : if (xfs_has_crc(mp)) {
406 350173545 : struct xfs_dir3_data_hdr *hdr3 = bp->b_addr;
407 :
408 350173545 : ASSERT(hdr3->hdr.magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
409 :
410 350173545 : if (be64_to_cpu(hdr3->hdr.owner) != owner)
411 0 : return __this_address;
412 : }
413 :
414 : return NULL;
415 : }
416 :
417 : int
418 350048235 : 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 350048235 : xfs_failaddr_t fa;
427 350048235 : int err;
428 :
429 350048235 : err = xfs_da_read_buf(tp, dp, bno, flags, bpp, XFS_DATA_FORK,
430 : &xfs_dir3_data_buf_ops);
431 350204473 : if (err || !*bpp)
432 : return err;
433 :
434 : /* Check things that we can't do in the verifier. */
435 350199836 : fa = xfs_dir3_data_header_check(*bpp, owner);
436 350251870 : 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 350251870 : xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
445 350251870 : return err;
446 : }
447 :
448 : int
449 140205993 : xfs_dir3_data_readahead(
450 : struct xfs_inode *dp,
451 : xfs_dablk_t bno,
452 : unsigned int flags)
453 : {
454 140205993 : 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 4606781176 : 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 4606781176 : struct xfs_dir2_data_free *dfp;
470 4606781176 : xfs_dir2_data_aoff_t off;
471 4606781176 : bool matched = false;
472 4606781176 : bool seenzero = false;
473 :
474 4606781176 : *bf_ent = NULL;
475 4606781176 : 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 18434846477 : for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
483 13828065301 : if (!dfp->offset) {
484 172733919 : if (dfp->length)
485 0 : return __this_address;
486 172733919 : seenzero = true;
487 172733919 : continue;
488 : }
489 13655331382 : if (seenzero)
490 0 : return __this_address;
491 13655331382 : if (be16_to_cpu(dfp->offset) == off) {
492 768826802 : matched = true;
493 768826802 : if (dfp->length != dup->length)
494 0 : return __this_address;
495 12886504580 : } else if (be16_to_cpu(dfp->offset) > off) {
496 3497276050 : if (off + be16_to_cpu(dup->length) >
497 : be16_to_cpu(dfp->offset))
498 0 : return __this_address;
499 : } else {
500 18778457060 : if (be16_to_cpu(dfp->offset) +
501 9389228530 : be16_to_cpu(dfp->length) > off)
502 0 : return __this_address;
503 : }
504 25079999474 : if (!matched &&
505 12193494894 : be16_to_cpu(dfp->length) < be16_to_cpu(dup->length))
506 0 : return __this_address;
507 22698005400 : if (dfp > &bf[0] &&
508 9042674018 : 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 4606781176 : *bf_ent = xfs_dir2_data_freefind(hdr, bf, dup);
514 4606781176 : 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 4661111884 : 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 4661111884 : xfs_dir2_data_free_t *dfp; /* bestfree entry */
528 4661111884 : xfs_dir2_data_aoff_t off; /* offset value needed */
529 :
530 4661111884 : 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 9322223768 : if (be16_to_cpu(dup->length) <
537 4661111884 : 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 10743425468 : for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
543 8438190426 : if (!dfp->offset)
544 : return NULL;
545 8438190426 : if (be16_to_cpu(dfp->offset) == off)
546 816463789 : 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 632255504 : 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 632255504 : xfs_dir2_data_free_t new; /* new bestfree entry */
565 :
566 632255504 : 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 632255504 : new.length = dup->length;
572 632255504 : 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 632255504 : if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
578 64306136 : dfp[2] = dfp[1];
579 64306136 : dfp[1] = dfp[0];
580 64306136 : dfp[0] = new;
581 64306136 : *loghead = 1;
582 64306136 : return &dfp[0];
583 : }
584 567949368 : if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
585 40237843 : dfp[2] = dfp[1];
586 40237843 : dfp[1] = new;
587 40237843 : *loghead = 1;
588 40237843 : return &dfp[1];
589 : }
590 527711525 : if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
591 40769895 : dfp[2] = new;
592 40769895 : *loghead = 1;
593 40769895 : return &dfp[2];
594 : }
595 : return NULL;
596 : }
597 :
598 : /*
599 : * Remove a bestfree entry from the table.
600 : */
601 : STATIC void
602 36739652 : 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 36739652 : 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 36739652 : if (dfp == &bf[0]) {
618 35178796 : bf[0] = bf[1];
619 35178796 : bf[1] = bf[2];
620 : }
621 : /*
622 : * It's the second entry, slide the 3rd entry up.
623 : */
624 1560856 : else if (dfp == &bf[1])
625 1139267 : bf[1] = bf[2];
626 : /*
627 : * Must be the last entry.
628 : */
629 : else
630 421589 : ASSERT(dfp == &bf[2]);
631 : /*
632 : * Clear the 3rd entry, must be zero now.
633 : */
634 36739652 : bf[2].length = 0;
635 36739652 : bf[2].offset = 0;
636 36739652 : *loghead = 1;
637 36739652 : }
638 :
639 : /*
640 : * Given a data block, reconstruct its bestfree map.
641 : */
642 : void
643 22617873 : xfs_dir2_data_freescan(
644 : struct xfs_mount *mp,
645 : struct xfs_dir2_data_hdr *hdr,
646 : int *loghead)
647 : {
648 22617873 : struct xfs_da_geometry *geo = mp->m_dir_geo;
649 22617873 : struct xfs_dir2_data_free *bf = xfs_dir2_data_bestfree_p(mp, hdr);
650 22617873 : void *addr = hdr;
651 22617873 : unsigned int offset = geo->data_entry_offset;
652 22617873 : unsigned int end;
653 :
654 22617873 : 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 22617873 : memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
663 22617873 : *loghead = 1;
664 :
665 22617873 : end = xfs_dir3_data_end_offset(geo, addr);
666 3500179419 : while (offset < end) {
667 3477561105 : struct xfs_dir2_data_unused *dup = addr + offset;
668 3477561105 : struct xfs_dir2_data_entry *dep = addr + offset;
669 :
670 : /*
671 : * If it's a free entry, insert it.
672 : */
673 3477561105 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
674 581540784 : ASSERT(offset ==
675 : be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
676 581540784 : xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
677 581541225 : offset += be16_to_cpu(dup->length);
678 581541225 : continue;
679 : }
680 :
681 : /*
682 : * For active entries, check their tags and skip them.
683 : */
684 2896020321 : ASSERT(offset ==
685 : be16_to_cpu(*xfs_dir2_data_entry_tag_p(mp, dep)));
686 5792982574 : offset += xfs_dir2_data_entsize(mp, dep->namelen);
687 : }
688 22618314 : }
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 351269 : 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 351269 : struct xfs_trans *tp = args->trans;
701 351269 : struct xfs_inode *dp = args->dp;
702 351269 : struct xfs_mount *mp = dp->i_mount;
703 351269 : struct xfs_da_geometry *geo = args->geo;
704 351269 : struct xfs_buf *bp;
705 351269 : struct xfs_dir2_data_hdr *hdr;
706 351269 : struct xfs_dir2_data_unused *dup;
707 351269 : struct xfs_dir2_data_free *bf;
708 351269 : int error;
709 351269 : int i;
710 :
711 : /*
712 : * Get the buffer set up for the block.
713 : */
714 351269 : error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
715 : &bp, XFS_DATA_FORK);
716 351271 : if (error)
717 : return error;
718 351271 : bp->b_ops = &xfs_dir3_data_buf_ops;
719 351271 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
720 :
721 : /*
722 : * Initialize the header.
723 : */
724 351270 : hdr = bp->b_addr;
725 351270 : if (xfs_has_crc(mp)) {
726 351266 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
727 :
728 351266 : memset(hdr3, 0, sizeof(*hdr3));
729 351266 : hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
730 351266 : hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp));
731 351266 : hdr3->owner = cpu_to_be64(args->owner);
732 351266 : uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
733 :
734 : } else
735 4 : hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
736 :
737 351270 : bf = xfs_dir2_data_bestfree_p(mp, hdr);
738 351270 : bf[0].offset = cpu_to_be16(geo->data_entry_offset);
739 351270 : bf[0].length = cpu_to_be16(geo->blksize - geo->data_entry_offset);
740 1053810 : for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
741 702540 : bf[i].length = 0;
742 702540 : bf[i].offset = 0;
743 : }
744 :
745 : /*
746 : * Set up an unused entry for the block's body.
747 : */
748 351270 : dup = bp->b_addr + geo->data_entry_offset;
749 351270 : dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
750 351270 : dup->length = bf[0].length;
751 351270 : *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
752 :
753 : /*
754 : * Log it and return it.
755 : */
756 351270 : xfs_dir2_data_log_header(args, bp);
757 351272 : xfs_dir2_data_log_unused(args, bp, dup);
758 351271 : *bpp = bp;
759 351271 : return 0;
760 : }
761 :
762 : /*
763 : * Log an active data entry from the block.
764 : */
765 : void
766 46126472 : 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 46126472 : struct xfs_mount *mp = bp->b_mount;
772 46126472 : struct xfs_dir2_data_hdr *hdr = bp->b_addr;
773 :
774 46126472 : 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 46126472 : xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr),
780 46126472 : (uint)((char *)(xfs_dir2_data_entry_tag_p(mp, dep) + 1) -
781 46126472 : (char *)hdr - 1));
782 46126724 : }
783 :
784 : /*
785 : * Log a data block header.
786 : */
787 : void
788 62552787 : xfs_dir2_data_log_header(
789 : struct xfs_da_args *args,
790 : struct xfs_buf *bp)
791 : {
792 : #ifdef DEBUG
793 62552787 : struct xfs_dir2_data_hdr *hdr = bp->b_addr;
794 :
795 62552787 : 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 62552787 : xfs_trans_log_buf(args->trans, bp, 0, args->geo->data_entry_offset - 1);
802 62552905 : }
803 :
804 : /*
805 : * Log a data unused entry.
806 : */
807 : void
808 58719998 : 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 58719998 : xfs_dir2_data_hdr_t *hdr = bp->b_addr;
814 :
815 58719998 : 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 58719998 : xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
824 58719998 : (uint)((char *)&dup->length + sizeof(dup->length) -
825 58719998 : 1 - (char *)hdr));
826 : /*
827 : * Log the end (tag) of the unused entry.
828 : */
829 58718759 : xfs_trans_log_buf(args->trans, bp,
830 58718759 : (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
831 : (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
832 : sizeof(xfs_dir2_data_off_t) - 1));
833 58722531 : }
834 :
835 : /*
836 : * Make a byte range in the data block unused.
837 : * Its current contents are unimportant.
838 : */
839 : void
840 36601999 : 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 36601999 : xfs_dir2_data_hdr_t *hdr; /* data block pointer */
849 36601999 : xfs_dir2_data_free_t *dfp; /* bestfree pointer */
850 36601999 : int needscan; /* need to regen bestfree */
851 36601999 : xfs_dir2_data_unused_t *newdup; /* new unused entry */
852 36601999 : xfs_dir2_data_unused_t *postdup; /* unused entry after us */
853 36601999 : xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
854 36601999 : unsigned int end;
855 36601999 : struct xfs_dir2_data_free *bf;
856 :
857 36601999 : hdr = bp->b_addr;
858 :
859 : /*
860 : * Figure out where the end of the data area is.
861 : */
862 36601999 : end = xfs_dir3_data_end_offset(args->geo, hdr);
863 36601999 : 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 36601999 : if (offset > args->geo->data_entry_offset) {
870 36424678 : __be16 *tagp; /* tag just before us */
871 :
872 36424678 : tagp = (__be16 *)((char *)hdr + offset) - 1;
873 36424678 : prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
874 36424678 : if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
875 27827330 : 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 36601999 : if (offset + len < end) {
883 36416612 : postdup =
884 36416612 : (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
885 36416612 : if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
886 30006818 : postdup = NULL;
887 : } else
888 : postdup = NULL;
889 36601999 : ASSERT(*needscanp == 0);
890 36601999 : needscan = 0;
891 : /*
892 : * Previous and following entries are both free,
893 : * merge everything into a single free entry.
894 : */
895 36601999 : bf = xfs_dir2_data_bestfree_p(args->dp->i_mount, hdr);
896 36601999 : if (prevdup && postdup) {
897 2172876 : xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
898 :
899 : /*
900 : * See if prevdup and/or postdup are in bestfree table.
901 : */
902 2172876 : dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
903 2172876 : 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 2172876 : needscan = (bf[2].length != 0);
911 : /*
912 : * Fix up the new big freespace.
913 : */
914 4345752 : be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
915 4345752 : *xfs_dir2_data_unused_tag_p(prevdup) =
916 2172876 : cpu_to_be16((char *)prevdup - (char *)hdr);
917 2172876 : xfs_dir2_data_log_unused(args, bp, prevdup);
918 2172898 : 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 58627 : ASSERT(dfp && dfp2);
926 58627 : if (dfp == &bf[1]) {
927 47683 : dfp = &bf[0];
928 47683 : ASSERT(dfp2 == dfp);
929 : dfp2 = &bf[1];
930 : }
931 58627 : xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
932 58627 : xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
933 : /*
934 : * Now insert the new entry.
935 : */
936 58627 : dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
937 : needlogp);
938 58627 : ASSERT(dfp == &bf[0]);
939 58627 : ASSERT(dfp->length == prevdup->length);
940 58627 : ASSERT(!dfp[1].length);
941 58627 : ASSERT(!dfp[2].length);
942 : }
943 : }
944 : /*
945 : * The entry before us is free, merge with it.
946 : */
947 34429123 : else if (prevdup) {
948 6425588 : dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
949 6425588 : be16_add_cpu(&prevdup->length, len);
950 12851080 : *xfs_dir2_data_unused_tag_p(prevdup) =
951 6425540 : cpu_to_be16((char *)prevdup - (char *)hdr);
952 6425540 : 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 6425699 : if (dfp) {
959 4359540 : xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
960 4359564 : 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 4132318 : needscan = be16_to_cpu(prevdup->length) >
967 2066159 : be16_to_cpu(bf[2].length);
968 : }
969 : }
970 : /*
971 : * The following entry is free, merge with it.
972 : */
973 28003535 : else if (postdup) {
974 4237907 : dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
975 4237907 : newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
976 4237907 : newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
977 8475814 : newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
978 8475814 : *xfs_dir2_data_unused_tag_p(newdup) =
979 4237907 : cpu_to_be16((char *)newdup - (char *)hdr);
980 4237907 : 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 4237929 : if (dfp) {
987 769834 : xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
988 769832 : 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 6936190 : needscan = be16_to_cpu(newdup->length) >
995 3468095 : be16_to_cpu(bf[2].length);
996 : }
997 : }
998 : /*
999 : * Neither neighbor is free. Make a new entry.
1000 : */
1001 : else {
1002 23765628 : newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
1003 23765628 : newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1004 23765628 : newdup->length = cpu_to_be16(len);
1005 47531256 : *xfs_dir2_data_unused_tag_p(newdup) =
1006 23765628 : cpu_to_be16((char *)newdup - (char *)hdr);
1007 23765628 : xfs_dir2_data_log_unused(args, bp, newdup);
1008 23765686 : xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
1009 : }
1010 36602641 : *needscanp = needscan;
1011 36602641 : }
1012 :
1013 : /* Check our free data for obvious signs of corruption. */
1014 : static inline xfs_failaddr_t
1015 41772106 : 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 41772106 : if (hdr->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC) &&
1022 4754473 : hdr->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC) &&
1023 4754121 : 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 41772106 : if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG)
1027 0 : return __this_address;
1028 41772106 : if (offset < (char *)dup - (char *)hdr)
1029 0 : return __this_address;
1030 41772106 : if (offset + len > (char *)dup + be16_to_cpu(dup->length) - (char *)hdr)
1031 0 : return __this_address;
1032 41772106 : if ((char *)dup - (char *)hdr !=
1033 41772106 : 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 20187748 : 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 20187748 : if (dfp == NULL)
1046 0 : return __this_address;
1047 20187748 : if (dfp->length != newdup->length)
1048 0 : return __this_address;
1049 20187748 : if (be16_to_cpu(dfp->offset) != (char *)newdup - (char *)hdr)
1050 80 : 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 41771481 : 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 41771481 : xfs_dir2_data_hdr_t *hdr; /* data block header */
1068 41771481 : xfs_dir2_data_free_t *dfp; /* bestfree pointer */
1069 41771481 : xfs_dir2_data_unused_t *newdup; /* new unused entry */
1070 41771481 : xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
1071 41771481 : struct xfs_dir2_data_free *bf;
1072 41771481 : xfs_failaddr_t fa;
1073 41771481 : int matchback; /* matches end of freespace */
1074 41771481 : int matchfront; /* matches start of freespace */
1075 41771481 : int needscan; /* need to regen bestfree */
1076 41771481 : int oldlen; /* old unused entry's length */
1077 :
1078 41771481 : hdr = bp->b_addr;
1079 41771481 : fa = xfs_dir2_data_check_free(hdr, dup, offset, len);
1080 41771182 : if (fa)
1081 0 : goto corrupt;
1082 : /*
1083 : * Look up the entry in the bestfree table.
1084 : */
1085 41771182 : oldlen = be16_to_cpu(dup->length);
1086 41771182 : bf = xfs_dir2_data_bestfree_p(args->dp->i_mount, hdr);
1087 41771182 : dfp = xfs_dir2_data_freefind(hdr, bf, dup);
1088 41771182 : ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
1089 : /*
1090 : * Check for alignment with front and back of the entry.
1091 : */
1092 41771182 : matchfront = (char *)dup - (char *)hdr == offset;
1093 41771182 : matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
1094 41771182 : ASSERT(*needscanp == 0);
1095 41771182 : needscan = 0;
1096 : /*
1097 : * If we matched it exactly we just need to get rid of it from
1098 : * the bestfree table.
1099 : */
1100 41771182 : if (matchfront && matchback) {
1101 21564754 : if (dfp) {
1102 21563602 : needscan = (bf[2].offset != 0);
1103 21563602 : if (!needscan)
1104 11306929 : 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 20206428 : else if (matchfront) {
1113 18347158 : newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1114 18347158 : newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1115 18347158 : newdup->length = cpu_to_be16(oldlen - len);
1116 36694316 : *xfs_dir2_data_unused_tag_p(newdup) =
1117 18347158 : cpu_to_be16((char *)newdup - (char *)hdr);
1118 18347158 : 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 18346346 : if (dfp) {
1123 18346024 : xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1124 18346044 : dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1125 : needlogp);
1126 18346417 : fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1127 18346667 : 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 18346667 : 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 1859270 : else if (matchback) {
1142 1859270 : newdup = dup;
1143 1859270 : newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1144 3718540 : *xfs_dir2_data_unused_tag_p(newdup) =
1145 1859270 : cpu_to_be16((char *)newdup - (char *)hdr);
1146 1859270 : 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 1859431 : if (dfp) {
1151 1841114 : xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1152 1841009 : dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1153 : needlogp);
1154 1841148 : fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1155 1841090 : 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 1841090 : 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 41771775 : *needscanp = needscan;
1202 41771775 : 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 537469500 : xfs_dir3_data_end_offset(
1213 : struct xfs_da_geometry *geo,
1214 : struct xfs_dir2_data_hdr *hdr)
1215 : {
1216 537469500 : void *p;
1217 :
1218 537469500 : switch (hdr->magic) {
1219 : case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
1220 : case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
1221 83762667 : p = xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo, hdr));
1222 83762667 : return p - (void *)hdr;
1223 453706833 : case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
1224 : case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
1225 453706833 : return geo->blksize;
1226 : default:
1227 : return 0;
1228 : }
1229 : }
|