blob: cb117234e32ef03c2a45f22e3eb6cb2a6e441214 [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"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dinode.h"
29#include "xfs_inode.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020030#include "xfs_dir2_format.h"
31#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_error.h"
33
Christoph Hellwigc2066e22011-07-08 14:35:38 +020034STATIC xfs_dir2_data_free_t *
35xfs_dir2_data_freefind(xfs_dir2_data_hdr_t *hdr, xfs_dir2_data_unused_t *dup);
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * Check the consistency of the data block.
39 * The input can also be a block-format directory.
Dave Chinner82025d72012-11-12 22:54:12 +110040 * Return 0 is the buffer is good, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
Dave Chinner82025d72012-11-12 22:54:12 +110042int
43__xfs_dir2_data_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +100044 struct xfs_inode *dp, /* incore inode pointer */
45 struct xfs_buf *bp) /* data block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
48 xfs_dir2_data_free_t *bf; /* bestfree table */
49 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
50 int count; /* count of entries found */
Christoph Hellwigc2066e22011-07-08 14:35:38 +020051 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 xfs_dir2_data_entry_t *dep; /* data entry */
53 xfs_dir2_data_free_t *dfp; /* bestfree entry */
54 xfs_dir2_data_unused_t *dup; /* unused entry */
55 char *endp; /* end of useful data */
56 int freeseen; /* mask of bestfrees seen */
57 xfs_dahash_t hash; /* hash of current name */
58 int i; /* leaf index */
59 int lastfree; /* last entry was unused */
60 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
61 xfs_mount_t *mp; /* filesystem mount point */
62 char *p; /* current data position */
63 int stale; /* count of stale leaves */
Barry Naujok5163f952008-05-21 16:41:01 +100064 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Dave Chinner82025d72012-11-12 22:54:12 +110066 mp = bp->b_target->bt_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +100067 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020068 bf = hdr->bestfree;
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +020069 p = (char *)(hdr + 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +020070
Dave Chinner82025d72012-11-12 22:54:12 +110071 switch (hdr->magic) {
72 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020073 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100074 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 endp = (char *)lep;
Dave Chinner82025d72012-11-12 22:54:12 +110076 break;
77 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020078 endp = (char *)hdr + mp->m_dirblksize;
Dave Chinner82025d72012-11-12 22:54:12 +110079 break;
80 default:
81 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
82 return EFSCORRUPTED;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020083 }
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) {
Dave Chinner82025d72012-11-12 22:54:12 +110090 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 freeseen |= 1 << 0;
92 }
93 if (!bf[1].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110094 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 freeseen |= 1 << 1;
96 }
97 if (!bf[2].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110098 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 freeseen |= 1 << 2;
100 }
Dave Chinner82025d72012-11-12 22:54:12 +1100101
102 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
103 be16_to_cpu(bf[1].length));
104 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
105 be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 /*
107 * Loop over the data/unused entries.
108 */
109 while (p < endp) {
110 dup = (xfs_dir2_data_unused_t *)p;
111 /*
112 * If it's unused, look for the space in the bestfree table.
113 * If we find it, account for that, else make sure it
114 * doesn't need to be there.
115 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100116 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Dave Chinner82025d72012-11-12 22:54:12 +1100117 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
118 XFS_WANT_CORRUPTED_RETURN(
119 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
120 (char *)dup - (char *)hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200121 dfp = xfs_dir2_data_freefind(hdr, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (dfp) {
123 i = (int)(dfp - bf);
Dave Chinner82025d72012-11-12 22:54:12 +1100124 XFS_WANT_CORRUPTED_RETURN(
125 (freeseen & (1 << i)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100127 } else {
Dave Chinner82025d72012-11-12 22:54:12 +1100128 XFS_WANT_CORRUPTED_RETURN(
129 be16_to_cpu(dup->length) <=
130 be16_to_cpu(bf[2].length));
Nathan Scott70e73f52006-03-17 17:26:52 +1100131 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100132 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 lastfree = 1;
134 continue;
135 }
136 /*
137 * It's a real entry. Validate the fields.
138 * If this is a block directory then make sure it's
139 * in the leaf section of the block.
140 * The linear search is crude but this is DEBUG code.
141 */
142 dep = (xfs_dir2_data_entry_t *)p;
Dave Chinner82025d72012-11-12 22:54:12 +1100143 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
144 XFS_WANT_CORRUPTED_RETURN(
145 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
146 XFS_WANT_CORRUPTED_RETURN(
147 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
148 (char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 count++;
150 lastfree = 0;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200151 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000152 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 (xfs_dir2_data_aoff_t)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200154 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000155 name.name = dep->name;
156 name.len = dep->namelen;
157 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100158 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100159 if (be32_to_cpu(lep[i].address) == addr &&
160 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 break;
162 }
Dave Chinner82025d72012-11-12 22:54:12 +1100163 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000165 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 /*
168 * Need to have seen all the entries and all the bestfree slots.
169 */
Dave Chinner82025d72012-11-12 22:54:12 +1100170 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200171 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100172 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200173 if (lep[i].address ==
174 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 stale++;
176 if (i > 0)
Dave Chinner82025d72012-11-12 22:54:12 +1100177 XFS_WANT_CORRUPTED_RETURN(
178 be32_to_cpu(lep[i].hashval) >=
179 be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Dave Chinner82025d72012-11-12 22:54:12 +1100181 XFS_WANT_CORRUPTED_RETURN(count ==
182 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
183 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
Dave Chinner82025d72012-11-12 22:54:12 +1100185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/*
189 * Given a data block and an unused entry from that block,
190 * return the bestfree entry if any that corresponds to it.
191 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200192STATIC xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193xfs_dir2_data_freefind(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200194 xfs_dir2_data_hdr_t *hdr, /* data block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 xfs_dir2_data_unused_t *dup) /* data unused entry */
196{
197 xfs_dir2_data_free_t *dfp; /* bestfree entry */
198 xfs_dir2_data_aoff_t off; /* offset value needed */
199#if defined(DEBUG) && defined(__KERNEL__)
200 int matched; /* matched the value */
201 int seenzero; /* saw a 0 bestfree entry */
202#endif
203
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200204 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205#if defined(DEBUG) && defined(__KERNEL__)
206 /*
207 * Validate some consistency in the bestfree table.
208 * Check order, non-overlapping entries, and if we find the
209 * one we're looking for it has to be exact.
210 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200211 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
212 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200213 for (dfp = &hdr->bestfree[0], seenzero = matched = 0;
214 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dfp++) {
216 if (!dfp->offset) {
217 ASSERT(!dfp->length);
218 seenzero = 1;
219 continue;
220 }
221 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100222 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100224 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100225 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100226 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100228 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100229 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200230 if (dfp > &hdr->bestfree[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100231 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233#endif
234 /*
235 * If this is smaller than the smallest bestfree entry,
236 * it can't be there since they're sorted.
237 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100238 if (be16_to_cpu(dup->length) <
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200239 be16_to_cpu(hdr->bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return NULL;
241 /*
242 * Look at the three bestfree entries for our guy.
243 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200244 for (dfp = &hdr->bestfree[0];
245 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 dfp++) {
247 if (!dfp->offset)
248 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100249 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return dfp;
251 }
252 /*
253 * Didn't find it. This only happens if there are duplicate lengths.
254 */
255 return NULL;
256}
257
258/*
259 * Insert an unused-space entry into the bestfree table.
260 */
261xfs_dir2_data_free_t * /* entry inserted */
262xfs_dir2_data_freeinsert(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200263 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 xfs_dir2_data_unused_t *dup, /* unused space */
265 int *loghead) /* log the data header (out) */
266{
267 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
268 xfs_dir2_data_free_t new; /* new bestfree entry */
269
270#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200271 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
272 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#endif
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200274 dfp = hdr->bestfree;
Nathan Scott70e73f52006-03-17 17:26:52 +1100275 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200276 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /*
279 * Insert at position 0, 1, or 2; or not at all.
280 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100281 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 dfp[2] = dfp[1];
283 dfp[1] = dfp[0];
284 dfp[0] = new;
285 *loghead = 1;
286 return &dfp[0];
287 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100288 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 dfp[2] = dfp[1];
290 dfp[1] = new;
291 *loghead = 1;
292 return &dfp[1];
293 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100294 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 dfp[2] = new;
296 *loghead = 1;
297 return &dfp[2];
298 }
299 return NULL;
300}
301
302/*
303 * Remove a bestfree entry from the table.
304 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000305STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306xfs_dir2_data_freeremove(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200307 xfs_dir2_data_hdr_t *hdr, /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
309 int *loghead) /* out: log data header */
310{
311#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200312 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
313 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#endif
315 /*
316 * It's the first entry, slide the next 2 up.
317 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200318 if (dfp == &hdr->bestfree[0]) {
319 hdr->bestfree[0] = hdr->bestfree[1];
320 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 /*
323 * It's the second entry, slide the 3rd entry up.
324 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200325 else if (dfp == &hdr->bestfree[1])
326 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /*
328 * Must be the last entry.
329 */
330 else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200331 ASSERT(dfp == &hdr->bestfree[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /*
333 * Clear the 3rd entry, must be zero now.
334 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200335 hdr->bestfree[2].length = 0;
336 hdr->bestfree[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 *loghead = 1;
338}
339
340/*
341 * Given a data block, reconstruct its bestfree map.
342 */
343void
344xfs_dir2_data_freescan(
345 xfs_mount_t *mp, /* filesystem mount point */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200346 xfs_dir2_data_hdr_t *hdr, /* data block header */
Eric Sandeenef497f82007-05-08 13:48:49 +1000347 int *loghead) /* out: log data header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 xfs_dir2_block_tail_t *btp; /* block tail */
350 xfs_dir2_data_entry_t *dep; /* active data entry */
351 xfs_dir2_data_unused_t *dup; /* unused data entry */
352 char *endp; /* end of block's data */
353 char *p; /* current entry pointer */
354
355#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200356 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
357 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#endif
359 /*
360 * Start by clearing the table.
361 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200362 memset(hdr->bestfree, 0, sizeof(hdr->bestfree));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *loghead = 1;
364 /*
365 * Set up pointers.
366 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +0200367 p = (char *)(hdr + 1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200368 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200369 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000370 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 } else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200372 endp = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /*
374 * Loop over the block's entries.
375 */
376 while (p < endp) {
377 dup = (xfs_dir2_data_unused_t *)p;
378 /*
379 * If it's a free entry, insert it.
380 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100381 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200382 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000383 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200384 xfs_dir2_data_freeinsert(hdr, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100385 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 /*
388 * For active entries, check their tags and skip them.
389 */
390 else {
391 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200392 ASSERT((char *)dep - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000393 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
394 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 }
397}
398
399/*
400 * Initialize a data block at the given block number in the directory.
401 * Give back the buffer for the created block.
402 */
403int /* error */
404xfs_dir2_data_init(
405 xfs_da_args_t *args, /* directory operation args */
406 xfs_dir2_db_t blkno, /* logical dir block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000407 struct xfs_buf **bpp) /* output block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000409 struct xfs_buf *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200410 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 xfs_inode_t *dp; /* incore directory inode */
412 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
413 int error; /* error return value */
414 int i; /* bestfree index */
415 xfs_mount_t *mp; /* filesystem mount point */
416 xfs_trans_t *tp; /* transaction pointer */
417 int t; /* temp */
418
419 dp = args->dp;
420 mp = dp->i_mount;
421 tp = args->trans;
422 /*
423 * Get the buffer set up for the block.
424 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000425 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 XFS_DATA_FORK);
427 if (error) {
428 return error;
429 }
430 ASSERT(bp != NULL);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /*
433 * Initialize the header.
434 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000435 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200436 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
437 hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200439 hdr->bestfree[i].length = 0;
440 hdr->bestfree[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
444 * Set up an unused entry for the block's body.
445 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +0200446 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
Nathan Scottad354eb2006-03-17 17:27:37 +1100447 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200449 t = mp->m_dirblksize - (uint)sizeof(*hdr);
450 hdr->bestfree[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100451 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200452 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /*
454 * Log it and return it.
455 */
456 xfs_dir2_data_log_header(tp, bp);
457 xfs_dir2_data_log_unused(tp, bp, dup);
458 *bpp = bp;
459 return 0;
460}
461
462/*
463 * Log an active data entry from the block.
464 */
465void
466xfs_dir2_data_log_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000467 struct xfs_trans *tp,
468 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 xfs_dir2_data_entry_t *dep) /* data entry pointer */
470{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000471 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200473 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
474 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200475
Dave Chinner1d9025e2012-06-22 18:50:14 +1000476 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000477 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200478 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481/*
482 * Log a data block header.
483 */
484void
485xfs_dir2_data_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000486 struct xfs_trans *tp,
487 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000489 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200491 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
492 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200493
Dave Chinner1d9025e2012-06-22 18:50:14 +1000494 xfs_trans_log_buf(tp, bp, 0, sizeof(*hdr) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/*
498 * Log a data unused entry.
499 */
500void
501xfs_dir2_data_log_unused(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000502 struct xfs_trans *tp,
503 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 xfs_dir2_data_unused_t *dup) /* data unused pointer */
505{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000506 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200508 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
509 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /*
512 * Log the first part of the unused entry.
513 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000514 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200516 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * Log the end (tag) of the unused entry.
519 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000520 xfs_trans_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200521 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
522 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 sizeof(xfs_dir2_data_off_t) - 1));
524}
525
526/*
527 * Make a byte range in the data block unused.
528 * Its current contents are unimportant.
529 */
530void
531xfs_dir2_data_make_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000532 struct xfs_trans *tp,
533 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 xfs_dir2_data_aoff_t offset, /* starting byte offset */
535 xfs_dir2_data_aoff_t len, /* length in bytes */
536 int *needlogp, /* out: log header */
537 int *needscanp) /* out: regen bestfree */
538{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200539 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
541 char *endptr; /* end of data area */
542 xfs_mount_t *mp; /* filesystem mount point */
543 int needscan; /* need to regen bestfree */
544 xfs_dir2_data_unused_t *newdup; /* new unused entry */
545 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
546 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
547
548 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000549 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /*
552 * Figure out where the end of the data area is.
553 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200554 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC))
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200555 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 else {
557 xfs_dir2_block_tail_t *btp; /* block tail */
558
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200559 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200560 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000561 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 /*
564 * If this isn't the start of the block, then back up to
565 * the previous entry and see if it's free.
566 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200567 if (offset > sizeof(*hdr)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100568 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200570 tagp = (__be16 *)((char *)hdr + offset) - 1;
571 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100572 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 prevdup = NULL;
574 } else
575 prevdup = NULL;
576 /*
577 * If this isn't the end of the block, see if the entry after
578 * us is free.
579 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200580 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200582 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100583 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 postdup = NULL;
585 } else
586 postdup = NULL;
587 ASSERT(*needscanp == 0);
588 needscan = 0;
589 /*
590 * Previous and following entries are both free,
591 * merge everything into a single free entry.
592 */
593 if (prevdup && postdup) {
594 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
595
596 /*
597 * See if prevdup and/or postdup are in bestfree table.
598 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200599 dfp = xfs_dir2_data_freefind(hdr, prevdup);
600 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /*
602 * We need a rescan unless there are exactly 2 free entries
603 * namely our two. Then we know what's happening, otherwise
604 * since the third bestfree is there, there might be more
605 * entries.
606 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200607 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * Fix up the new big freespace.
610 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800611 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000612 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200613 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 xfs_dir2_data_log_unused(tp, bp, prevdup);
615 if (!needscan) {
616 /*
617 * Has to be the case that entries 0 and 1 are
618 * dfp and dfp2 (don't know which is which), and
619 * entry 2 is empty.
620 * Remove entry 1 first then entry 0.
621 */
622 ASSERT(dfp && dfp2);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200623 if (dfp == &hdr->bestfree[1]) {
624 dfp = &hdr->bestfree[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ASSERT(dfp2 == dfp);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200626 dfp2 = &hdr->bestfree[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200628 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
629 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
631 * Now insert the new entry.
632 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200633 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
634 ASSERT(dfp == &hdr->bestfree[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100635 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 ASSERT(!dfp[1].length);
637 ASSERT(!dfp[2].length);
638 }
639 }
640 /*
641 * The entry before us is free, merge with it.
642 */
643 else if (prevdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200644 dfp = xfs_dir2_data_freefind(hdr, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800645 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000646 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200647 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 xfs_dir2_data_log_unused(tp, bp, prevdup);
649 /*
650 * If the previous entry was in the table, the new entry
651 * is longer, so it will be in the table too. Remove
652 * the old one and add the new one.
653 */
654 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200655 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
656 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 /*
659 * Otherwise we need a scan if the new entry is big enough.
660 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100661 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100662 needscan = be16_to_cpu(prevdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200663 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 /*
667 * The following entry is free, merge with it.
668 */
669 else if (postdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200670 dfp = xfs_dir2_data_freefind(hdr, postdup);
671 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100672 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
673 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000674 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200675 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 xfs_dir2_data_log_unused(tp, bp, newdup);
677 /*
678 * If the following entry was in the table, the new entry
679 * is longer, so it will be in the table too. Remove
680 * the old one and add the new one.
681 */
682 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200683 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
684 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686 /*
687 * Otherwise we need a scan if the new entry is big enough.
688 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100689 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100690 needscan = be16_to_cpu(newdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200691 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 /*
695 * Neither neighbor is free. Make a new entry.
696 */
697 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200698 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100699 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
700 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000701 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200702 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200704 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 *needscanp = needscan;
707}
708
709/*
710 * Take a byte range out of an existing unused space and make it un-free.
711 */
712void
713xfs_dir2_data_use_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000714 struct xfs_trans *tp,
715 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 xfs_dir2_data_unused_t *dup, /* unused entry */
717 xfs_dir2_data_aoff_t offset, /* starting offset to use */
718 xfs_dir2_data_aoff_t len, /* length to use */
719 int *needlogp, /* out: need to log header */
720 int *needscanp) /* out: need regen bestfree */
721{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200722 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
724 int matchback; /* matches end of freespace */
725 int matchfront; /* matches start of freespace */
726 int needscan; /* need to regen bestfree */
727 xfs_dir2_data_unused_t *newdup; /* new unused entry */
728 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
729 int oldlen; /* old unused entry's length */
730
Dave Chinner1d9025e2012-06-22 18:50:14 +1000731 hdr = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200732 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
733 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Nathan Scottad354eb2006-03-17 17:27:37 +1100734 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200735 ASSERT(offset >= (char *)dup - (char *)hdr);
736 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
737 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /*
739 * Look up the entry in the bestfree table.
740 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200741 dfp = xfs_dir2_data_freefind(hdr, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100742 oldlen = be16_to_cpu(dup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200743 ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /*
745 * Check for alignment with front and back of the entry.
746 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200747 matchfront = (char *)dup - (char *)hdr == offset;
748 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 ASSERT(*needscanp == 0);
750 needscan = 0;
751 /*
752 * If we matched it exactly we just need to get rid of it from
753 * the bestfree table.
754 */
755 if (matchfront && matchback) {
756 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200757 needscan = (hdr->bestfree[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (!needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200759 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 }
762 /*
763 * We match the first part of the entry.
764 * Make a new entry with the remaining freespace.
765 */
766 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200767 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100768 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
769 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000770 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200771 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 xfs_dir2_data_log_unused(tp, bp, newdup);
773 /*
774 * If it was in the table, remove it and add the new one.
775 */
776 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200777 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
778 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100780 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200781 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /*
783 * If we got inserted at the last slot,
784 * that means we don't know if there was a better
785 * choice for the last slot, or not. Rescan.
786 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200787 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789 }
790 /*
791 * We match the last part of the entry.
792 * Trim the allocated space off the tail of the entry.
793 */
794 else if (matchback) {
795 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200796 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000797 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200798 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 xfs_dir2_data_log_unused(tp, bp, newdup);
800 /*
801 * If it was in the table, remove it and add the new one.
802 */
803 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200804 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
805 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100807 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200808 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /*
810 * If we got inserted at the last slot,
811 * that means we don't know if there was a better
812 * choice for the last slot, or not. Rescan.
813 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200814 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 }
817 /*
818 * Poking out the middle of an entry.
819 * Make two new entries.
820 */
821 else {
822 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200823 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000824 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200825 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200827 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100828 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
829 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000830 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200831 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 xfs_dir2_data_log_unused(tp, bp, newdup2);
833 /*
834 * If the old entry was in the table, we need to scan
835 * if the 3rd entry was valid, since these entries
836 * are smaller than the old one.
837 * If we don't need to scan that means there were 1 or 2
838 * entries in the table, and removing the old and adding
839 * the 2 new will work.
840 */
841 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200842 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (!needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200844 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
845 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
846 xfs_dir2_data_freeinsert(hdr, newdup2,
847 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849 }
850 }
851 *needscanp = needscan;
852}