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