blob: 498f8d694330dba475aad5c1c1d1147f73bfab9c [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"
David Chinnerda353b02007-08-28 14:00:13 +100025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dinode.h"
34#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir2_data.h"
36#include "xfs_dir2_leaf.h"
37#include "xfs_dir2_block.h"
38#include "xfs_error.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 */
53 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
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 */
64 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
65 xfs_mount_t *mp; /* filesystem mount point */
66 char *p; /* current data position */
67 int stale; /* count of stale leaves */
Barry Naujok5163f952008-05-21 16:41:01 +100068 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 mp = dp->i_mount;
71 d = bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +110072 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
73 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 bf = d->hdr.bestfree;
75 p = (char *)d->u;
Nathan Scott70e73f52006-03-17 17:26:52 +110076 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100077 btp = xfs_dir2_block_tail_p(mp, (xfs_dir2_block_t *)d);
78 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 endp = (char *)lep;
80 } else
81 endp = (char *)d + mp->m_dirblksize;
82 count = lastfree = freeseen = 0;
83 /*
84 * Account for zero bestfree entries.
85 */
86 if (!bf[0].length) {
87 ASSERT(!bf[0].offset);
88 freeseen |= 1 << 0;
89 }
90 if (!bf[1].length) {
91 ASSERT(!bf[1].offset);
92 freeseen |= 1 << 1;
93 }
94 if (!bf[2].length) {
95 ASSERT(!bf[2].offset);
96 freeseen |= 1 << 2;
97 }
Nathan Scott70e73f52006-03-17 17:26:52 +110098 ASSERT(be16_to_cpu(bf[0].length) >= be16_to_cpu(bf[1].length));
99 ASSERT(be16_to_cpu(bf[1].length) >= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /*
101 * Loop over the data/unused entries.
102 */
103 while (p < endp) {
104 dup = (xfs_dir2_data_unused_t *)p;
105 /*
106 * If it's unused, look for the space in the bestfree table.
107 * If we find it, account for that, else make sure it
108 * doesn't need to be there.
109 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100110 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 ASSERT(lastfree == 0);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000112 ASSERT(be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 (char *)dup - (char *)d);
114 dfp = xfs_dir2_data_freefind(d, dup);
115 if (dfp) {
116 i = (int)(dfp - bf);
117 ASSERT((freeseen & (1 << i)) == 0);
118 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100119 } else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100120 ASSERT(be16_to_cpu(dup->length) <=
Nathan Scott70e73f52006-03-17 17:26:52 +1100121 be16_to_cpu(bf[2].length));
122 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100123 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 lastfree = 1;
125 continue;
126 }
127 /*
128 * It's a real entry. Validate the fields.
129 * If this is a block directory then make sure it's
130 * in the leaf section of the block.
131 * The linear search is crude but this is DEBUG code.
132 */
133 dep = (xfs_dir2_data_entry_t *)p;
134 ASSERT(dep->namelen != 0);
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000135 ASSERT(xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)) == 0);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000136 ASSERT(be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 (char *)dep - (char *)d);
138 count++;
139 lastfree = 0;
Nathan Scott70e73f52006-03-17 17:26:52 +1100140 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000141 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 (xfs_dir2_data_aoff_t)
143 ((char *)dep - (char *)d));
Barry Naujok5163f952008-05-21 16:41:01 +1000144 name.name = dep->name;
145 name.len = dep->namelen;
146 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100147 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100148 if (be32_to_cpu(lep[i].address) == addr &&
149 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 break;
151 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100152 ASSERT(i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000154 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 /*
157 * Need to have seen all the entries and all the bestfree slots.
158 */
159 ASSERT(freeseen == 7);
Nathan Scott70e73f52006-03-17 17:26:52 +1100160 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100161 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100162 if (be32_to_cpu(lep[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 stale++;
164 if (i > 0)
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100165 ASSERT(be32_to_cpu(lep[i].hashval) >= be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100167 ASSERT(count == be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
168 ASSERT(stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170}
171#endif
172
173/*
174 * Given a data block and an unused entry from that block,
175 * return the bestfree entry if any that corresponds to it.
176 */
177xfs_dir2_data_free_t *
178xfs_dir2_data_freefind(
179 xfs_dir2_data_t *d, /* data block */
180 xfs_dir2_data_unused_t *dup) /* data unused entry */
181{
182 xfs_dir2_data_free_t *dfp; /* bestfree entry */
183 xfs_dir2_data_aoff_t off; /* offset value needed */
184#if defined(DEBUG) && defined(__KERNEL__)
185 int matched; /* matched the value */
186 int seenzero; /* saw a 0 bestfree entry */
187#endif
188
189 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)d);
190#if defined(DEBUG) && defined(__KERNEL__)
191 /*
192 * Validate some consistency in the bestfree table.
193 * Check order, non-overlapping entries, and if we find the
194 * one we're looking for it has to be exact.
195 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100196 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
197 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 for (dfp = &d->hdr.bestfree[0], seenzero = matched = 0;
199 dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
200 dfp++) {
201 if (!dfp->offset) {
202 ASSERT(!dfp->length);
203 seenzero = 1;
204 continue;
205 }
206 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100207 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100209 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100210 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100211 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100213 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100214 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (dfp > &d->hdr.bestfree[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100216 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218#endif
219 /*
220 * If this is smaller than the smallest bestfree entry,
221 * it can't be there since they're sorted.
222 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100223 if (be16_to_cpu(dup->length) <
Nathan Scott70e73f52006-03-17 17:26:52 +1100224 be16_to_cpu(d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return NULL;
226 /*
227 * Look at the three bestfree entries for our guy.
228 */
229 for (dfp = &d->hdr.bestfree[0];
230 dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
231 dfp++) {
232 if (!dfp->offset)
233 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100234 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return dfp;
236 }
237 /*
238 * Didn't find it. This only happens if there are duplicate lengths.
239 */
240 return NULL;
241}
242
243/*
244 * Insert an unused-space entry into the bestfree table.
245 */
246xfs_dir2_data_free_t * /* entry inserted */
247xfs_dir2_data_freeinsert(
248 xfs_dir2_data_t *d, /* data block pointer */
249 xfs_dir2_data_unused_t *dup, /* unused space */
250 int *loghead) /* log the data header (out) */
251{
252 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
253 xfs_dir2_data_free_t new; /* new bestfree entry */
254
255#ifdef __KERNEL__
Nathan Scott70e73f52006-03-17 17:26:52 +1100256 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
257 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#endif
259 dfp = d->hdr.bestfree;
Nathan Scott70e73f52006-03-17 17:26:52 +1100260 new.length = dup->length;
261 new.offset = cpu_to_be16((char *)dup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /*
263 * Insert at position 0, 1, or 2; or not at all.
264 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100265 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 dfp[2] = dfp[1];
267 dfp[1] = dfp[0];
268 dfp[0] = new;
269 *loghead = 1;
270 return &dfp[0];
271 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100272 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 dfp[2] = dfp[1];
274 dfp[1] = new;
275 *loghead = 1;
276 return &dfp[1];
277 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100278 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 dfp[2] = new;
280 *loghead = 1;
281 return &dfp[2];
282 }
283 return NULL;
284}
285
286/*
287 * Remove a bestfree entry from the table.
288 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000289STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290xfs_dir2_data_freeremove(
291 xfs_dir2_data_t *d, /* data block pointer */
292 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
293 int *loghead) /* out: log data header */
294{
295#ifdef __KERNEL__
Nathan Scott70e73f52006-03-17 17:26:52 +1100296 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
297 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#endif
299 /*
300 * It's the first entry, slide the next 2 up.
301 */
302 if (dfp == &d->hdr.bestfree[0]) {
303 d->hdr.bestfree[0] = d->hdr.bestfree[1];
304 d->hdr.bestfree[1] = d->hdr.bestfree[2];
305 }
306 /*
307 * It's the second entry, slide the 3rd entry up.
308 */
309 else if (dfp == &d->hdr.bestfree[1])
310 d->hdr.bestfree[1] = d->hdr.bestfree[2];
311 /*
312 * Must be the last entry.
313 */
314 else
315 ASSERT(dfp == &d->hdr.bestfree[2]);
316 /*
317 * Clear the 3rd entry, must be zero now.
318 */
319 d->hdr.bestfree[2].length = 0;
320 d->hdr.bestfree[2].offset = 0;
321 *loghead = 1;
322}
323
324/*
325 * Given a data block, reconstruct its bestfree map.
326 */
327void
328xfs_dir2_data_freescan(
329 xfs_mount_t *mp, /* filesystem mount point */
330 xfs_dir2_data_t *d, /* data block pointer */
Eric Sandeenef497f82007-05-08 13:48:49 +1000331 int *loghead) /* out: log data header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
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;
Eric Sandeenef497f82007-05-08 13:48:49 +1000352 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000353 btp = xfs_dir2_block_tail_p(mp, (xfs_dir2_block_t *)d);
354 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 } else
356 endp = (char *)d + mp->m_dirblksize;
357 /*
358 * Loop over the block's entries.
359 */
360 while (p < endp) {
361 dup = (xfs_dir2_data_unused_t *)p;
362 /*
363 * If it's a free entry, insert it.
364 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100365 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 ASSERT((char *)dup - (char *)d ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000367 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 xfs_dir2_data_freeinsert(d, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100369 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 /*
372 * For active entries, check their tags and skip them.
373 */
374 else {
375 dep = (xfs_dir2_data_entry_t *)p;
376 ASSERT((char *)dep - (char *)d ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000377 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
378 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 }
381}
382
383/*
384 * Initialize a data block at the given block number in the directory.
385 * Give back the buffer for the created block.
386 */
387int /* error */
388xfs_dir2_data_init(
389 xfs_da_args_t *args, /* directory operation args */
390 xfs_dir2_db_t blkno, /* logical dir block number */
391 xfs_dabuf_t **bpp) /* output block buffer */
392{
393 xfs_dabuf_t *bp; /* block buffer */
394 xfs_dir2_data_t *d; /* pointer to block */
395 xfs_inode_t *dp; /* incore directory inode */
396 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
397 int error; /* error return value */
398 int i; /* bestfree index */
399 xfs_mount_t *mp; /* filesystem mount point */
400 xfs_trans_t *tp; /* transaction pointer */
401 int t; /* temp */
402
403 dp = args->dp;
404 mp = dp->i_mount;
405 tp = args->trans;
406 /*
407 * Get the buffer set up for the block.
408 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000409 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 XFS_DATA_FORK);
411 if (error) {
412 return error;
413 }
414 ASSERT(bp != NULL);
415 /*
416 * Initialize the header.
417 */
418 d = bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +1100419 d->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
420 d->hdr.bestfree[0].offset = cpu_to_be16(sizeof(d->hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
422 d->hdr.bestfree[i].length = 0;
423 d->hdr.bestfree[i].offset = 0;
424 }
425 /*
426 * Set up an unused entry for the block's body.
427 */
428 dup = &d->u[0].unused;
Nathan Scottad354eb2006-03-17 17:27:37 +1100429 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 t=mp->m_dirblksize - (uint)sizeof(d->hdr);
Nathan Scott70e73f52006-03-17 17:26:52 +1100432 d->hdr.bestfree[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100433 dup->length = cpu_to_be16(t);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000434 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /*
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 */
447void
448xfs_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;
Nathan Scott70e73f52006-03-17 17:26:52 +1100456 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
457 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 xfs_da_log_buf(tp, bp, (uint)((char *)dep - (char *)d),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000459 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 (char *)d - 1));
461}
462
463/*
464 * Log a data block header.
465 */
466void
467xfs_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;
Nathan Scott70e73f52006-03-17 17:26:52 +1100474 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
475 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 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 */
483void
484xfs_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;
Nathan Scott70e73f52006-03-17 17:26:52 +1100492 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
493 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /*
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,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000504 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)d),
505 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)d +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 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 */
513void
514xfs_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 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100536 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 endptr = (char *)d + mp->m_dirblksize;
538 else {
539 xfs_dir2_block_tail_t *btp; /* block tail */
540
Nathan Scott70e73f52006-03-17 17:26:52 +1100541 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000542 btp = xfs_dir2_block_tail_p(mp, (xfs_dir2_block_t *)d);
543 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
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)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100550 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Nathan Scott3d693c62006-03-17 17:28:27 +1100552 tagp = (__be16 *)((char *)d + offset) - 1;
553 prevdup = (xfs_dir2_data_unused_t *)((char *)d + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100554 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 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);
Nathan Scottad354eb2006-03-17 17:27:37 +1100565 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 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 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100589 needscan = (d->hdr.bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /*
591 * Fix up the new big freespace.
592 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800593 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000594 *xfs_dir2_data_unused_tag_p(prevdup) =
Nathan Scott1fba9f72006-03-17 17:27:47 +1100595 cpu_to_be16((char *)prevdup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 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]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100617 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 ASSERT(!dfp[1].length);
619 ASSERT(!dfp[2].length);
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);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800627 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000628 *xfs_dir2_data_unused_tag_p(prevdup) =
Nathan Scott1fba9f72006-03-17 17:27:47 +1100629 cpu_to_be16((char *)prevdup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 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 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100643 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100644 needscan = be16_to_cpu(prevdup->length) >
Nathan Scott70e73f52006-03-17 17:26:52 +1100645 be16_to_cpu(d->hdr.bestfree[2].length);
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 /*
649 * The following entry is free, merge with it.
650 */
651 else if (postdup) {
652 dfp = xfs_dir2_data_freefind(d, postdup);
653 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100654 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
655 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000656 *xfs_dir2_data_unused_tag_p(newdup) =
Nathan Scott1fba9f72006-03-17 17:27:47 +1100657 cpu_to_be16((char *)newdup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfs_dir2_data_log_unused(tp, bp, newdup);
659 /*
660 * If the following entry was in the table, the new entry
661 * is longer, so it will be in the table too. Remove
662 * the old one and add the new one.
663 */
664 if (dfp) {
665 xfs_dir2_data_freeremove(d, dfp, needlogp);
666 (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
667 }
668 /*
669 * Otherwise we need a scan if the new entry is big enough.
670 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100671 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100672 needscan = be16_to_cpu(newdup->length) >
Nathan Scott70e73f52006-03-17 17:26:52 +1100673 be16_to_cpu(d->hdr.bestfree[2].length);
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 /*
677 * Neither neighbor is free. Make a new entry.
678 */
679 else {
680 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100681 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
682 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000683 *xfs_dir2_data_unused_tag_p(newdup) =
Nathan Scott1fba9f72006-03-17 17:27:47 +1100684 cpu_to_be16((char *)newdup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 xfs_dir2_data_log_unused(tp, bp, newdup);
686 (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
687 }
688 *needscanp = needscan;
689}
690
691/*
692 * Take a byte range out of an existing unused space and make it un-free.
693 */
694void
695xfs_dir2_data_use_free(
696 xfs_trans_t *tp, /* transaction pointer */
697 xfs_dabuf_t *bp, /* data block buffer */
698 xfs_dir2_data_unused_t *dup, /* unused entry */
699 xfs_dir2_data_aoff_t offset, /* starting offset to use */
700 xfs_dir2_data_aoff_t len, /* length to use */
701 int *needlogp, /* out: need to log header */
702 int *needscanp) /* out: need regen bestfree */
703{
704 xfs_dir2_data_t *d; /* data block */
705 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
706 int matchback; /* matches end of freespace */
707 int matchfront; /* matches start of freespace */
708 int needscan; /* need to regen bestfree */
709 xfs_dir2_data_unused_t *newdup; /* new unused entry */
710 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
711 int oldlen; /* old unused entry's length */
712
713 d = bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +1100714 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
715 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
Nathan Scottad354eb2006-03-17 17:27:37 +1100716 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ASSERT(offset >= (char *)dup - (char *)d);
Nathan Scottad354eb2006-03-17 17:27:37 +1100718 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)d);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000719 ASSERT((char *)dup - (char *)d == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /*
721 * Look up the entry in the bestfree table.
722 */
723 dfp = xfs_dir2_data_freefind(d, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100724 oldlen = be16_to_cpu(dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100725 ASSERT(dfp || oldlen <= be16_to_cpu(d->hdr.bestfree[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 * Check for alignment with front and back of the entry.
728 */
729 matchfront = (char *)dup - (char *)d == offset;
730 matchback = (char *)dup + oldlen - (char *)d == offset + len;
731 ASSERT(*needscanp == 0);
732 needscan = 0;
733 /*
734 * If we matched it exactly we just need to get rid of it from
735 * the bestfree table.
736 */
737 if (matchfront && matchback) {
738 if (dfp) {
Nathan Scott70e73f52006-03-17 17:26:52 +1100739 needscan = (d->hdr.bestfree[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (!needscan)
741 xfs_dir2_data_freeremove(d, dfp, needlogp);
742 }
743 }
744 /*
745 * We match the first part of the entry.
746 * Make a new entry with the remaining freespace.
747 */
748 else if (matchfront) {
749 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100750 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
751 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000752 *xfs_dir2_data_unused_tag_p(newdup) =
Nathan Scott1fba9f72006-03-17 17:27:47 +1100753 cpu_to_be16((char *)newdup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 xfs_dir2_data_log_unused(tp, bp, newdup);
755 /*
756 * If it was in the table, remove it and add the new one.
757 */
758 if (dfp) {
759 xfs_dir2_data_freeremove(d, dfp, needlogp);
760 dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
761 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100762 ASSERT(dfp->length == newdup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100763 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
765 * If we got inserted at the last slot,
766 * that means we don't know if there was a better
767 * choice for the last slot, or not. Rescan.
768 */
769 needscan = dfp == &d->hdr.bestfree[2];
770 }
771 }
772 /*
773 * We match the last part of the entry.
774 * Trim the allocated space off the tail of the entry.
775 */
776 else if (matchback) {
777 newdup = dup;
Nathan Scottad354eb2006-03-17 17:27:37 +1100778 newdup->length = cpu_to_be16(((char *)d + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000779 *xfs_dir2_data_unused_tag_p(newdup) =
Nathan Scott1fba9f72006-03-17 17:27:47 +1100780 cpu_to_be16((char *)newdup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 xfs_dir2_data_log_unused(tp, bp, newdup);
782 /*
783 * If it was in the table, remove it and add the new one.
784 */
785 if (dfp) {
786 xfs_dir2_data_freeremove(d, dfp, needlogp);
787 dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
788 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100789 ASSERT(dfp->length == newdup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100790 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /*
792 * If we got inserted at the last slot,
793 * that means we don't know if there was a better
794 * choice for the last slot, or not. Rescan.
795 */
796 needscan = dfp == &d->hdr.bestfree[2];
797 }
798 }
799 /*
800 * Poking out the middle of an entry.
801 * Make two new entries.
802 */
803 else {
804 newdup = dup;
Nathan Scottad354eb2006-03-17 17:27:37 +1100805 newdup->length = cpu_to_be16(((char *)d + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000806 *xfs_dir2_data_unused_tag_p(newdup) =
Nathan Scott1fba9f72006-03-17 17:27:47 +1100807 cpu_to_be16((char *)newdup - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 xfs_dir2_data_log_unused(tp, bp, newdup);
809 newdup2 = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100810 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
811 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000812 *xfs_dir2_data_unused_tag_p(newdup2) =
Nathan Scott1fba9f72006-03-17 17:27:47 +1100813 cpu_to_be16((char *)newdup2 - (char *)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 xfs_dir2_data_log_unused(tp, bp, newdup2);
815 /*
816 * If the old entry was in the table, we need to scan
817 * if the 3rd entry was valid, since these entries
818 * are smaller than the old one.
819 * If we don't need to scan that means there were 1 or 2
820 * entries in the table, and removing the old and adding
821 * the 2 new will work.
822 */
823 if (dfp) {
Nathan Scott70e73f52006-03-17 17:26:52 +1100824 needscan = (d->hdr.bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!needscan) {
826 xfs_dir2_data_freeremove(d, dfp, needlogp);
827 (void)xfs_dir2_data_freeinsert(d, newdup,
828 needlogp);
829 (void)xfs_dir2_data_freeinsert(d, newdup2,
830 needlogp);
831 }
832 }
833 }
834 *needscanp = needscan;
835}