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