]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_dir2_data.c
Cosmetic configure tweaks to keep xfsprogs in sync with other packages.
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_data.c
CommitLineData
2bd0ea18 1/*
0d3e0b37 2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
5000d01d 3 *
2bd0ea18
NS
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
5000d01d 7 *
2bd0ea18
NS
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5000d01d 11 *
2bd0ea18
NS
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
5000d01d 14 * or the like. Any license provided herein, whether implied or
2bd0ea18
NS
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
5000d01d 18 *
2bd0ea18
NS
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
5000d01d 22 *
2bd0ea18
NS
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
5000d01d
SL
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
2bd0ea18
NS
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33/*
34 * xfs_dir2_data.c
35 * Core data block handling routines for XFS V2 directories.
36 * See xfs_dir2_data.h for data structures.
37 */
38#include <xfs.h>
39
40#ifdef DEBUG
41/*
42 * Check the consistency of the data block.
43 * The input can also be a block-format directory.
44 * Pop an assert if we find anything bad.
45 */
46void
47xfs_dir2_data_check(
48 xfs_inode_t *dp, /* incore inode pointer */
49 xfs_dabuf_t *bp) /* data block's buffer */
50{
51 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
52 xfs_dir2_data_free_t *bf; /* bestfree table */
0e266570 53 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
2bd0ea18
NS
54 int count; /* count of entries found */
55 xfs_dir2_data_t *d; /* data block pointer */
56 xfs_dir2_data_entry_t *dep; /* data entry */
57 xfs_dir2_data_free_t *dfp; /* bestfree entry */
58 xfs_dir2_data_unused_t *dup; /* unused entry */
59 char *endp; /* end of useful data */
60 int freeseen; /* mask of bestfrees seen */
61 xfs_dahash_t hash; /* hash of current name */
62 int i; /* leaf index */
63 int lastfree; /* last entry was unused */
0e266570 64 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
2bd0ea18
NS
65 xfs_mount_t *mp; /* filesystem mount point */
66 char *p; /* current data position */
67 int stale; /* count of stale leaves */
68
69 mp = dp->i_mount;
70 d = bp->data;
71 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
72 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
73 bf = d->hdr.bestfree;
74 p = (char *)d->u;
75 if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
76 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
77 lep = XFS_DIR2_BLOCK_LEAF_P_ARCH(btp, ARCH_CONVERT);
78 endp = (char *)lep;
79 } else
80 endp = (char *)d + mp->m_dirblksize;
81 count = lastfree = freeseen = 0;
82 /*
83 * Account for zero bestfree entries.
84 */
5ce1d1f7
NS
85 if (INT_ISZERO(bf[0].length, ARCH_CONVERT)) {
86 ASSERT(INT_ISZERO(bf[0].offset, ARCH_CONVERT));
2bd0ea18
NS
87 freeseen |= 1 << 0;
88 }
5ce1d1f7
NS
89 if (INT_ISZERO(bf[1].length, ARCH_CONVERT)) {
90 ASSERT(INT_ISZERO(bf[1].offset, ARCH_CONVERT));
2bd0ea18
NS
91 freeseen |= 1 << 1;
92 }
5ce1d1f7
NS
93 if (INT_ISZERO(bf[2].length, ARCH_CONVERT)) {
94 ASSERT(INT_ISZERO(bf[2].offset, ARCH_CONVERT));
2bd0ea18
NS
95 freeseen |= 1 << 2;
96 }
97 ASSERT(INT_GET(bf[0].length, ARCH_CONVERT) >= INT_GET(bf[1].length, ARCH_CONVERT));
98 ASSERT(INT_GET(bf[1].length, ARCH_CONVERT) >= INT_GET(bf[2].length, ARCH_CONVERT));
99 /*
100 * Loop over the data/unused entries.
101 */
102 while (p < endp) {
103 dup = (xfs_dir2_data_unused_t *)p;
104 /*
105 * If it's unused, look for the space in the bestfree table.
5000d01d 106 * If we find it, account for that, else make sure it
2bd0ea18
NS
107 * doesn't need to be there.
108 */
109 if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
110 ASSERT(lastfree == 0);
111 ASSERT(INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(dup, ARCH_CONVERT), ARCH_CONVERT) ==
112 (char *)dup - (char *)d);
113 dfp = xfs_dir2_data_freefind(d, dup);
114 if (dfp) {
115 i = (int)(dfp - bf);
116 ASSERT((freeseen & (1 << i)) == 0);
117 freeseen |= 1 << i;
118 } else
119 ASSERT(INT_GET(dup->length, ARCH_CONVERT) <= INT_GET(bf[2].length, ARCH_CONVERT));
120 p += INT_GET(dup->length, ARCH_CONVERT);
121 lastfree = 1;
122 continue;
123 }
124 /*
125 * It's a real entry. Validate the fields.
5000d01d 126 * If this is a block directory then make sure it's
2bd0ea18
NS
127 * in the leaf section of the block.
128 * The linear search is crude but this is DEBUG code.
129 */
130 dep = (xfs_dir2_data_entry_t *)p;
131 ASSERT(dep->namelen != 0);
132 ASSERT(xfs_dir_ino_validate(mp, INT_GET(dep->inumber, ARCH_CONVERT)) == 0);
133 ASSERT(INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep), ARCH_CONVERT) ==
134 (char *)dep - (char *)d);
135 count++;
136 lastfree = 0;
137 if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
138 addr = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
139 (xfs_dir2_data_aoff_t)
140 ((char *)dep - (char *)d));
141 hash = xfs_da_hashname((char *)dep->name, dep->namelen);
142 for (i = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
143 if (INT_GET(lep[i].address, ARCH_CONVERT) == addr &&
144 INT_GET(lep[i].hashval, ARCH_CONVERT) == hash)
145 break;
146 }
147 ASSERT(i < INT_GET(btp->count, ARCH_CONVERT));
148 }
149 p += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
150 }
151 /*
152 * Need to have seen all the entries and all the bestfree slots.
153 */
154 ASSERT(freeseen == 7);
155 if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
156 for (i = stale = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
157 if (INT_GET(lep[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
158 stale++;
159 if (i > 0)
160 ASSERT(INT_GET(lep[i].hashval, ARCH_CONVERT) >= INT_GET(lep[i - 1].hashval, ARCH_CONVERT));
161 }
162 ASSERT(count == INT_GET(btp->count, ARCH_CONVERT) - INT_GET(btp->stale, ARCH_CONVERT));
163 ASSERT(stale == INT_GET(btp->stale, ARCH_CONVERT));
164 }
165}
166#endif
167
168/*
169 * Given a data block and an unused entry from that block,
170 * return the bestfree entry if any that corresponds to it.
171 */
172xfs_dir2_data_free_t *
173xfs_dir2_data_freefind(
174 xfs_dir2_data_t *d, /* data block */
175 xfs_dir2_data_unused_t *dup) /* data unused entry */
176{
177 xfs_dir2_data_free_t *dfp; /* bestfree entry */
178 xfs_dir2_data_aoff_t off; /* offset value needed */
179#if defined(DEBUG) && defined(__KERNEL__)
180 int matched; /* matched the value */
181 int seenzero; /* saw a 0 bestfree entry */
182#endif
183
184 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)d);
185#if defined(DEBUG) && defined(__KERNEL__)
186 /*
187 * Validate some consistency in the bestfree table.
188 * Check order, non-overlapping entries, and if we find the
189 * one we're looking for it has to be exact.
190 */
191 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
192 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
193 for (dfp = &d->hdr.bestfree[0], seenzero = matched = 0;
194 dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
195 dfp++) {
5ce1d1f7
NS
196 if (INT_ISZERO(dfp->offset, ARCH_CONVERT)) {
197 ASSERT(INT_ISZERO(dfp->length, ARCH_CONVERT));
2bd0ea18
NS
198 seenzero = 1;
199 continue;
200 }
201 ASSERT(seenzero == 0);
202 if (INT_GET(dfp->offset, ARCH_CONVERT) == off) {
203 matched = 1;
204 ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(dup->length, ARCH_CONVERT));
205 } else if (off < INT_GET(dfp->offset, ARCH_CONVERT))
206 ASSERT(off + INT_GET(dup->length, ARCH_CONVERT) <= INT_GET(dfp->offset, ARCH_CONVERT));
207 else
208 ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) + INT_GET(dfp->length, ARCH_CONVERT) <= off);
209 ASSERT(matched || INT_GET(dfp->length, ARCH_CONVERT) >= INT_GET(dup->length, ARCH_CONVERT));
210 if (dfp > &d->hdr.bestfree[0])
211 ASSERT(INT_GET(dfp[-1].length, ARCH_CONVERT) >= INT_GET(dfp[0].length, ARCH_CONVERT));
212 }
213#endif
214 /*
215 * If this is smaller than the smallest bestfree entry,
216 * it can't be there since they're sorted.
217 */
218 if (INT_GET(dup->length, ARCH_CONVERT) < INT_GET(d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length, ARCH_CONVERT))
219 return NULL;
220 /*
221 * Look at the three bestfree entries for our guy.
222 */
223 for (dfp = &d->hdr.bestfree[0];
224 dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
225 dfp++) {
5ce1d1f7 226 if (INT_ISZERO(dfp->offset, ARCH_CONVERT))
2bd0ea18
NS
227 return NULL;
228 if (INT_GET(dfp->offset, ARCH_CONVERT) == off)
229 return dfp;
230 }
231 /*
232 * Didn't find it. This only happens if there are duplicate lengths.
233 */
234 return NULL;
235}
236
237/*
238 * Insert an unused-space entry into the bestfree table.
239 */
240xfs_dir2_data_free_t * /* entry inserted */
241xfs_dir2_data_freeinsert(
242 xfs_dir2_data_t *d, /* data block pointer */
243 xfs_dir2_data_unused_t *dup, /* unused space */
244 int *loghead) /* log the data header (out) */
245{
246 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
247 xfs_dir2_data_free_t new; /* new bestfree entry */
248
249#ifdef __KERNEL__
250 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
251 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
252#endif
253 dfp = d->hdr.bestfree;
5000d01d 254 INT_COPY(new.length, dup->length, ARCH_CONVERT);
2bd0ea18
NS
255 INT_SET(new.offset, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dup - (char *)d));
256 /*
257 * Insert at position 0, 1, or 2; or not at all.
258 */
259 if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[0].length, ARCH_CONVERT)) {
260 dfp[2] = dfp[1];
261 dfp[1] = dfp[0];
262 dfp[0] = new;
263 *loghead = 1;
264 return &dfp[0];
265 }
266 if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[1].length, ARCH_CONVERT)) {
267 dfp[2] = dfp[1];
268 dfp[1] = new;
269 *loghead = 1;
270 return &dfp[1];
271 }
272 if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[2].length, ARCH_CONVERT)) {
273 dfp[2] = new;
274 *loghead = 1;
275 return &dfp[2];
276 }
277 return NULL;
278}
279
280/*
281 * Remove a bestfree entry from the table.
282 */
283void
284xfs_dir2_data_freeremove(
285 xfs_dir2_data_t *d, /* data block pointer */
286 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
287 int *loghead) /* out: log data header */
288{
289#ifdef __KERNEL__
290 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
291 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
292#endif
293 /*
294 * It's the first entry, slide the next 2 up.
295 */
296 if (dfp == &d->hdr.bestfree[0]) {
297 d->hdr.bestfree[0] = d->hdr.bestfree[1];
298 d->hdr.bestfree[1] = d->hdr.bestfree[2];
299 }
300 /*
301 * It's the second entry, slide the 3rd entry up.
302 */
303 else if (dfp == &d->hdr.bestfree[1])
304 d->hdr.bestfree[1] = d->hdr.bestfree[2];
305 /*
306 * Must be the last entry.
307 */
308 else
309 ASSERT(dfp == &d->hdr.bestfree[2]);
310 /*
311 * Clear the 3rd entry, must be zero now.
312 */
5000d01d 313 INT_ZERO(d->hdr.bestfree[2].length, ARCH_CONVERT);
2bd0ea18
NS
314 INT_ZERO(d->hdr.bestfree[2].offset, ARCH_CONVERT);
315 *loghead = 1;
316}
317
318/*
319 * Given a data block, reconstruct its bestfree map.
320 */
321void
322xfs_dir2_data_freescan(
323 xfs_mount_t *mp, /* filesystem mount point */
324 xfs_dir2_data_t *d, /* data block pointer */
325 int *loghead, /* out: log data header */
326 char *aendp) /* in: caller's endp */
327{
328 xfs_dir2_block_tail_t *btp; /* block tail */
329 xfs_dir2_data_entry_t *dep; /* active data entry */
330 xfs_dir2_data_unused_t *dup; /* unused data entry */
331 char *endp; /* end of block's data */
332 char *p; /* current entry pointer */
333
334#ifdef __KERNEL__
335 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
336 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
337#endif
338 /*
339 * Start by clearing the table.
340 */
32181a02 341 memset(d->hdr.bestfree, 0, sizeof(d->hdr.bestfree));
2bd0ea18
NS
342 *loghead = 1;
343 /*
344 * Set up pointers.
345 */
346 p = (char *)d->u;
347 if (aendp)
348 endp = aendp;
349 else if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
350 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
351 endp = (char *)XFS_DIR2_BLOCK_LEAF_P_ARCH(btp, ARCH_CONVERT);
352 } else
353 endp = (char *)d + mp->m_dirblksize;
354 /*
355 * Loop over the block's entries.
356 */
357 while (p < endp) {
358 dup = (xfs_dir2_data_unused_t *)p;
359 /*
360 * If it's a free entry, insert it.
361 */
362 if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
363 ASSERT((char *)dup - (char *)d ==
364 INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(dup, ARCH_CONVERT), ARCH_CONVERT));
365 xfs_dir2_data_freeinsert(d, dup, loghead);
366 p += INT_GET(dup->length, ARCH_CONVERT);
367 }
368 /*
369 * For active entries, check their tags and skip them.
370 */
371 else {
372 dep = (xfs_dir2_data_entry_t *)p;
373 ASSERT((char *)dep - (char *)d ==
374 INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep), ARCH_CONVERT));
375 p += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
376 }
377 }
378}
379
380/*
381 * Initialize a data block at the given block number in the directory.
382 * Give back the buffer for the created block.
383 */
384int /* error */
385xfs_dir2_data_init(
386 xfs_da_args_t *args, /* directory operation args */
387 xfs_dir2_db_t blkno, /* logical dir block number */
388 xfs_dabuf_t **bpp) /* output block buffer */
389{
390 xfs_dabuf_t *bp; /* block buffer */
391 xfs_dir2_data_t *d; /* pointer to block */
392 xfs_inode_t *dp; /* incore directory inode */
393 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
394 int error; /* error return value */
395 int i; /* bestfree index */
396 xfs_mount_t *mp; /* filesystem mount point */
397 xfs_trans_t *tp; /* transaction pointer */
5000d01d 398 int t; /* temp */
2bd0ea18
NS
399
400 dp = args->dp;
401 mp = dp->i_mount;
402 tp = args->trans;
403 /*
404 * Get the buffer set up for the block.
405 */
406 error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, blkno), -1, &bp,
407 XFS_DATA_FORK);
408 if (error) {
2bd0ea18
NS
409 return error;
410 }
411 ASSERT(bp != NULL);
412 /*
413 * Initialize the header.
414 */
415 d = bp->data;
416 INT_SET(d->hdr.magic, ARCH_CONVERT, XFS_DIR2_DATA_MAGIC);
417 INT_SET(d->hdr.bestfree[0].offset, ARCH_CONVERT, (xfs_dir2_data_off_t)sizeof(d->hdr));
418 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
5000d01d 419 INT_ZERO(d->hdr.bestfree[i].length, ARCH_CONVERT);
2bd0ea18 420 INT_ZERO(d->hdr.bestfree[i].offset, ARCH_CONVERT);
5000d01d 421 }
2bd0ea18
NS
422 /*
423 * Set up an unused entry for the block's body.
424 */
425 dup = &d->u[0].unused;
426 INT_SET(dup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
5000d01d
SL
427
428 t=mp->m_dirblksize - (uint)sizeof(d->hdr);
429 INT_SET(d->hdr.bestfree[0].length, ARCH_CONVERT, t);
2bd0ea18
NS
430 INT_SET(dup->length, ARCH_CONVERT, t);
431 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(dup, ARCH_CONVERT), ARCH_CONVERT,
432 (xfs_dir2_data_off_t)((char *)dup - (char *)d));
433 /*
434 * Log it and return it.
435 */
436 xfs_dir2_data_log_header(tp, bp);
437 xfs_dir2_data_log_unused(tp, bp, dup);
438 *bpp = bp;
439 return 0;
440}
441
442/*
443 * Log an active data entry from the block.
444 */
445void
446xfs_dir2_data_log_entry(
447 xfs_trans_t *tp, /* transaction pointer */
448 xfs_dabuf_t *bp, /* block buffer */
449 xfs_dir2_data_entry_t *dep) /* data entry pointer */
450{
451 xfs_dir2_data_t *d; /* data block pointer */
452
453 d = bp->data;
454 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
455 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
456 xfs_da_log_buf(tp, bp, (uint)((char *)dep - (char *)d),
457 (uint)((char *)(XFS_DIR2_DATA_ENTRY_TAG_P(dep) + 1) -
458 (char *)d - 1));
459}
460
461/*
462 * Log a data block header.
463 */
464void
465xfs_dir2_data_log_header(
466 xfs_trans_t *tp, /* transaction pointer */
467 xfs_dabuf_t *bp) /* block buffer */
468{
469 xfs_dir2_data_t *d; /* data block pointer */
470
471 d = bp->data;
472 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
473 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
5000d01d 474 xfs_da_log_buf(tp, bp, (uint)((char *)&d->hdr - (char *)d),
2bd0ea18
NS
475 (uint)(sizeof(d->hdr) - 1));
476}
477
478/*
479 * Log a data unused entry.
480 */
481void
482xfs_dir2_data_log_unused(
483 xfs_trans_t *tp, /* transaction pointer */
484 xfs_dabuf_t *bp, /* block buffer */
485 xfs_dir2_data_unused_t *dup) /* data unused pointer */
486{
487 xfs_dir2_data_t *d; /* data block pointer */
488
489 d = bp->data;
490 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
491 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
492 /*
493 * Log the first part of the unused entry.
494 */
495 xfs_da_log_buf(tp, bp, (uint)((char *)dup - (char *)d),
496 (uint)((char *)&dup->length + sizeof(dup->length) -
497 1 - (char *)d));
498 /*
499 * Log the end (tag) of the unused entry.
500 */
501 xfs_da_log_buf(tp, bp,
502 (uint)((char *)XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(dup, ARCH_CONVERT) - (char *)d),
503 (uint)((char *)XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(dup, ARCH_CONVERT) - (char *)d +
504 sizeof(xfs_dir2_data_off_t) - 1));
505}
506
507/*
508 * Make a byte range in the data block unused.
509 * Its current contents are unimportant.
510 */
511void
512xfs_dir2_data_make_free(
513 xfs_trans_t *tp, /* transaction pointer */
514 xfs_dabuf_t *bp, /* block buffer */
515 xfs_dir2_data_aoff_t offset, /* starting byte offset */
516 xfs_dir2_data_aoff_t len, /* length in bytes */
517 int *needlogp, /* out: log header */
518 int *needscanp) /* out: regen bestfree */
519{
520 xfs_dir2_data_t *d; /* data block pointer */
521 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
522 char *endptr; /* end of data area */
523 xfs_mount_t *mp; /* filesystem mount point */
524 int needscan; /* need to regen bestfree */
525 xfs_dir2_data_unused_t *newdup; /* new unused entry */
526 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
527 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
528
529 mp = tp->t_mountp;
530 d = bp->data;
531 /*
532 * Figure out where the end of the data area is.
533 */
534 if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC)
535 endptr = (char *)d + mp->m_dirblksize;
536 else {
537 xfs_dir2_block_tail_t *btp; /* block tail */
538
539 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
540 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
541 endptr = (char *)XFS_DIR2_BLOCK_LEAF_P_ARCH(btp, ARCH_CONVERT);
542 }
543 /*
5000d01d 544 * If this isn't the start of the block, then back up to
2bd0ea18
NS
545 * the previous entry and see if it's free.
546 */
547 if (offset > sizeof(d->hdr)) {
548 xfs_dir2_data_off_t *tagp; /* tag just before us */
549
550 tagp = (xfs_dir2_data_off_t *)((char *)d + offset) - 1;
551 prevdup = (xfs_dir2_data_unused_t *)((char *)d + INT_GET(*tagp, ARCH_CONVERT));
552 if (INT_GET(prevdup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG)
553 prevdup = NULL;
554 } else
555 prevdup = NULL;
556 /*
557 * If this isn't the end of the block, see if the entry after
558 * us is free.
559 */
560 if ((char *)d + offset + len < endptr) {
561 postdup =
562 (xfs_dir2_data_unused_t *)((char *)d + offset + len);
563 if (INT_GET(postdup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG)
564 postdup = NULL;
565 } else
566 postdup = NULL;
567 ASSERT(*needscanp == 0);
568 needscan = 0;
569 /*
5000d01d 570 * Previous and following entries are both free,
2bd0ea18
NS
571 * merge everything into a single free entry.
572 */
573 if (prevdup && postdup) {
574 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
575
576 /*
577 * See if prevdup and/or postdup are in bestfree table.
578 */
579 dfp = xfs_dir2_data_freefind(d, prevdup);
580 dfp2 = xfs_dir2_data_freefind(d, postdup);
581 /*
582 * We need a rescan unless there are exactly 2 free entries
583 * namely our two. Then we know what's happening, otherwise
584 * since the third bestfree is there, there might be more
585 * entries.
586 */
5ce1d1f7 587 needscan = !INT_ISZERO(d->hdr.bestfree[2].length, ARCH_CONVERT);
2bd0ea18
NS
588 /*
589 * Fix up the new big freespace.
590 */
591 INT_MOD(prevdup->length, ARCH_CONVERT, len + INT_GET(postdup->length, ARCH_CONVERT));
592 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(prevdup, ARCH_CONVERT), ARCH_CONVERT,
593 (xfs_dir2_data_off_t)((char *)prevdup - (char *)d));
594 xfs_dir2_data_log_unused(tp, bp, prevdup);
595 if (!needscan) {
596 /*
5000d01d 597 * Has to be the case that entries 0 and 1 are
2bd0ea18
NS
598 * dfp and dfp2 (don't know which is which), and
599 * entry 2 is empty.
600 * Remove entry 1 first then entry 0.
601 */
602 ASSERT(dfp && dfp2);
603 if (dfp == &d->hdr.bestfree[1]) {
604 dfp = &d->hdr.bestfree[0];
605 ASSERT(dfp2 == dfp);
606 dfp2 = &d->hdr.bestfree[1];
607 }
608 xfs_dir2_data_freeremove(d, dfp2, needlogp);
609 xfs_dir2_data_freeremove(d, dfp, needlogp);
610 /*
611 * Now insert the new entry.
612 */
613 dfp = xfs_dir2_data_freeinsert(d, prevdup, needlogp);
614 ASSERT(dfp == &d->hdr.bestfree[0]);
615 ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(prevdup->length, ARCH_CONVERT));
5ce1d1f7
NS
616 ASSERT(INT_ISZERO(dfp[1].length, ARCH_CONVERT));
617 ASSERT(INT_ISZERO(dfp[2].length, ARCH_CONVERT));
2bd0ea18
NS
618 }
619 }
620 /*
621 * The entry before us is free, merge with it.
622 */
623 else if (prevdup) {
624 dfp = xfs_dir2_data_freefind(d, prevdup);
625 INT_MOD(prevdup->length, ARCH_CONVERT, len);
626 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(prevdup, ARCH_CONVERT), ARCH_CONVERT,
627 (xfs_dir2_data_off_t)((char *)prevdup - (char *)d));
628 xfs_dir2_data_log_unused(tp, bp, prevdup);
629 /*
630 * If the previous entry was in the table, the new entry
631 * is longer, so it will be in the table too. Remove
632 * the old one and add the new one.
633 */
634 if (dfp) {
635 xfs_dir2_data_freeremove(d, dfp, needlogp);
636 (void)xfs_dir2_data_freeinsert(d, prevdup, needlogp);
637 }
638 /*
639 * Otherwise we need a scan if the new entry is big enough.
640 */
641 else
642 needscan = INT_GET(prevdup->length, ARCH_CONVERT) > INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT);
643 }
644 /*
645 * The following entry is free, merge with it.
646 */
647 else if (postdup) {
648 dfp = xfs_dir2_data_freefind(d, postdup);
649 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
650 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
651 INT_SET(newdup->length, ARCH_CONVERT, len + INT_GET(postdup->length, ARCH_CONVERT));
652 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(newdup, ARCH_CONVERT), ARCH_CONVERT,
653 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
654 xfs_dir2_data_log_unused(tp, bp, newdup);
655 /*
656 * If the following entry was in the table, the new entry
657 * is longer, so it will be in the table too. Remove
658 * the old one and add the new one.
659 */
660 if (dfp) {
661 xfs_dir2_data_freeremove(d, dfp, needlogp);
662 (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
663 }
664 /*
665 * Otherwise we need a scan if the new entry is big enough.
666 */
667 else
668 needscan = INT_GET(newdup->length, ARCH_CONVERT) > INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT);
669 }
670 /*
671 * Neither neighbor is free. Make a new entry.
672 */
673 else {
674 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
675 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
676 INT_SET(newdup->length, ARCH_CONVERT, len);
677 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(newdup, ARCH_CONVERT), ARCH_CONVERT,
678 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
679 xfs_dir2_data_log_unused(tp, bp, newdup);
680 (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
681 }
682 *needscanp = needscan;
683}
684
685/*
686 * Take a byte range out of an existing unused space and make it un-free.
687 */
688void
689xfs_dir2_data_use_free(
690 xfs_trans_t *tp, /* transaction pointer */
691 xfs_dabuf_t *bp, /* data block buffer */
692 xfs_dir2_data_unused_t *dup, /* unused entry */
693 xfs_dir2_data_aoff_t offset, /* starting offset to use */
694 xfs_dir2_data_aoff_t len, /* length to use */
695 int *needlogp, /* out: need to log header */
696 int *needscanp) /* out: need regen bestfree */
697{
698 xfs_dir2_data_t *d; /* data block */
699 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
700 int matchback; /* matches end of freespace */
701 int matchfront; /* matches start of freespace */
702 int needscan; /* need to regen bestfree */
703 xfs_dir2_data_unused_t *newdup; /* new unused entry */
704 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
705 int oldlen; /* old unused entry's length */
706
707 d = bp->data;
708 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
709 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
710 ASSERT(INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG);
711 ASSERT(offset >= (char *)dup - (char *)d);
712 ASSERT(offset + len <= (char *)dup + INT_GET(dup->length, ARCH_CONVERT) - (char *)d);
713 ASSERT((char *)dup - (char *)d == INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(dup, ARCH_CONVERT), ARCH_CONVERT));
714 /*
715 * Look up the entry in the bestfree table.
716 */
717 dfp = xfs_dir2_data_freefind(d, dup);
718 oldlen = INT_GET(dup->length, ARCH_CONVERT);
719 ASSERT(dfp || oldlen <= INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT));
720 /*
721 * Check for alignment with front and back of the entry.
722 */
723 matchfront = (char *)dup - (char *)d == offset;
724 matchback = (char *)dup + oldlen - (char *)d == offset + len;
725 ASSERT(*needscanp == 0);
726 needscan = 0;
727 /*
728 * If we matched it exactly we just need to get rid of it from
729 * the bestfree table.
730 */
731 if (matchfront && matchback) {
732 if (dfp) {
5ce1d1f7 733 needscan = !INT_ISZERO(d->hdr.bestfree[2].offset, ARCH_CONVERT);
2bd0ea18
NS
734 if (!needscan)
735 xfs_dir2_data_freeremove(d, dfp, needlogp);
736 }
737 }
738 /*
739 * We match the first part of the entry.
740 * Make a new entry with the remaining freespace.
741 */
742 else if (matchfront) {
743 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
744 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
745 INT_SET(newdup->length, ARCH_CONVERT, oldlen - len);
746 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(newdup, ARCH_CONVERT), ARCH_CONVERT,
747 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
748 xfs_dir2_data_log_unused(tp, bp, newdup);
749 /*
750 * If it was in the table, remove it and add the new one.
751 */
752 if (dfp) {
753 xfs_dir2_data_freeremove(d, dfp, needlogp);
754 dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
755 ASSERT(dfp != NULL);
756 ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(newdup->length, ARCH_CONVERT));
757 ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) == (char *)newdup - (char *)d);
758 /*
759 * If we got inserted at the last slot,
760 * that means we don't know if there was a better
761 * choice for the last slot, or not. Rescan.
762 */
763 needscan = dfp == &d->hdr.bestfree[2];
764 }
765 }
766 /*
767 * We match the last part of the entry.
768 * Trim the allocated space off the tail of the entry.
769 */
770 else if (matchback) {
771 newdup = dup;
772 INT_SET(newdup->length, ARCH_CONVERT, (xfs_dir2_data_off_t)
773 (((char *)d + offset) - (char *)newdup));
774 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(newdup, ARCH_CONVERT), ARCH_CONVERT,
775 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
776 xfs_dir2_data_log_unused(tp, bp, newdup);
777 /*
778 * If it was in the table, remove it and add the new one.
779 */
780 if (dfp) {
781 xfs_dir2_data_freeremove(d, dfp, needlogp);
782 dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
783 ASSERT(dfp != NULL);
784 ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(newdup->length, ARCH_CONVERT));
785 ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) == (char *)newdup - (char *)d);
786 /*
787 * If we got inserted at the last slot,
788 * that means we don't know if there was a better
789 * choice for the last slot, or not. Rescan.
790 */
791 needscan = dfp == &d->hdr.bestfree[2];
792 }
793 }
794 /*
795 * Poking out the middle of an entry.
796 * Make two new entries.
797 */
798 else {
799 newdup = dup;
800 INT_SET(newdup->length, ARCH_CONVERT, (xfs_dir2_data_off_t)
801 (((char *)d + offset) - (char *)newdup));
802 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(newdup, ARCH_CONVERT), ARCH_CONVERT,
803 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
804 xfs_dir2_data_log_unused(tp, bp, newdup);
805 newdup2 = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
806 INT_SET(newdup2->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
807 INT_SET(newdup2->length, ARCH_CONVERT, oldlen - len - INT_GET(newdup->length, ARCH_CONVERT));
808 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(newdup2, ARCH_CONVERT), ARCH_CONVERT,
809 (xfs_dir2_data_off_t)((char *)newdup2 - (char *)d));
810 xfs_dir2_data_log_unused(tp, bp, newdup2);
811 /*
812 * If the old entry was in the table, we need to scan
813 * if the 3rd entry was valid, since these entries
814 * are smaller than the old one.
815 * If we don't need to scan that means there were 1 or 2
816 * entries in the table, and removing the old and adding
817 * the 2 new will work.
818 */
819 if (dfp) {
5ce1d1f7 820 needscan = !INT_ISZERO(d->hdr.bestfree[2].length, ARCH_CONVERT);
2bd0ea18
NS
821 if (!needscan) {
822 xfs_dir2_data_freeremove(d, dfp, needlogp);
823 (void)xfs_dir2_data_freeinsert(d, newdup,
824 needlogp);
825 (void)xfs_dir2_data_freeinsert(d, newdup2,
826 needlogp);
827 }
828 }
829 }
830 *needscanp = needscan;
831}