blob: 8e47ac3a3b9ddc26dcc92f1a0c2dcceebff44060 [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 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +020056 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 xfs_dir2_data_entry_t *dep; /* data entry */
58 xfs_dir2_data_free_t *dfp; /* bestfree entry */
59 xfs_dir2_data_unused_t *dup; /* unused entry */
60 char *endp; /* end of useful data */
61 int freeseen; /* mask of bestfrees seen */
62 xfs_dahash_t hash; /* hash of current name */
63 int i; /* leaf index */
64 int lastfree; /* last entry was unused */
65 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
66 xfs_mount_t *mp; /* filesystem mount point */
67 char *p; /* current data position */
68 int stale; /* count of stale leaves */
Barry Naujok5163f952008-05-21 16:41:01 +100069 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 mp = dp->i_mount;
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +020072 hdr = bp->data;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020073 bf = hdr->bestfree;
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +020074 p = (char *)(hdr + 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +020075
76 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
77 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100078 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 endp = (char *)lep;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020080 } else {
81 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
82 endp = (char *)hdr + mp->m_dirblksize;
83 }
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 count = lastfree = freeseen = 0;
86 /*
87 * Account for zero bestfree entries.
88 */
89 if (!bf[0].length) {
90 ASSERT(!bf[0].offset);
91 freeseen |= 1 << 0;
92 }
93 if (!bf[1].length) {
94 ASSERT(!bf[1].offset);
95 freeseen |= 1 << 1;
96 }
97 if (!bf[2].length) {
98 ASSERT(!bf[2].offset);
99 freeseen |= 1 << 2;
100 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100101 ASSERT(be16_to_cpu(bf[0].length) >= be16_to_cpu(bf[1].length));
102 ASSERT(be16_to_cpu(bf[1].length) >= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /*
104 * Loop over the data/unused entries.
105 */
106 while (p < endp) {
107 dup = (xfs_dir2_data_unused_t *)p;
108 /*
109 * If it's unused, look for the space in the bestfree table.
110 * If we find it, account for that, else make sure it
111 * doesn't need to be there.
112 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100113 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 ASSERT(lastfree == 0);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000115 ASSERT(be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200116 (char *)dup - (char *)hdr);
117 dfp = xfs_dir2_data_freefind(hdr, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (dfp) {
119 i = (int)(dfp - bf);
120 ASSERT((freeseen & (1 << i)) == 0);
121 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100122 } else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100123 ASSERT(be16_to_cpu(dup->length) <=
Nathan Scott70e73f52006-03-17 17:26:52 +1100124 be16_to_cpu(bf[2].length));
125 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100126 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 lastfree = 1;
128 continue;
129 }
130 /*
131 * It's a real entry. Validate the fields.
132 * If this is a block directory then make sure it's
133 * in the leaf section of the block.
134 * The linear search is crude but this is DEBUG code.
135 */
136 dep = (xfs_dir2_data_entry_t *)p;
137 ASSERT(dep->namelen != 0);
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000138 ASSERT(xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)) == 0);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000139 ASSERT(be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200140 (char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 count++;
142 lastfree = 0;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200143 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000144 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 (xfs_dir2_data_aoff_t)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200146 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000147 name.name = dep->name;
148 name.len = dep->namelen;
149 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100150 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100151 if (be32_to_cpu(lep[i].address) == addr &&
152 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
154 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100155 ASSERT(i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000157 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159 /*
160 * Need to have seen all the entries and all the bestfree slots.
161 */
162 ASSERT(freeseen == 7);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200163 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100164 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100165 if (be32_to_cpu(lep[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 stale++;
167 if (i > 0)
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100168 ASSERT(be32_to_cpu(lep[i].hashval) >= be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100170 ASSERT(count == be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
171 ASSERT(stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173}
174#endif
175
176/*
177 * Given a data block and an unused entry from that block,
178 * return the bestfree entry if any that corresponds to it.
179 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200180STATIC xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181xfs_dir2_data_freefind(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200182 xfs_dir2_data_hdr_t *hdr, /* data block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 xfs_dir2_data_unused_t *dup) /* data unused entry */
184{
185 xfs_dir2_data_free_t *dfp; /* bestfree entry */
186 xfs_dir2_data_aoff_t off; /* offset value needed */
187#if defined(DEBUG) && defined(__KERNEL__)
188 int matched; /* matched the value */
189 int seenzero; /* saw a 0 bestfree entry */
190#endif
191
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200192 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#if defined(DEBUG) && defined(__KERNEL__)
194 /*
195 * Validate some consistency in the bestfree table.
196 * Check order, non-overlapping entries, and if we find the
197 * one we're looking for it has to be exact.
198 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200199 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
200 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
201 for (dfp = &hdr->bestfree[0], seenzero = matched = 0;
202 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 dfp++) {
204 if (!dfp->offset) {
205 ASSERT(!dfp->length);
206 seenzero = 1;
207 continue;
208 }
209 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100210 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100212 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100213 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100214 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100216 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100217 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200218 if (dfp > &hdr->bestfree[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100219 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221#endif
222 /*
223 * If this is smaller than the smallest bestfree entry,
224 * it can't be there since they're sorted.
225 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100226 if (be16_to_cpu(dup->length) <
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200227 be16_to_cpu(hdr->bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return NULL;
229 /*
230 * Look at the three bestfree entries for our guy.
231 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200232 for (dfp = &hdr->bestfree[0];
233 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 dfp++) {
235 if (!dfp->offset)
236 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100237 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return dfp;
239 }
240 /*
241 * Didn't find it. This only happens if there are duplicate lengths.
242 */
243 return NULL;
244}
245
246/*
247 * Insert an unused-space entry into the bestfree table.
248 */
249xfs_dir2_data_free_t * /* entry inserted */
250xfs_dir2_data_freeinsert(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200251 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 xfs_dir2_data_unused_t *dup, /* unused space */
253 int *loghead) /* log the data header (out) */
254{
255 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
256 xfs_dir2_data_free_t new; /* new bestfree entry */
257
258#ifdef __KERNEL__
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200259 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
260 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261#endif
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200262 dfp = hdr->bestfree;
Nathan Scott70e73f52006-03-17 17:26:52 +1100263 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200264 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /*
267 * Insert at position 0, 1, or 2; or not at all.
268 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100269 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dfp[2] = dfp[1];
271 dfp[1] = dfp[0];
272 dfp[0] = new;
273 *loghead = 1;
274 return &dfp[0];
275 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100276 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dfp[2] = dfp[1];
278 dfp[1] = new;
279 *loghead = 1;
280 return &dfp[1];
281 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100282 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 dfp[2] = new;
284 *loghead = 1;
285 return &dfp[2];
286 }
287 return NULL;
288}
289
290/*
291 * Remove a bestfree entry from the table.
292 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000293STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294xfs_dir2_data_freeremove(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200295 xfs_dir2_data_hdr_t *hdr, /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
297 int *loghead) /* out: log data header */
298{
299#ifdef __KERNEL__
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200300 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
301 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#endif
303 /*
304 * It's the first entry, slide the next 2 up.
305 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200306 if (dfp == &hdr->bestfree[0]) {
307 hdr->bestfree[0] = hdr->bestfree[1];
308 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 /*
311 * It's the second entry, slide the 3rd entry up.
312 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200313 else if (dfp == &hdr->bestfree[1])
314 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
316 * Must be the last entry.
317 */
318 else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200319 ASSERT(dfp == &hdr->bestfree[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /*
321 * Clear the 3rd entry, must be zero now.
322 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200323 hdr->bestfree[2].length = 0;
324 hdr->bestfree[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 *loghead = 1;
326}
327
328/*
329 * Given a data block, reconstruct its bestfree map.
330 */
331void
332xfs_dir2_data_freescan(
333 xfs_mount_t *mp, /* filesystem mount point */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200334 xfs_dir2_data_hdr_t *hdr, /* data block header */
Eric Sandeenef497f82007-05-08 13:48:49 +1000335 int *loghead) /* out: log data header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 xfs_dir2_block_tail_t *btp; /* block tail */
338 xfs_dir2_data_entry_t *dep; /* active data entry */
339 xfs_dir2_data_unused_t *dup; /* unused data entry */
340 char *endp; /* end of block's data */
341 char *p; /* current entry pointer */
342
343#ifdef __KERNEL__
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200344 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
345 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#endif
347 /*
348 * Start by clearing the table.
349 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200350 memset(hdr->bestfree, 0, sizeof(hdr->bestfree));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *loghead = 1;
352 /*
353 * Set up pointers.
354 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +0200355 p = (char *)(hdr + 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200356 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
357 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000358 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 } else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200360 endp = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /*
362 * Loop over the block's entries.
363 */
364 while (p < endp) {
365 dup = (xfs_dir2_data_unused_t *)p;
366 /*
367 * If it's a free entry, insert it.
368 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100369 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200370 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000371 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200372 xfs_dir2_data_freeinsert(hdr, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100373 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 /*
376 * For active entries, check their tags and skip them.
377 */
378 else {
379 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200380 ASSERT((char *)dep - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000381 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
382 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 }
385}
386
387/*
388 * Initialize a data block at the given block number in the directory.
389 * Give back the buffer for the created block.
390 */
391int /* error */
392xfs_dir2_data_init(
393 xfs_da_args_t *args, /* directory operation args */
394 xfs_dir2_db_t blkno, /* logical dir block number */
395 xfs_dabuf_t **bpp) /* output block buffer */
396{
397 xfs_dabuf_t *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200398 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 xfs_inode_t *dp; /* incore directory inode */
400 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
401 int error; /* error return value */
402 int i; /* bestfree index */
403 xfs_mount_t *mp; /* filesystem mount point */
404 xfs_trans_t *tp; /* transaction pointer */
405 int t; /* temp */
406
407 dp = args->dp;
408 mp = dp->i_mount;
409 tp = args->trans;
410 /*
411 * Get the buffer set up for the block.
412 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000413 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 XFS_DATA_FORK);
415 if (error) {
416 return error;
417 }
418 ASSERT(bp != NULL);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /*
421 * Initialize the header.
422 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +0200423 hdr = bp->data;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200424 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
425 hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200427 hdr->bestfree[i].length = 0;
428 hdr->bestfree[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
432 * Set up an unused entry for the block's body.
433 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +0200434 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
Nathan Scottad354eb2006-03-17 17:27:37 +1100435 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200437 t = mp->m_dirblksize - (uint)sizeof(*hdr);
438 hdr->bestfree[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100439 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200440 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /*
442 * Log it and return it.
443 */
444 xfs_dir2_data_log_header(tp, bp);
445 xfs_dir2_data_log_unused(tp, bp, dup);
446 *bpp = bp;
447 return 0;
448}
449
450/*
451 * Log an active data entry from the block.
452 */
453void
454xfs_dir2_data_log_entry(
455 xfs_trans_t *tp, /* transaction pointer */
456 xfs_dabuf_t *bp, /* block buffer */
457 xfs_dir2_data_entry_t *dep) /* data entry pointer */
458{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200459 xfs_dir2_data_hdr_t *hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200461 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
462 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
463
464 xfs_da_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000465 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200466 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/*
470 * Log a data block header.
471 */
472void
473xfs_dir2_data_log_header(
474 xfs_trans_t *tp, /* transaction pointer */
475 xfs_dabuf_t *bp) /* block buffer */
476{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200477 xfs_dir2_data_hdr_t *hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200479 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
480 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
481
482 xfs_da_log_buf(tp, bp, 0, sizeof(*hdr) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485/*
486 * Log a data unused entry.
487 */
488void
489xfs_dir2_data_log_unused(
490 xfs_trans_t *tp, /* transaction pointer */
491 xfs_dabuf_t *bp, /* block buffer */
492 xfs_dir2_data_unused_t *dup) /* data unused pointer */
493{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200494 xfs_dir2_data_hdr_t *hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200496 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
497 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /*
500 * Log the first part of the unused entry.
501 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200502 xfs_da_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200504 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /*
506 * Log the end (tag) of the unused entry.
507 */
508 xfs_da_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200509 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
510 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 sizeof(xfs_dir2_data_off_t) - 1));
512}
513
514/*
515 * Make a byte range in the data block unused.
516 * Its current contents are unimportant.
517 */
518void
519xfs_dir2_data_make_free(
520 xfs_trans_t *tp, /* transaction pointer */
521 xfs_dabuf_t *bp, /* block buffer */
522 xfs_dir2_data_aoff_t offset, /* starting byte offset */
523 xfs_dir2_data_aoff_t len, /* length in bytes */
524 int *needlogp, /* out: log header */
525 int *needscanp) /* out: regen bestfree */
526{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200527 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
529 char *endptr; /* end of data area */
530 xfs_mount_t *mp; /* filesystem mount point */
531 int needscan; /* need to regen bestfree */
532 xfs_dir2_data_unused_t *newdup; /* new unused entry */
533 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
534 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
535
536 mp = tp->t_mountp;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200537 hdr = bp->data;
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /*
540 * Figure out where the end of the data area is.
541 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200542 if (be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC)
543 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 else {
545 xfs_dir2_block_tail_t *btp; /* block tail */
546
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200547 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
548 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000549 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 /*
552 * If this isn't the start of the block, then back up to
553 * the previous entry and see if it's free.
554 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200555 if (offset > sizeof(*hdr)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100556 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200558 tagp = (__be16 *)((char *)hdr + offset) - 1;
559 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100560 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 prevdup = NULL;
562 } else
563 prevdup = NULL;
564 /*
565 * If this isn't the end of the block, see if the entry after
566 * us is free.
567 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200568 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200570 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100571 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 postdup = NULL;
573 } else
574 postdup = NULL;
575 ASSERT(*needscanp == 0);
576 needscan = 0;
577 /*
578 * Previous and following entries are both free,
579 * merge everything into a single free entry.
580 */
581 if (prevdup && postdup) {
582 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
583
584 /*
585 * See if prevdup and/or postdup are in bestfree table.
586 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200587 dfp = xfs_dir2_data_freefind(hdr, prevdup);
588 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /*
590 * We need a rescan unless there are exactly 2 free entries
591 * namely our two. Then we know what's happening, otherwise
592 * since the third bestfree is there, there might be more
593 * entries.
594 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200595 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /*
597 * Fix up the new big freespace.
598 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800599 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000600 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200601 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 xfs_dir2_data_log_unused(tp, bp, prevdup);
603 if (!needscan) {
604 /*
605 * Has to be the case that entries 0 and 1 are
606 * dfp and dfp2 (don't know which is which), and
607 * entry 2 is empty.
608 * Remove entry 1 first then entry 0.
609 */
610 ASSERT(dfp && dfp2);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200611 if (dfp == &hdr->bestfree[1]) {
612 dfp = &hdr->bestfree[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 ASSERT(dfp2 == dfp);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200614 dfp2 = &hdr->bestfree[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200616 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
617 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /*
619 * Now insert the new entry.
620 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200621 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
622 ASSERT(dfp == &hdr->bestfree[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100623 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 ASSERT(!dfp[1].length);
625 ASSERT(!dfp[2].length);
626 }
627 }
628 /*
629 * The entry before us is free, merge with it.
630 */
631 else if (prevdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200632 dfp = xfs_dir2_data_freefind(hdr, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800633 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000634 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200635 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 xfs_dir2_data_log_unused(tp, bp, prevdup);
637 /*
638 * If the previous entry was in the table, the new entry
639 * is longer, so it will be in the table too. Remove
640 * the old one and add the new one.
641 */
642 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200643 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
644 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 /*
647 * Otherwise we need a scan if the new entry is big enough.
648 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100649 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100650 needscan = be16_to_cpu(prevdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200651 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 /*
655 * The following entry is free, merge with it.
656 */
657 else if (postdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200658 dfp = xfs_dir2_data_freefind(hdr, postdup);
659 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100660 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
661 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000662 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200663 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 xfs_dir2_data_log_unused(tp, bp, newdup);
665 /*
666 * If the following entry was in the table, the new entry
667 * is longer, so it will be in the table too. Remove
668 * the old one and add the new one.
669 */
670 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200671 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
672 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674 /*
675 * Otherwise we need a scan if the new entry is big enough.
676 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100677 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100678 needscan = be16_to_cpu(newdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200679 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 /*
683 * Neither neighbor is free. Make a new entry.
684 */
685 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200686 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100687 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
688 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000689 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200690 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200692 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 *needscanp = needscan;
695}
696
697/*
698 * Take a byte range out of an existing unused space and make it un-free.
699 */
700void
701xfs_dir2_data_use_free(
702 xfs_trans_t *tp, /* transaction pointer */
703 xfs_dabuf_t *bp, /* data block buffer */
704 xfs_dir2_data_unused_t *dup, /* unused entry */
705 xfs_dir2_data_aoff_t offset, /* starting offset to use */
706 xfs_dir2_data_aoff_t len, /* length to use */
707 int *needlogp, /* out: need to log header */
708 int *needscanp) /* out: need regen bestfree */
709{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200710 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
712 int matchback; /* matches end of freespace */
713 int matchfront; /* matches start of freespace */
714 int needscan; /* need to regen bestfree */
715 xfs_dir2_data_unused_t *newdup; /* new unused entry */
716 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
717 int oldlen; /* old unused entry's length */
718
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200719 hdr = bp->data;
720 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
721 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
Nathan Scottad354eb2006-03-17 17:27:37 +1100722 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200723 ASSERT(offset >= (char *)dup - (char *)hdr);
724 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
725 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 * Look up the entry in the bestfree table.
728 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200729 dfp = xfs_dir2_data_freefind(hdr, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100730 oldlen = be16_to_cpu(dup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200731 ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /*
733 * Check for alignment with front and back of the entry.
734 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200735 matchfront = (char *)dup - (char *)hdr == offset;
736 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 ASSERT(*needscanp == 0);
738 needscan = 0;
739 /*
740 * If we matched it exactly we just need to get rid of it from
741 * the bestfree table.
742 */
743 if (matchfront && matchback) {
744 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200745 needscan = (hdr->bestfree[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200747 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749 }
750 /*
751 * We match the first part of the entry.
752 * Make a new entry with the remaining freespace.
753 */
754 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200755 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100756 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
757 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000758 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200759 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 xfs_dir2_data_log_unused(tp, bp, newdup);
761 /*
762 * If it was in the table, remove it and add the new one.
763 */
764 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200765 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
766 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100768 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200769 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /*
771 * If we got inserted at the last slot,
772 * that means we don't know if there was a better
773 * choice for the last slot, or not. Rescan.
774 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200775 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 }
778 /*
779 * We match the last part of the entry.
780 * Trim the allocated space off the tail of the entry.
781 */
782 else if (matchback) {
783 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200784 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000785 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200786 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 xfs_dir2_data_log_unused(tp, bp, newdup);
788 /*
789 * If it was in the table, remove it and add the new one.
790 */
791 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200792 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
793 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100795 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200796 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /*
798 * If we got inserted at the last slot,
799 * that means we don't know if there was a better
800 * choice for the last slot, or not. Rescan.
801 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200802 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 }
805 /*
806 * Poking out the middle of an entry.
807 * Make two new entries.
808 */
809 else {
810 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200811 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000812 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200813 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200815 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100816 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
817 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000818 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200819 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 xfs_dir2_data_log_unused(tp, bp, newdup2);
821 /*
822 * If the old entry was in the table, we need to scan
823 * if the 3rd entry was valid, since these entries
824 * are smaller than the old one.
825 * If we don't need to scan that means there were 1 or 2
826 * entries in the table, and removing the old and adding
827 * the 2 new will work.
828 */
829 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200830 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (!needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200832 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
833 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
834 xfs_dir2_data_freeinsert(hdr, newdup2,
835 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 }
838 }
839 *needscanp = needscan;
840}