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