blob: fd6b7c03018a68595672af069f626057b77129e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_dir.h"
26#include "xfs_dir2.h"
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dir_sf.h"
32#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dinode.h"
35#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dir_leaf.h"
37#include "xfs_dir2_data.h"
38#include "xfs_dir2_leaf.h"
39#include "xfs_dir2_block.h"
40#include "xfs_error.h"
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 */
48void
49xfs_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;
Nathan Scott70e73f52006-03-17 17:26:52 +110073 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
74 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 bf = d->hdr.bestfree;
76 p = (char *)d->u;
Nathan Scott70e73f52006-03-17 17:26:52 +110077 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
79 lep = XFS_DIR2_BLOCK_LEAF_P(btp);
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 (!bf[0].length) {
88 ASSERT(!bf[0].offset);
89 freeseen |= 1 << 0;
90 }
91 if (!bf[1].length) {
92 ASSERT(!bf[1].offset);
93 freeseen |= 1 << 1;
94 }
95 if (!bf[2].length) {
96 ASSERT(!bf[2].offset);
97 freeseen |= 1 << 2;
98 }
Nathan Scott70e73f52006-03-17 17:26:52 +110099 ASSERT(be16_to_cpu(bf[0].length) >= be16_to_cpu(bf[1].length));
100 ASSERT(be16_to_cpu(bf[1].length) >= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /*
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(dup), 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;
Nathan Scott70e73f52006-03-17 17:26:52 +1100120 } else {
121 ASSERT(INT_GET(dup->length, ARCH_CONVERT) <=
122 be16_to_cpu(bf[2].length));
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 p += INT_GET(dup->length, ARCH_CONVERT);
125 lastfree = 1;
126 continue;
127 }
128 /*
129 * It's a real entry. Validate the fields.
130 * If this is a block directory then make sure it's
131 * in the leaf section of the block.
132 * The linear search is crude but this is DEBUG code.
133 */
134 dep = (xfs_dir2_data_entry_t *)p;
135 ASSERT(dep->namelen != 0);
136 ASSERT(xfs_dir_ino_validate(mp, INT_GET(dep->inumber, ARCH_CONVERT)) == 0);
137 ASSERT(INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep), ARCH_CONVERT) ==
138 (char *)dep - (char *)d);
139 count++;
140 lastfree = 0;
Nathan Scott70e73f52006-03-17 17:26:52 +1100141 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 addr = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
143 (xfs_dir2_data_aoff_t)
144 ((char *)dep - (char *)d));
145 hash = xfs_da_hashname((char *)dep->name, dep->namelen);
146 for (i = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
147 if (INT_GET(lep[i].address, ARCH_CONVERT) == addr &&
148 INT_GET(lep[i].hashval, ARCH_CONVERT) == hash)
149 break;
150 }
151 ASSERT(i < INT_GET(btp->count, ARCH_CONVERT));
152 }
153 p += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
154 }
155 /*
156 * Need to have seen all the entries and all the bestfree slots.
157 */
158 ASSERT(freeseen == 7);
Nathan Scott70e73f52006-03-17 17:26:52 +1100159 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 for (i = stale = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
161 if (INT_GET(lep[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
162 stale++;
163 if (i > 0)
164 ASSERT(INT_GET(lep[i].hashval, ARCH_CONVERT) >= INT_GET(lep[i - 1].hashval, ARCH_CONVERT));
165 }
166 ASSERT(count == INT_GET(btp->count, ARCH_CONVERT) - INT_GET(btp->stale, ARCH_CONVERT));
167 ASSERT(stale == INT_GET(btp->stale, ARCH_CONVERT));
168 }
169}
170#endif
171
172/*
173 * Given a data block and an unused entry from that block,
174 * return the bestfree entry if any that corresponds to it.
175 */
176xfs_dir2_data_free_t *
177xfs_dir2_data_freefind(
178 xfs_dir2_data_t *d, /* data block */
179 xfs_dir2_data_unused_t *dup) /* data unused entry */
180{
181 xfs_dir2_data_free_t *dfp; /* bestfree entry */
182 xfs_dir2_data_aoff_t off; /* offset value needed */
183#if defined(DEBUG) && defined(__KERNEL__)
184 int matched; /* matched the value */
185 int seenzero; /* saw a 0 bestfree entry */
186#endif
187
188 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)d);
189#if defined(DEBUG) && defined(__KERNEL__)
190 /*
191 * Validate some consistency in the bestfree table.
192 * Check order, non-overlapping entries, and if we find the
193 * one we're looking for it has to be exact.
194 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100195 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
196 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 for (dfp = &d->hdr.bestfree[0], seenzero = matched = 0;
198 dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
199 dfp++) {
200 if (!dfp->offset) {
201 ASSERT(!dfp->length);
202 seenzero = 1;
203 continue;
204 }
205 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100206 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 matched = 1;
Nathan Scott70e73f52006-03-17 17:26:52 +1100208 ASSERT(be16_to_cpu(dfp->length) == INT_GET(dup->length, ARCH_CONVERT));
209 } else if (off < be16_to_cpu(dfp->offset))
210 ASSERT(off + INT_GET(dup->length, ARCH_CONVERT) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100212 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
213 ASSERT(matched || be16_to_cpu(dfp->length) >= INT_GET(dup->length, ARCH_CONVERT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (dfp > &d->hdr.bestfree[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100215 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217#endif
218 /*
219 * If this is smaller than the smallest bestfree entry,
220 * it can't be there since they're sorted.
221 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100222 if (INT_GET(dup->length, ARCH_CONVERT) <
223 be16_to_cpu(d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return NULL;
225 /*
226 * Look at the three bestfree entries for our guy.
227 */
228 for (dfp = &d->hdr.bestfree[0];
229 dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
230 dfp++) {
231 if (!dfp->offset)
232 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100233 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return dfp;
235 }
236 /*
237 * Didn't find it. This only happens if there are duplicate lengths.
238 */
239 return NULL;
240}
241
242/*
243 * Insert an unused-space entry into the bestfree table.
244 */
245xfs_dir2_data_free_t * /* entry inserted */
246xfs_dir2_data_freeinsert(
247 xfs_dir2_data_t *d, /* data block pointer */
248 xfs_dir2_data_unused_t *dup, /* unused space */
249 int *loghead) /* log the data header (out) */
250{
251 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
252 xfs_dir2_data_free_t new; /* new bestfree entry */
253
254#ifdef __KERNEL__
Nathan Scott70e73f52006-03-17 17:26:52 +1100255 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
256 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#endif
258 dfp = d->hdr.bestfree;
Nathan Scott70e73f52006-03-17 17:26:52 +1100259 new.length = dup->length;
260 new.offset = cpu_to_be16((char *)dup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /*
262 * Insert at position 0, 1, or 2; or not at all.
263 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100264 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 dfp[2] = dfp[1];
266 dfp[1] = dfp[0];
267 dfp[0] = new;
268 *loghead = 1;
269 return &dfp[0];
270 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100271 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 dfp[2] = dfp[1];
273 dfp[1] = new;
274 *loghead = 1;
275 return &dfp[1];
276 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100277 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 dfp[2] = new;
279 *loghead = 1;
280 return &dfp[2];
281 }
282 return NULL;
283}
284
285/*
286 * Remove a bestfree entry from the table.
287 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000288STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289xfs_dir2_data_freeremove(
290 xfs_dir2_data_t *d, /* data block pointer */
291 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
292 int *loghead) /* out: log data header */
293{
294#ifdef __KERNEL__
Nathan Scott70e73f52006-03-17 17:26:52 +1100295 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
296 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#endif
298 /*
299 * It's the first entry, slide the next 2 up.
300 */
301 if (dfp == &d->hdr.bestfree[0]) {
302 d->hdr.bestfree[0] = d->hdr.bestfree[1];
303 d->hdr.bestfree[1] = d->hdr.bestfree[2];
304 }
305 /*
306 * It's the second entry, slide the 3rd entry up.
307 */
308 else if (dfp == &d->hdr.bestfree[1])
309 d->hdr.bestfree[1] = d->hdr.bestfree[2];
310 /*
311 * Must be the last entry.
312 */
313 else
314 ASSERT(dfp == &d->hdr.bestfree[2]);
315 /*
316 * Clear the 3rd entry, must be zero now.
317 */
318 d->hdr.bestfree[2].length = 0;
319 d->hdr.bestfree[2].offset = 0;
320 *loghead = 1;
321}
322
323/*
324 * Given a data block, reconstruct its bestfree map.
325 */
326void
327xfs_dir2_data_freescan(
328 xfs_mount_t *mp, /* filesystem mount point */
329 xfs_dir2_data_t *d, /* data block pointer */
330 int *loghead, /* out: log data header */
331 char *aendp) /* in: caller's endp */
332{
333 xfs_dir2_block_tail_t *btp; /* block tail */
334 xfs_dir2_data_entry_t *dep; /* active data entry */
335 xfs_dir2_data_unused_t *dup; /* unused data entry */
336 char *endp; /* end of block's data */
337 char *p; /* current entry pointer */
338
339#ifdef __KERNEL__
Nathan Scott70e73f52006-03-17 17:26:52 +1100340 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
341 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342#endif
343 /*
344 * Start by clearing the table.
345 */
346 memset(d->hdr.bestfree, 0, sizeof(d->hdr.bestfree));
347 *loghead = 1;
348 /*
349 * Set up pointers.
350 */
351 p = (char *)d->u;
352 if (aendp)
353 endp = aendp;
Nathan Scott70e73f52006-03-17 17:26:52 +1100354 else if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
356 endp = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
357 } else
358 endp = (char *)d + mp->m_dirblksize;
359 /*
360 * Loop over the block's entries.
361 */
362 while (p < endp) {
363 dup = (xfs_dir2_data_unused_t *)p;
364 /*
365 * If it's a free entry, insert it.
366 */
367 if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
368 ASSERT((char *)dup - (char *)d ==
369 INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT));
370 xfs_dir2_data_freeinsert(d, dup, loghead);
371 p += INT_GET(dup->length, ARCH_CONVERT);
372 }
373 /*
374 * For active entries, check their tags and skip them.
375 */
376 else {
377 dep = (xfs_dir2_data_entry_t *)p;
378 ASSERT((char *)dep - (char *)d ==
379 INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep), ARCH_CONVERT));
380 p += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
381 }
382 }
383}
384
385/*
386 * Initialize a data block at the given block number in the directory.
387 * Give back the buffer for the created block.
388 */
389int /* error */
390xfs_dir2_data_init(
391 xfs_da_args_t *args, /* directory operation args */
392 xfs_dir2_db_t blkno, /* logical dir block number */
393 xfs_dabuf_t **bpp) /* output block buffer */
394{
395 xfs_dabuf_t *bp; /* block buffer */
396 xfs_dir2_data_t *d; /* pointer to block */
397 xfs_inode_t *dp; /* incore directory inode */
398 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
399 int error; /* error return value */
400 int i; /* bestfree index */
401 xfs_mount_t *mp; /* filesystem mount point */
402 xfs_trans_t *tp; /* transaction pointer */
403 int t; /* temp */
404
405 dp = args->dp;
406 mp = dp->i_mount;
407 tp = args->trans;
408 /*
409 * Get the buffer set up for the block.
410 */
411 error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, blkno), -1, &bp,
412 XFS_DATA_FORK);
413 if (error) {
414 return error;
415 }
416 ASSERT(bp != NULL);
417 /*
418 * Initialize the header.
419 */
420 d = bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +1100421 d->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
422 d->hdr.bestfree[0].offset = cpu_to_be16(sizeof(d->hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
424 d->hdr.bestfree[i].length = 0;
425 d->hdr.bestfree[i].offset = 0;
426 }
427 /*
428 * Set up an unused entry for the block's body.
429 */
430 dup = &d->u[0].unused;
431 INT_SET(dup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
432
433 t=mp->m_dirblksize - (uint)sizeof(d->hdr);
Nathan Scott70e73f52006-03-17 17:26:52 +1100434 d->hdr.bestfree[0].length = cpu_to_be16(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 INT_SET(dup->length, ARCH_CONVERT, t);
436 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT,
437 (xfs_dir2_data_off_t)((char *)dup - (char *)d));
438 /*
439 * Log it and return it.
440 */
441 xfs_dir2_data_log_header(tp, bp);
442 xfs_dir2_data_log_unused(tp, bp, dup);
443 *bpp = bp;
444 return 0;
445}
446
447/*
448 * Log an active data entry from the block.
449 */
450void
451xfs_dir2_data_log_entry(
452 xfs_trans_t *tp, /* transaction pointer */
453 xfs_dabuf_t *bp, /* block buffer */
454 xfs_dir2_data_entry_t *dep) /* data entry pointer */
455{
456 xfs_dir2_data_t *d; /* data block pointer */
457
458 d = bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +1100459 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
460 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 xfs_da_log_buf(tp, bp, (uint)((char *)dep - (char *)d),
462 (uint)((char *)(XFS_DIR2_DATA_ENTRY_TAG_P(dep) + 1) -
463 (char *)d - 1));
464}
465
466/*
467 * Log a data block header.
468 */
469void
470xfs_dir2_data_log_header(
471 xfs_trans_t *tp, /* transaction pointer */
472 xfs_dabuf_t *bp) /* block buffer */
473{
474 xfs_dir2_data_t *d; /* data block pointer */
475
476 d = bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +1100477 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
478 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 xfs_da_log_buf(tp, bp, (uint)((char *)&d->hdr - (char *)d),
480 (uint)(sizeof(d->hdr) - 1));
481}
482
483/*
484 * Log a data unused entry.
485 */
486void
487xfs_dir2_data_log_unused(
488 xfs_trans_t *tp, /* transaction pointer */
489 xfs_dabuf_t *bp, /* block buffer */
490 xfs_dir2_data_unused_t *dup) /* data unused pointer */
491{
492 xfs_dir2_data_t *d; /* data block pointer */
493
494 d = bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +1100495 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
496 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /*
498 * Log the first part of the unused entry.
499 */
500 xfs_da_log_buf(tp, bp, (uint)((char *)dup - (char *)d),
501 (uint)((char *)&dup->length + sizeof(dup->length) -
502 1 - (char *)d));
503 /*
504 * Log the end (tag) of the unused entry.
505 */
506 xfs_da_log_buf(tp, bp,
507 (uint)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup) - (char *)d),
508 (uint)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup) - (char *)d +
509 sizeof(xfs_dir2_data_off_t) - 1));
510}
511
512/*
513 * Make a byte range in the data block unused.
514 * Its current contents are unimportant.
515 */
516void
517xfs_dir2_data_make_free(
518 xfs_trans_t *tp, /* transaction pointer */
519 xfs_dabuf_t *bp, /* block buffer */
520 xfs_dir2_data_aoff_t offset, /* starting byte offset */
521 xfs_dir2_data_aoff_t len, /* length in bytes */
522 int *needlogp, /* out: log header */
523 int *needscanp) /* out: regen bestfree */
524{
525 xfs_dir2_data_t *d; /* data block pointer */
526 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
527 char *endptr; /* end of data area */
528 xfs_mount_t *mp; /* filesystem mount point */
529 int needscan; /* need to regen bestfree */
530 xfs_dir2_data_unused_t *newdup; /* new unused entry */
531 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
532 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
533
534 mp = tp->t_mountp;
535 d = bp->data;
536 /*
537 * Figure out where the end of the data area is.
538 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100539 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 endptr = (char *)d + mp->m_dirblksize;
541 else {
542 xfs_dir2_block_tail_t *btp; /* block tail */
543
Nathan Scott70e73f52006-03-17 17:26:52 +1100544 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
546 endptr = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
547 }
548 /*
549 * If this isn't the start of the block, then back up to
550 * the previous entry and see if it's free.
551 */
552 if (offset > sizeof(d->hdr)) {
553 xfs_dir2_data_off_t *tagp; /* tag just before us */
554
555 tagp = (xfs_dir2_data_off_t *)((char *)d + offset) - 1;
556 prevdup = (xfs_dir2_data_unused_t *)((char *)d + INT_GET(*tagp, ARCH_CONVERT));
557 if (INT_GET(prevdup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG)
558 prevdup = NULL;
559 } else
560 prevdup = NULL;
561 /*
562 * If this isn't the end of the block, see if the entry after
563 * us is free.
564 */
565 if ((char *)d + offset + len < endptr) {
566 postdup =
567 (xfs_dir2_data_unused_t *)((char *)d + offset + len);
568 if (INT_GET(postdup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG)
569 postdup = NULL;
570 } else
571 postdup = NULL;
572 ASSERT(*needscanp == 0);
573 needscan = 0;
574 /*
575 * Previous and following entries are both free,
576 * merge everything into a single free entry.
577 */
578 if (prevdup && postdup) {
579 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
580
581 /*
582 * See if prevdup and/or postdup are in bestfree table.
583 */
584 dfp = xfs_dir2_data_freefind(d, prevdup);
585 dfp2 = xfs_dir2_data_freefind(d, postdup);
586 /*
587 * We need a rescan unless there are exactly 2 free entries
588 * namely our two. Then we know what's happening, otherwise
589 * since the third bestfree is there, there might be more
590 * entries.
591 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100592 needscan = (d->hdr.bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /*
594 * Fix up the new big freespace.
595 */
596 INT_MOD(prevdup->length, ARCH_CONVERT, len + INT_GET(postdup->length, ARCH_CONVERT));
597 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup), ARCH_CONVERT,
598 (xfs_dir2_data_off_t)((char *)prevdup - (char *)d));
599 xfs_dir2_data_log_unused(tp, bp, prevdup);
600 if (!needscan) {
601 /*
602 * Has to be the case that entries 0 and 1 are
603 * dfp and dfp2 (don't know which is which), and
604 * entry 2 is empty.
605 * Remove entry 1 first then entry 0.
606 */
607 ASSERT(dfp && dfp2);
608 if (dfp == &d->hdr.bestfree[1]) {
609 dfp = &d->hdr.bestfree[0];
610 ASSERT(dfp2 == dfp);
611 dfp2 = &d->hdr.bestfree[1];
612 }
613 xfs_dir2_data_freeremove(d, dfp2, needlogp);
614 xfs_dir2_data_freeremove(d, dfp, needlogp);
615 /*
616 * Now insert the new entry.
617 */
618 dfp = xfs_dir2_data_freeinsert(d, prevdup, needlogp);
619 ASSERT(dfp == &d->hdr.bestfree[0]);
Nathan Scott70e73f52006-03-17 17:26:52 +1100620 ASSERT(be16_to_cpu(dfp->length) == INT_GET(prevdup->length, ARCH_CONVERT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 ASSERT(!dfp[1].length);
622 ASSERT(!dfp[2].length);
623 }
624 }
625 /*
626 * The entry before us is free, merge with it.
627 */
628 else if (prevdup) {
629 dfp = xfs_dir2_data_freefind(d, prevdup);
630 INT_MOD(prevdup->length, ARCH_CONVERT, len);
631 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup), ARCH_CONVERT,
632 (xfs_dir2_data_off_t)((char *)prevdup - (char *)d));
633 xfs_dir2_data_log_unused(tp, bp, prevdup);
634 /*
635 * If the previous entry was in the table, the new entry
636 * is longer, so it will be in the table too. Remove
637 * the old one and add the new one.
638 */
639 if (dfp) {
640 xfs_dir2_data_freeremove(d, dfp, needlogp);
641 (void)xfs_dir2_data_freeinsert(d, prevdup, needlogp);
642 }
643 /*
644 * Otherwise we need a scan if the new entry is big enough.
645 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100646 else {
647 needscan = INT_GET(prevdup->length, ARCH_CONVERT) >
648 be16_to_cpu(d->hdr.bestfree[2].length);
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 /*
652 * The following entry is free, merge with it.
653 */
654 else if (postdup) {
655 dfp = xfs_dir2_data_freefind(d, postdup);
656 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
657 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
658 INT_SET(newdup->length, ARCH_CONVERT, len + INT_GET(postdup->length, ARCH_CONVERT));
659 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
660 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
661 xfs_dir2_data_log_unused(tp, bp, newdup);
662 /*
663 * If the following entry was in the table, the new entry
664 * is longer, so it will be in the table too. Remove
665 * the old one and add the new one.
666 */
667 if (dfp) {
668 xfs_dir2_data_freeremove(d, dfp, needlogp);
669 (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
670 }
671 /*
672 * Otherwise we need a scan if the new entry is big enough.
673 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100674 else {
675 needscan = INT_GET(newdup->length, ARCH_CONVERT) >
676 be16_to_cpu(d->hdr.bestfree[2].length);
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679 /*
680 * Neither neighbor is free. Make a new entry.
681 */
682 else {
683 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
684 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
685 INT_SET(newdup->length, ARCH_CONVERT, len);
686 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
687 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
688 xfs_dir2_data_log_unused(tp, bp, newdup);
689 (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
690 }
691 *needscanp = needscan;
692}
693
694/*
695 * Take a byte range out of an existing unused space and make it un-free.
696 */
697void
698xfs_dir2_data_use_free(
699 xfs_trans_t *tp, /* transaction pointer */
700 xfs_dabuf_t *bp, /* data block buffer */
701 xfs_dir2_data_unused_t *dup, /* unused entry */
702 xfs_dir2_data_aoff_t offset, /* starting offset to use */
703 xfs_dir2_data_aoff_t len, /* length to use */
704 int *needlogp, /* out: need to log header */
705 int *needscanp) /* out: need regen bestfree */
706{
707 xfs_dir2_data_t *d; /* data block */
708 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
709 int matchback; /* matches end of freespace */
710 int matchfront; /* matches start of freespace */
711 int needscan; /* need to regen bestfree */
712 xfs_dir2_data_unused_t *newdup; /* new unused entry */
713 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
714 int oldlen; /* old unused entry's length */
715
716 d = bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +1100717 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
718 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 ASSERT(INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG);
720 ASSERT(offset >= (char *)dup - (char *)d);
721 ASSERT(offset + len <= (char *)dup + INT_GET(dup->length, ARCH_CONVERT) - (char *)d);
722 ASSERT((char *)dup - (char *)d == INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT));
723 /*
724 * Look up the entry in the bestfree table.
725 */
726 dfp = xfs_dir2_data_freefind(d, dup);
727 oldlen = INT_GET(dup->length, ARCH_CONVERT);
Nathan Scott70e73f52006-03-17 17:26:52 +1100728 ASSERT(dfp || oldlen <= be16_to_cpu(d->hdr.bestfree[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /*
730 * Check for alignment with front and back of the entry.
731 */
732 matchfront = (char *)dup - (char *)d == offset;
733 matchback = (char *)dup + oldlen - (char *)d == offset + len;
734 ASSERT(*needscanp == 0);
735 needscan = 0;
736 /*
737 * If we matched it exactly we just need to get rid of it from
738 * the bestfree table.
739 */
740 if (matchfront && matchback) {
741 if (dfp) {
Nathan Scott70e73f52006-03-17 17:26:52 +1100742 needscan = (d->hdr.bestfree[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (!needscan)
744 xfs_dir2_data_freeremove(d, dfp, needlogp);
745 }
746 }
747 /*
748 * We match the first part of the entry.
749 * Make a new entry with the remaining freespace.
750 */
751 else if (matchfront) {
752 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
753 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
754 INT_SET(newdup->length, ARCH_CONVERT, oldlen - len);
755 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
756 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
757 xfs_dir2_data_log_unused(tp, bp, newdup);
758 /*
759 * If it was in the table, remove it and add the new one.
760 */
761 if (dfp) {
762 xfs_dir2_data_freeremove(d, dfp, needlogp);
763 dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
764 ASSERT(dfp != NULL);
Nathan Scott70e73f52006-03-17 17:26:52 +1100765 ASSERT(be16_to_cpu(dfp->length) == INT_GET(newdup->length, ARCH_CONVERT));
766 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
768 * If we got inserted at the last slot,
769 * that means we don't know if there was a better
770 * choice for the last slot, or not. Rescan.
771 */
772 needscan = dfp == &d->hdr.bestfree[2];
773 }
774 }
775 /*
776 * We match the last part of the entry.
777 * Trim the allocated space off the tail of the entry.
778 */
779 else if (matchback) {
780 newdup = dup;
781 INT_SET(newdup->length, ARCH_CONVERT, (xfs_dir2_data_off_t)
782 (((char *)d + offset) - (char *)newdup));
783 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
784 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
785 xfs_dir2_data_log_unused(tp, bp, newdup);
786 /*
787 * If it was in the table, remove it and add the new one.
788 */
789 if (dfp) {
790 xfs_dir2_data_freeremove(d, dfp, needlogp);
791 dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
792 ASSERT(dfp != NULL);
Nathan Scott70e73f52006-03-17 17:26:52 +1100793 ASSERT(be16_to_cpu(dfp->length) == INT_GET(newdup->length, ARCH_CONVERT));
794 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /*
796 * If we got inserted at the last slot,
797 * that means we don't know if there was a better
798 * choice for the last slot, or not. Rescan.
799 */
800 needscan = dfp == &d->hdr.bestfree[2];
801 }
802 }
803 /*
804 * Poking out the middle of an entry.
805 * Make two new entries.
806 */
807 else {
808 newdup = dup;
809 INT_SET(newdup->length, ARCH_CONVERT, (xfs_dir2_data_off_t)
810 (((char *)d + offset) - (char *)newdup));
811 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
812 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
813 xfs_dir2_data_log_unused(tp, bp, newdup);
814 newdup2 = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
815 INT_SET(newdup2->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
816 INT_SET(newdup2->length, ARCH_CONVERT, oldlen - len - INT_GET(newdup->length, ARCH_CONVERT));
817 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup2), ARCH_CONVERT,
818 (xfs_dir2_data_off_t)((char *)newdup2 - (char *)d));
819 xfs_dir2_data_log_unused(tp, bp, newdup2);
820 /*
821 * If the old entry was in the table, we need to scan
822 * if the 3rd entry was valid, since these entries
823 * are smaller than the old one.
824 * If we don't need to scan that means there were 1 or 2
825 * entries in the table, and removing the old and adding
826 * the 2 new will work.
827 */
828 if (dfp) {
Nathan Scott70e73f52006-03-17 17:26:52 +1100829 needscan = (d->hdr.bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (!needscan) {
831 xfs_dir2_data_freeremove(d, dfp, needlogp);
832 (void)xfs_dir2_data_freeinsert(d, newdup,
833 needlogp);
834 (void)xfs_dir2_data_freeinsert(d, newdup2,
835 needlogp);
836 }
837 }
838 }
839 *needscanp = needscan;
840}