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