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