blob: 2bbc21696944d82200969fbfdbb8e1d58401a4a6 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dir2_sf.h"
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_data.h"
34#include "xfs_dir2_leaf.h"
35#include "xfs_dir2_block.h"
36#include "xfs_error.h"
37
Christoph Hellwigc2066e22011-07-08 14:35:38 +020038STATIC xfs_dir2_data_free_t *
39xfs_dir2_data_freefind(xfs_dir2_data_hdr_t *hdr, xfs_dir2_data_unused_t *dup);
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifdef DEBUG
42/*
43 * Check the consistency of the data block.
44 * The input can also be a block-format directory.
45 * Pop an assert if we find anything bad.
46 */
47void
48xfs_dir2_data_check(
49 xfs_inode_t *dp, /* incore inode pointer */
50 xfs_dabuf_t *bp) /* data block's buffer */
51{
52 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
53 xfs_dir2_data_free_t *bf; /* bestfree table */
54 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
55 int count; /* count of entries found */
56 xfs_dir2_data_t *d; /* data block pointer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +020057 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 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 */
Barry Naujok5163f952008-05-21 16:41:01 +100070 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 mp = dp->i_mount;
73 d = bp->data;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020074 hdr = &d->hdr;
75 bf = hdr->bestfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 p = (char *)d->u;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020077
78 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
79 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100080 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 endp = (char *)lep;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020082 } else {
83 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
84 endp = (char *)hdr + mp->m_dirblksize;
85 }
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 count = lastfree = freeseen = 0;
88 /*
89 * Account for zero bestfree entries.
90 */
91 if (!bf[0].length) {
92 ASSERT(!bf[0].offset);
93 freeseen |= 1 << 0;
94 }
95 if (!bf[1].length) {
96 ASSERT(!bf[1].offset);
97 freeseen |= 1 << 1;
98 }
99 if (!bf[2].length) {
100 ASSERT(!bf[2].offset);
101 freeseen |= 1 << 2;
102 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100103 ASSERT(be16_to_cpu(bf[0].length) >= be16_to_cpu(bf[1].length));
104 ASSERT(be16_to_cpu(bf[1].length) >= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /*
106 * Loop over the data/unused entries.
107 */
108 while (p < endp) {
109 dup = (xfs_dir2_data_unused_t *)p;
110 /*
111 * If it's unused, look for the space in the bestfree table.
112 * If we find it, account for that, else make sure it
113 * doesn't need to be there.
114 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100115 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ASSERT(lastfree == 0);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000117 ASSERT(be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200118 (char *)dup - (char *)hdr);
119 dfp = xfs_dir2_data_freefind(hdr, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (dfp) {
121 i = (int)(dfp - bf);
122 ASSERT((freeseen & (1 << i)) == 0);
123 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100124 } else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100125 ASSERT(be16_to_cpu(dup->length) <=
Nathan Scott70e73f52006-03-17 17:26:52 +1100126 be16_to_cpu(bf[2].length));
127 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100128 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 lastfree = 1;
130 continue;
131 }
132 /*
133 * It's a real entry. Validate the fields.
134 * If this is a block directory then make sure it's
135 * in the leaf section of the block.
136 * The linear search is crude but this is DEBUG code.
137 */
138 dep = (xfs_dir2_data_entry_t *)p;
139 ASSERT(dep->namelen != 0);
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000140 ASSERT(xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)) == 0);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000141 ASSERT(be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200142 (char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 count++;
144 lastfree = 0;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200145 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000146 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 (xfs_dir2_data_aoff_t)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200148 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000149 name.name = dep->name;
150 name.len = dep->namelen;
151 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100152 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100153 if (be32_to_cpu(lep[i].address) == addr &&
154 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 break;
156 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100157 ASSERT(i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000159 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161 /*
162 * Need to have seen all the entries and all the bestfree slots.
163 */
164 ASSERT(freeseen == 7);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200165 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100166 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100167 if (be32_to_cpu(lep[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 stale++;
169 if (i > 0)
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100170 ASSERT(be32_to_cpu(lep[i].hashval) >= be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100172 ASSERT(count == be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
173 ASSERT(stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175}
176#endif
177
178/*
179 * Given a data block and an unused entry from that block,
180 * return the bestfree entry if any that corresponds to it.
181 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200182STATIC xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183xfs_dir2_data_freefind(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200184 xfs_dir2_data_hdr_t *hdr, /* data block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 xfs_dir2_data_unused_t *dup) /* data unused entry */
186{
187 xfs_dir2_data_free_t *dfp; /* bestfree entry */
188 xfs_dir2_data_aoff_t off; /* offset value needed */
189#if defined(DEBUG) && defined(__KERNEL__)
190 int matched; /* matched the value */
191 int seenzero; /* saw a 0 bestfree entry */
192#endif
193
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200194 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#if defined(DEBUG) && defined(__KERNEL__)
196 /*
197 * Validate some consistency in the bestfree table.
198 * Check order, non-overlapping entries, and if we find the
199 * one we're looking for it has to be exact.
200 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200201 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
202 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
203 for (dfp = &hdr->bestfree[0], seenzero = matched = 0;
204 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 dfp++) {
206 if (!dfp->offset) {
207 ASSERT(!dfp->length);
208 seenzero = 1;
209 continue;
210 }
211 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100212 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100214 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100215 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100216 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100218 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100219 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200220 if (dfp > &hdr->bestfree[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100221 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223#endif
224 /*
225 * If this is smaller than the smallest bestfree entry,
226 * it can't be there since they're sorted.
227 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100228 if (be16_to_cpu(dup->length) <
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200229 be16_to_cpu(hdr->bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return NULL;
231 /*
232 * Look at the three bestfree entries for our guy.
233 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200234 for (dfp = &hdr->bestfree[0];
235 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 dfp++) {
237 if (!dfp->offset)
238 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100239 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return dfp;
241 }
242 /*
243 * Didn't find it. This only happens if there are duplicate lengths.
244 */
245 return NULL;
246}
247
248/*
249 * Insert an unused-space entry into the bestfree table.
250 */
251xfs_dir2_data_free_t * /* entry inserted */
252xfs_dir2_data_freeinsert(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200253 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 xfs_dir2_data_unused_t *dup, /* unused space */
255 int *loghead) /* log the data header (out) */
256{
257 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
258 xfs_dir2_data_free_t new; /* new bestfree entry */
259
260#ifdef __KERNEL__
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200261 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
262 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#endif
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200264 dfp = hdr->bestfree;
Nathan Scott70e73f52006-03-17 17:26:52 +1100265 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200266 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /*
269 * Insert at position 0, 1, or 2; or not at all.
270 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100271 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 dfp[2] = dfp[1];
273 dfp[1] = dfp[0];
274 dfp[0] = new;
275 *loghead = 1;
276 return &dfp[0];
277 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100278 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 dfp[2] = dfp[1];
280 dfp[1] = new;
281 *loghead = 1;
282 return &dfp[1];
283 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100284 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 dfp[2] = new;
286 *loghead = 1;
287 return &dfp[2];
288 }
289 return NULL;
290}
291
292/*
293 * Remove a bestfree entry from the table.
294 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000295STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296xfs_dir2_data_freeremove(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200297 xfs_dir2_data_hdr_t *hdr, /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
299 int *loghead) /* out: log data header */
300{
301#ifdef __KERNEL__
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200302 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
303 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#endif
305 /*
306 * It's the first entry, slide the next 2 up.
307 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200308 if (dfp == &hdr->bestfree[0]) {
309 hdr->bestfree[0] = hdr->bestfree[1];
310 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 /*
313 * It's the second entry, slide the 3rd entry up.
314 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200315 else if (dfp == &hdr->bestfree[1])
316 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /*
318 * Must be the last entry.
319 */
320 else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200321 ASSERT(dfp == &hdr->bestfree[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /*
323 * Clear the 3rd entry, must be zero now.
324 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200325 hdr->bestfree[2].length = 0;
326 hdr->bestfree[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 *loghead = 1;
328}
329
330/*
331 * Given a data block, reconstruct its bestfree map.
332 */
333void
334xfs_dir2_data_freescan(
335 xfs_mount_t *mp, /* filesystem mount point */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200336 xfs_dir2_data_hdr_t *hdr, /* data block header */
Eric Sandeenef497f82007-05-08 13:48:49 +1000337 int *loghead) /* out: log data header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200339 xfs_dir2_data_t *d = (xfs_dir2_data_t *)hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 xfs_dir2_block_tail_t *btp; /* block tail */
341 xfs_dir2_data_entry_t *dep; /* active data entry */
342 xfs_dir2_data_unused_t *dup; /* unused data entry */
343 char *endp; /* end of block's data */
344 char *p; /* current entry pointer */
345
346#ifdef __KERNEL__
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200347 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
348 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349#endif
350 /*
351 * Start by clearing the table.
352 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200353 memset(hdr->bestfree, 0, sizeof(hdr->bestfree));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 *loghead = 1;
355 /*
356 * Set up pointers.
357 */
358 p = (char *)d->u;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200359 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
360 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000361 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 } else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200363 endp = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * Loop over the block's entries.
366 */
367 while (p < endp) {
368 dup = (xfs_dir2_data_unused_t *)p;
369 /*
370 * If it's a free entry, insert it.
371 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100372 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200373 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000374 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200375 xfs_dir2_data_freeinsert(hdr, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100376 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 /*
379 * For active entries, check their tags and skip them.
380 */
381 else {
382 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200383 ASSERT((char *)dep - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000384 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
385 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 }
388}
389
390/*
391 * Initialize a data block at the given block number in the directory.
392 * Give back the buffer for the created block.
393 */
394int /* error */
395xfs_dir2_data_init(
396 xfs_da_args_t *args, /* directory operation args */
397 xfs_dir2_db_t blkno, /* logical dir block number */
398 xfs_dabuf_t **bpp) /* output block buffer */
399{
400 xfs_dabuf_t *bp; /* block buffer */
401 xfs_dir2_data_t *d; /* pointer to block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200402 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 xfs_inode_t *dp; /* incore directory inode */
404 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
405 int error; /* error return value */
406 int i; /* bestfree index */
407 xfs_mount_t *mp; /* filesystem mount point */
408 xfs_trans_t *tp; /* transaction pointer */
409 int t; /* temp */
410
411 dp = args->dp;
412 mp = dp->i_mount;
413 tp = args->trans;
414 /*
415 * Get the buffer set up for the block.
416 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000417 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 XFS_DATA_FORK);
419 if (error) {
420 return error;
421 }
422 ASSERT(bp != NULL);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
425 * Initialize the header.
426 */
427 d = bp->data;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200428 hdr = &d->hdr;
429 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
430 hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200432 hdr->bestfree[i].length = 0;
433 hdr->bestfree[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /*
437 * Set up an unused entry for the block's body.
438 */
439 dup = &d->u[0].unused;
Nathan Scottad354eb2006-03-17 17:27:37 +1100440 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200442 t = mp->m_dirblksize - (uint)sizeof(*hdr);
443 hdr->bestfree[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100444 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200445 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * Log it and return it.
448 */
449 xfs_dir2_data_log_header(tp, bp);
450 xfs_dir2_data_log_unused(tp, bp, dup);
451 *bpp = bp;
452 return 0;
453}
454
455/*
456 * Log an active data entry from the block.
457 */
458void
459xfs_dir2_data_log_entry(
460 xfs_trans_t *tp, /* transaction pointer */
461 xfs_dabuf_t *bp, /* block buffer */
462 xfs_dir2_data_entry_t *dep) /* data entry pointer */
463{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200464 xfs_dir2_data_hdr_t *hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200466 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
467 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
468
469 xfs_da_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000470 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200471 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474/*
475 * Log a data block header.
476 */
477void
478xfs_dir2_data_log_header(
479 xfs_trans_t *tp, /* transaction pointer */
480 xfs_dabuf_t *bp) /* block buffer */
481{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200482 xfs_dir2_data_hdr_t *hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200484 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
485 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
486
487 xfs_da_log_buf(tp, bp, 0, sizeof(*hdr) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490/*
491 * Log a data unused entry.
492 */
493void
494xfs_dir2_data_log_unused(
495 xfs_trans_t *tp, /* transaction pointer */
496 xfs_dabuf_t *bp, /* block buffer */
497 xfs_dir2_data_unused_t *dup) /* data unused pointer */
498{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200499 xfs_dir2_data_hdr_t *hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200501 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
502 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
505 * Log the first part of the unused entry.
506 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200507 xfs_da_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200509 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /*
511 * Log the end (tag) of the unused entry.
512 */
513 xfs_da_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200514 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
515 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 sizeof(xfs_dir2_data_off_t) - 1));
517}
518
519/*
520 * Make a byte range in the data block unused.
521 * Its current contents are unimportant.
522 */
523void
524xfs_dir2_data_make_free(
525 xfs_trans_t *tp, /* transaction pointer */
526 xfs_dabuf_t *bp, /* block buffer */
527 xfs_dir2_data_aoff_t offset, /* starting byte offset */
528 xfs_dir2_data_aoff_t len, /* length in bytes */
529 int *needlogp, /* out: log header */
530 int *needscanp) /* out: regen bestfree */
531{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200532 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
534 char *endptr; /* end of data area */
535 xfs_mount_t *mp; /* filesystem mount point */
536 int needscan; /* need to regen bestfree */
537 xfs_dir2_data_unused_t *newdup; /* new unused entry */
538 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
539 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
540
541 mp = tp->t_mountp;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200542 hdr = bp->data;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /*
545 * Figure out where the end of the data area is.
546 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200547 if (be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC)
548 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 else {
550 xfs_dir2_block_tail_t *btp; /* block tail */
551
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200552 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
553 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000554 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 /*
557 * If this isn't the start of the block, then back up to
558 * the previous entry and see if it's free.
559 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200560 if (offset > sizeof(*hdr)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100561 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200563 tagp = (__be16 *)((char *)hdr + offset) - 1;
564 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100565 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 prevdup = NULL;
567 } else
568 prevdup = NULL;
569 /*
570 * If this isn't the end of the block, see if the entry after
571 * us is free.
572 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200573 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200575 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100576 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 postdup = NULL;
578 } else
579 postdup = NULL;
580 ASSERT(*needscanp == 0);
581 needscan = 0;
582 /*
583 * Previous and following entries are both free,
584 * merge everything into a single free entry.
585 */
586 if (prevdup && postdup) {
587 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
588
589 /*
590 * See if prevdup and/or postdup are in bestfree table.
591 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200592 dfp = xfs_dir2_data_freefind(hdr, prevdup);
593 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /*
595 * We need a rescan unless there are exactly 2 free entries
596 * namely our two. Then we know what's happening, otherwise
597 * since the third bestfree is there, there might be more
598 * entries.
599 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200600 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /*
602 * Fix up the new big freespace.
603 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800604 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000605 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200606 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 xfs_dir2_data_log_unused(tp, bp, prevdup);
608 if (!needscan) {
609 /*
610 * Has to be the case that entries 0 and 1 are
611 * dfp and dfp2 (don't know which is which), and
612 * entry 2 is empty.
613 * Remove entry 1 first then entry 0.
614 */
615 ASSERT(dfp && dfp2);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200616 if (dfp == &hdr->bestfree[1]) {
617 dfp = &hdr->bestfree[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 ASSERT(dfp2 == dfp);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200619 dfp2 = &hdr->bestfree[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200621 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
622 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /*
624 * Now insert the new entry.
625 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200626 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
627 ASSERT(dfp == &hdr->bestfree[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100628 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 ASSERT(!dfp[1].length);
630 ASSERT(!dfp[2].length);
631 }
632 }
633 /*
634 * The entry before us is free, merge with it.
635 */
636 else if (prevdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200637 dfp = xfs_dir2_data_freefind(hdr, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800638 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000639 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200640 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 xfs_dir2_data_log_unused(tp, bp, prevdup);
642 /*
643 * If the previous entry was in the table, the new entry
644 * is longer, so it will be in the table too. Remove
645 * the old one and add the new one.
646 */
647 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200648 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
649 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 /*
652 * Otherwise we need a scan if the new entry is big enough.
653 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100654 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100655 needscan = be16_to_cpu(prevdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200656 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 /*
660 * The following entry is free, merge with it.
661 */
662 else if (postdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200663 dfp = xfs_dir2_data_freefind(hdr, postdup);
664 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100665 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
666 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000667 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200668 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 xfs_dir2_data_log_unused(tp, bp, newdup);
670 /*
671 * If the following entry was in the table, the new entry
672 * is longer, so it will be in the table too. Remove
673 * the old one and add the new one.
674 */
675 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200676 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
677 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679 /*
680 * Otherwise we need a scan if the new entry is big enough.
681 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100682 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100683 needscan = be16_to_cpu(newdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200684 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 /*
688 * Neither neighbor is free. Make a new entry.
689 */
690 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200691 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100692 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
693 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000694 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200695 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200697 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 *needscanp = needscan;
700}
701
702/*
703 * Take a byte range out of an existing unused space and make it un-free.
704 */
705void
706xfs_dir2_data_use_free(
707 xfs_trans_t *tp, /* transaction pointer */
708 xfs_dabuf_t *bp, /* data block buffer */
709 xfs_dir2_data_unused_t *dup, /* unused entry */
710 xfs_dir2_data_aoff_t offset, /* starting offset to use */
711 xfs_dir2_data_aoff_t len, /* length to use */
712 int *needlogp, /* out: need to log header */
713 int *needscanp) /* out: need regen bestfree */
714{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200715 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
717 int matchback; /* matches end of freespace */
718 int matchfront; /* matches start of freespace */
719 int needscan; /* need to regen bestfree */
720 xfs_dir2_data_unused_t *newdup; /* new unused entry */
721 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
722 int oldlen; /* old unused entry's length */
723
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200724 hdr = bp->data;
725 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
726 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
Nathan Scottad354eb2006-03-17 17:27:37 +1100727 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200728 ASSERT(offset >= (char *)dup - (char *)hdr);
729 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
730 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /*
732 * Look up the entry in the bestfree table.
733 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200734 dfp = xfs_dir2_data_freefind(hdr, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100735 oldlen = be16_to_cpu(dup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200736 ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /*
738 * Check for alignment with front and back of the entry.
739 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200740 matchfront = (char *)dup - (char *)hdr == offset;
741 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 ASSERT(*needscanp == 0);
743 needscan = 0;
744 /*
745 * If we matched it exactly we just need to get rid of it from
746 * the bestfree table.
747 */
748 if (matchfront && matchback) {
749 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200750 needscan = (hdr->bestfree[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200752 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754 }
755 /*
756 * We match the first part of the entry.
757 * Make a new entry with the remaining freespace.
758 */
759 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200760 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100761 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
762 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000763 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200764 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 xfs_dir2_data_log_unused(tp, bp, newdup);
766 /*
767 * If it was in the table, remove it and add the new one.
768 */
769 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200770 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
771 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100773 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200774 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /*
776 * If we got inserted at the last slot,
777 * that means we don't know if there was a better
778 * choice for the last slot, or not. Rescan.
779 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200780 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782 }
783 /*
784 * We match the last part of the entry.
785 * Trim the allocated space off the tail of the entry.
786 */
787 else if (matchback) {
788 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200789 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000790 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200791 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 xfs_dir2_data_log_unused(tp, bp, newdup);
793 /*
794 * If it was in the table, remove it and add the new one.
795 */
796 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200797 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
798 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100800 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200801 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /*
803 * If we got inserted at the last slot,
804 * that means we don't know if there was a better
805 * choice for the last slot, or not. Rescan.
806 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200807 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 }
810 /*
811 * Poking out the middle of an entry.
812 * Make two new entries.
813 */
814 else {
815 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200816 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000817 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200818 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200820 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100821 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
822 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000823 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200824 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 xfs_dir2_data_log_unused(tp, bp, newdup2);
826 /*
827 * If the old entry was in the table, we need to scan
828 * if the 3rd entry was valid, since these entries
829 * are smaller than the old one.
830 * If we don't need to scan that means there were 1 or 2
831 * entries in the table, and removing the old and adding
832 * the 2 new will work.
833 */
834 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200835 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (!needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200837 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
838 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
839 xfs_dir2_data_freeinsert(hdr, newdup2,
840 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842 }
843 }
844 *needscanp = needscan;
845}