blob: 47e1326c169a08c71d8d21ac51e8d113444d3295 [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.
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
23#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_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dinode.h"
30#include "xfs_inode.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020031#include "xfs_dir2_format.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100032#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020033#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_error.h"
Dave Chinner33363fe2013-04-03 16:11:22 +110035#include "xfs_buf_item.h"
36#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * Check the consistency of the data block.
40 * The input can also be a block-format directory.
Dave Chinner82025d72012-11-12 22:54:12 +110041 * Return 0 is the buffer is good, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
Dave Chinner82025d72012-11-12 22:54:12 +110043int
Dave Chinner33363fe2013-04-03 16:11:22 +110044__xfs_dir3_data_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +100045 struct xfs_inode *dp, /* incore inode pointer */
46 struct xfs_buf *bp) /* data block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
49 xfs_dir2_data_free_t *bf; /* bestfree table */
50 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
51 int count; /* count of entries found */
Christoph Hellwigc2066e22011-07-08 14:35:38 +020052 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 xfs_dir2_data_entry_t *dep; /* data entry */
54 xfs_dir2_data_free_t *dfp; /* bestfree entry */
55 xfs_dir2_data_unused_t *dup; /* unused entry */
56 char *endp; /* end of useful data */
57 int freeseen; /* mask of bestfrees seen */
58 xfs_dahash_t hash; /* hash of current name */
59 int i; /* leaf index */
60 int lastfree; /* last entry was unused */
61 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
62 xfs_mount_t *mp; /* filesystem mount point */
63 char *p; /* current data position */
64 int stale; /* count of stale leaves */
Barry Naujok5163f952008-05-21 16:41:01 +100065 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Dave Chinner82025d72012-11-12 22:54:12 +110067 mp = bp->b_target->bt_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +100068 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110069 bf = xfs_dir3_data_bestfree_p(hdr);
70 p = (char *)xfs_dir3_data_entry_p(hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +020071
Dave Chinner82025d72012-11-12 22:54:12 +110072 switch (hdr->magic) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110073 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110074 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020075 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100076 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 endp = (char *)lep;
Dave Chinner82025d72012-11-12 22:54:12 +110078 break;
Dave Chinner33363fe2013-04-03 16:11:22 +110079 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110080 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020081 endp = (char *)hdr + mp->m_dirblksize;
Dave Chinner82025d72012-11-12 22:54:12 +110082 break;
83 default:
84 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
85 return EFSCORRUPTED;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020086 }
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 count = lastfree = freeseen = 0;
89 /*
90 * Account for zero bestfree entries.
91 */
92 if (!bf[0].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110093 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 freeseen |= 1 << 0;
95 }
96 if (!bf[1].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110097 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 freeseen |= 1 << 1;
99 }
100 if (!bf[2].length) {
Dave Chinner82025d72012-11-12 22:54:12 +1100101 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 freeseen |= 1 << 2;
103 }
Dave Chinner82025d72012-11-12 22:54:12 +1100104
105 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
106 be16_to_cpu(bf[1].length));
107 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
108 be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /*
110 * Loop over the data/unused entries.
111 */
112 while (p < endp) {
113 dup = (xfs_dir2_data_unused_t *)p;
114 /*
115 * If it's unused, look for the space in the bestfree table.
116 * If we find it, account for that, else make sure it
117 * doesn't need to be there.
118 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100119 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Dave Chinner82025d72012-11-12 22:54:12 +1100120 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
121 XFS_WANT_CORRUPTED_RETURN(
122 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
123 (char *)dup - (char *)hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200124 dfp = xfs_dir2_data_freefind(hdr, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (dfp) {
126 i = (int)(dfp - bf);
Dave Chinner82025d72012-11-12 22:54:12 +1100127 XFS_WANT_CORRUPTED_RETURN(
128 (freeseen & (1 << i)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100130 } else {
Dave Chinner82025d72012-11-12 22:54:12 +1100131 XFS_WANT_CORRUPTED_RETURN(
132 be16_to_cpu(dup->length) <=
133 be16_to_cpu(bf[2].length));
Nathan Scott70e73f52006-03-17 17:26:52 +1100134 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100135 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 lastfree = 1;
137 continue;
138 }
139 /*
140 * It's a real entry. Validate the fields.
141 * If this is a block directory then make sure it's
142 * in the leaf section of the block.
143 * The linear search is crude but this is DEBUG code.
144 */
145 dep = (xfs_dir2_data_entry_t *)p;
Dave Chinner82025d72012-11-12 22:54:12 +1100146 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
147 XFS_WANT_CORRUPTED_RETURN(
148 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
149 XFS_WANT_CORRUPTED_RETURN(
Dave Chinner0cb97762013-08-12 20:50:09 +1000150 be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep)) ==
Dave Chinner82025d72012-11-12 22:54:12 +1100151 (char *)dep - (char *)hdr);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000152 XFS_WANT_CORRUPTED_RETURN(
153 xfs_dir3_dirent_get_ftype(mp, dep) < XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 count++;
155 lastfree = 0;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100156 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
157 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000158 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 (xfs_dir2_data_aoff_t)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200160 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000161 name.name = dep->name;
162 name.len = dep->namelen;
163 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100164 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100165 if (be32_to_cpu(lep[i].address) == addr &&
166 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168 }
Dave Chinner82025d72012-11-12 22:54:12 +1100169 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
Dave Chinner0cb97762013-08-12 20:50:09 +1000171 p += xfs_dir3_data_entsize(mp, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 /*
174 * Need to have seen all the entries and all the bestfree slots.
175 */
Dave Chinner82025d72012-11-12 22:54:12 +1100176 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100177 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
178 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100179 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200180 if (lep[i].address ==
181 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 stale++;
183 if (i > 0)
Dave Chinner82025d72012-11-12 22:54:12 +1100184 XFS_WANT_CORRUPTED_RETURN(
185 be32_to_cpu(lep[i].hashval) >=
186 be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Dave Chinner82025d72012-11-12 22:54:12 +1100188 XFS_WANT_CORRUPTED_RETURN(count ==
189 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
190 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Dave Chinner82025d72012-11-12 22:54:12 +1100192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Dave Chinner33363fe2013-04-03 16:11:22 +1100195static bool
196xfs_dir3_data_verify(
Dave Chinnere4813572012-11-12 22:54:14 +1100197 struct xfs_buf *bp)
198{
199 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner33363fe2013-04-03 16:11:22 +1100200 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Dave Chinnere4813572012-11-12 22:54:14 +1100201
Dave Chinner33363fe2013-04-03 16:11:22 +1100202 if (xfs_sb_version_hascrc(&mp->m_sb)) {
203 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
204 return false;
205 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
206 return false;
207 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
208 return false;
209 } else {
210 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
211 return false;
Dave Chinnere4813572012-11-12 22:54:14 +1100212 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100213 if (__xfs_dir3_data_check(NULL, bp))
214 return false;
215 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +1100216}
Dave Chinnere4813572012-11-12 22:54:14 +1100217
Dave Chinner1813dd62012-11-14 17:54:40 +1100218/*
219 * Readahead of the first block of the directory when it is opened is completely
220 * oblivious to the format of the directory. Hence we can either get a block
221 * format buffer or a data format buffer on readahead.
222 */
223static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100224xfs_dir3_data_reada_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100225 struct xfs_buf *bp)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100226{
Dave Chinner1813dd62012-11-14 17:54:40 +1100227 struct xfs_mount *mp = bp->b_target->bt_mount;
228 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
229
230 switch (hdr->magic) {
231 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100232 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
233 bp->b_ops = &xfs_dir3_block_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100234 bp->b_ops->verify_read(bp);
235 return;
236 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Dave Chinner33363fe2013-04-03 16:11:22 +1100237 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
238 xfs_dir3_data_verify(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +1100239 return;
240 default:
241 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
242 xfs_buf_ioerror(bp, EFSCORRUPTED);
243 break;
244 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100245}
246
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100247static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100248xfs_dir3_data_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100249 struct xfs_buf *bp)
250{
Dave Chinner33363fe2013-04-03 16:11:22 +1100251 struct xfs_mount *mp = bp->b_target->bt_mount;
252
253 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
254 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
255 XFS_DIR3_DATA_CRC_OFF)) ||
256 !xfs_dir3_data_verify(bp)) {
257 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
258 xfs_buf_ioerror(bp, EFSCORRUPTED);
259 }
Dave Chinnere4813572012-11-12 22:54:14 +1100260}
261
Dave Chinner1813dd62012-11-14 17:54:40 +1100262static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100263xfs_dir3_data_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100264 struct xfs_buf *bp)
265{
Dave Chinner33363fe2013-04-03 16:11:22 +1100266 struct xfs_mount *mp = bp->b_target->bt_mount;
267 struct xfs_buf_log_item *bip = bp->b_fspriv;
268 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
269
270 if (!xfs_dir3_data_verify(bp)) {
271 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
272 xfs_buf_ioerror(bp, EFSCORRUPTED);
273 return;
274 }
275
276 if (!xfs_sb_version_hascrc(&mp->m_sb))
277 return;
278
279 if (bip)
280 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
281
282 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100283}
284
Dave Chinner33363fe2013-04-03 16:11:22 +1100285const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
286 .verify_read = xfs_dir3_data_read_verify,
287 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100288};
289
Dave Chinner33363fe2013-04-03 16:11:22 +1100290static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
291 .verify_read = xfs_dir3_data_reada_verify,
292 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100293};
294
Dave Chinner612cfbf2012-11-14 17:52:32 +1100295
Dave Chinnere4813572012-11-12 22:54:14 +1100296int
Dave Chinner33363fe2013-04-03 16:11:22 +1100297xfs_dir3_data_read(
Dave Chinnere4813572012-11-12 22:54:14 +1100298 struct xfs_trans *tp,
299 struct xfs_inode *dp,
300 xfs_dablk_t bno,
301 xfs_daddr_t mapped_bno,
302 struct xfs_buf **bpp)
303{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100304 int err;
305
306 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
Dave Chinner33363fe2013-04-03 16:11:22 +1100307 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100308 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100309 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100310 return err;
Dave Chinnere4813572012-11-12 22:54:14 +1100311}
312
Dave Chinnerda6958c2012-11-12 22:54:18 +1100313int
Dave Chinner33363fe2013-04-03 16:11:22 +1100314xfs_dir3_data_readahead(
Dave Chinnerda6958c2012-11-12 22:54:18 +1100315 struct xfs_trans *tp,
316 struct xfs_inode *dp,
317 xfs_dablk_t bno,
318 xfs_daddr_t mapped_bno)
319{
320 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
Dave Chinner33363fe2013-04-03 16:11:22 +1100321 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
Dave Chinnerda6958c2012-11-12 22:54:18 +1100322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * Given a data block and an unused entry from that block,
326 * return the bestfree entry if any that corresponds to it.
327 */
Dave Chinner2b9ab5a2013-08-12 20:49:37 +1000328xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329xfs_dir2_data_freefind(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200330 xfs_dir2_data_hdr_t *hdr, /* data block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 xfs_dir2_data_unused_t *dup) /* data unused entry */
332{
333 xfs_dir2_data_free_t *dfp; /* bestfree entry */
334 xfs_dir2_data_aoff_t off; /* offset value needed */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100335 struct xfs_dir2_data_free *bf;
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000336#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int matched; /* matched the value */
338 int seenzero; /* saw a 0 bestfree entry */
339#endif
340
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200341 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100342 bf = xfs_dir3_data_bestfree_p(hdr);
343
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000344#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /*
346 * Validate some consistency in the bestfree table.
347 * Check order, non-overlapping entries, and if we find the
348 * one we're looking for it has to be exact.
349 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200350 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100351 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100352 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
353 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
354 for (dfp = &bf[0], seenzero = matched = 0;
355 dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 dfp++) {
357 if (!dfp->offset) {
358 ASSERT(!dfp->length);
359 seenzero = 1;
360 continue;
361 }
362 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100363 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100365 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100366 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100367 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100369 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100370 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100371 if (dfp > &bf[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100372 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374#endif
375 /*
376 * If this is smaller than the smallest bestfree entry,
377 * it can't be there since they're sorted.
378 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100379 if (be16_to_cpu(dup->length) <
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100380 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return NULL;
382 /*
383 * Look at the three bestfree entries for our guy.
384 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100385 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (!dfp->offset)
387 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100388 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return dfp;
390 }
391 /*
392 * Didn't find it. This only happens if there are duplicate lengths.
393 */
394 return NULL;
395}
396
397/*
398 * Insert an unused-space entry into the bestfree table.
399 */
400xfs_dir2_data_free_t * /* entry inserted */
401xfs_dir2_data_freeinsert(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200402 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 xfs_dir2_data_unused_t *dup, /* unused space */
404 int *loghead) /* log the data header (out) */
405{
406 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
407 xfs_dir2_data_free_t new; /* new bestfree entry */
408
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200409 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100410 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
411 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
412 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
413
414 dfp = xfs_dir3_data_bestfree_p(hdr);
Nathan Scott70e73f52006-03-17 17:26:52 +1100415 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200416 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
419 * Insert at position 0, 1, or 2; or not at all.
420 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100421 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 dfp[2] = dfp[1];
423 dfp[1] = dfp[0];
424 dfp[0] = new;
425 *loghead = 1;
426 return &dfp[0];
427 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100428 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 dfp[2] = dfp[1];
430 dfp[1] = new;
431 *loghead = 1;
432 return &dfp[1];
433 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100434 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 dfp[2] = new;
436 *loghead = 1;
437 return &dfp[2];
438 }
439 return NULL;
440}
441
442/*
443 * Remove a bestfree entry from the table.
444 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000445STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446xfs_dir2_data_freeremove(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200447 xfs_dir2_data_hdr_t *hdr, /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
449 int *loghead) /* out: log data header */
450{
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100451 struct xfs_dir2_data_free *bf;
452
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200453 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100454 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
455 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
456 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /*
459 * It's the first entry, slide the next 2 up.
460 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100461 bf = xfs_dir3_data_bestfree_p(hdr);
462 if (dfp == &bf[0]) {
463 bf[0] = bf[1];
464 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466 /*
467 * It's the second entry, slide the 3rd entry up.
468 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100469 else if (dfp == &bf[1])
470 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /*
472 * Must be the last entry.
473 */
474 else
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100475 ASSERT(dfp == &bf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /*
477 * Clear the 3rd entry, must be zero now.
478 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100479 bf[2].length = 0;
480 bf[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 *loghead = 1;
482}
483
484/*
485 * Given a data block, reconstruct its bestfree map.
486 */
487void
488xfs_dir2_data_freescan(
489 xfs_mount_t *mp, /* filesystem mount point */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200490 xfs_dir2_data_hdr_t *hdr, /* data block header */
Eric Sandeenef497f82007-05-08 13:48:49 +1000491 int *loghead) /* out: log data header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 xfs_dir2_block_tail_t *btp; /* block tail */
494 xfs_dir2_data_entry_t *dep; /* active data entry */
495 xfs_dir2_data_unused_t *dup; /* unused data entry */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100496 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 char *endp; /* end of block's data */
498 char *p; /* current entry pointer */
499
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200500 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100501 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100502 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
503 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /*
506 * Start by clearing the table.
507 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100508 bf = xfs_dir3_data_bestfree_p(hdr);
509 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *loghead = 1;
511 /*
512 * Set up pointers.
513 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100514 p = (char *)xfs_dir3_data_entry_p(hdr);
515 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
516 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200517 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000518 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 } else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200520 endp = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /*
522 * Loop over the block's entries.
523 */
524 while (p < endp) {
525 dup = (xfs_dir2_data_unused_t *)p;
526 /*
527 * If it's a free entry, insert it.
528 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100529 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200530 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000531 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200532 xfs_dir2_data_freeinsert(hdr, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100533 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 /*
536 * For active entries, check their tags and skip them.
537 */
538 else {
539 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200540 ASSERT((char *)dep - (char *)hdr ==
Dave Chinner0cb97762013-08-12 20:50:09 +1000541 be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep)));
542 p += xfs_dir3_data_entsize(mp, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544 }
545}
546
547/*
548 * Initialize a data block at the given block number in the directory.
549 * Give back the buffer for the created block.
550 */
551int /* error */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100552xfs_dir3_data_init(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 xfs_da_args_t *args, /* directory operation args */
554 xfs_dir2_db_t blkno, /* logical dir block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000555 struct xfs_buf **bpp) /* output block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000557 struct xfs_buf *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200558 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 xfs_inode_t *dp; /* incore directory inode */
560 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100561 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int error; /* error return value */
563 int i; /* bestfree index */
564 xfs_mount_t *mp; /* filesystem mount point */
565 xfs_trans_t *tp; /* transaction pointer */
566 int t; /* temp */
567
568 dp = args->dp;
569 mp = dp->i_mount;
570 tp = args->trans;
571 /*
572 * Get the buffer set up for the block.
573 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000574 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 XFS_DATA_FORK);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100576 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return error;
Dave Chinner33363fe2013-04-03 16:11:22 +1100578 bp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100579 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * Initialize the header.
583 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000584 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100585 if (xfs_sb_version_hascrc(&mp->m_sb)) {
586 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
587
588 memset(hdr3, 0, sizeof(*hdr3));
589 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
590 hdr3->blkno = cpu_to_be64(bp->b_bn);
591 hdr3->owner = cpu_to_be64(dp->i_ino);
592 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
593
594 } else
595 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
596
597 bf = xfs_dir3_data_bestfree_p(hdr);
598 bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100600 bf[i].length = 0;
601 bf[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
605 * Set up an unused entry for the block's body.
606 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100607 dup = xfs_dir3_data_unused_p(hdr);
Nathan Scottad354eb2006-03-17 17:27:37 +1100608 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100610 t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
611 bf[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100612 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200613 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
615 * Log it and return it.
616 */
617 xfs_dir2_data_log_header(tp, bp);
618 xfs_dir2_data_log_unused(tp, bp, dup);
619 *bpp = bp;
620 return 0;
621}
622
623/*
624 * Log an active data entry from the block.
625 */
626void
627xfs_dir2_data_log_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000628 struct xfs_trans *tp,
629 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 xfs_dir2_data_entry_t *dep) /* data entry pointer */
631{
Dave Chinner0cb97762013-08-12 20:50:09 +1000632 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
633 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200635 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100636 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100637 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
638 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200639
Dave Chinner1d9025e2012-06-22 18:50:14 +1000640 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
Dave Chinner0cb97762013-08-12 20:50:09 +1000641 (uint)((char *)(xfs_dir3_data_entry_tag_p(mp, dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200642 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645/*
646 * Log a data block header.
647 */
648void
649xfs_dir2_data_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000650 struct xfs_trans *tp,
651 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000653 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200655 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100656 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100657 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
658 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200659
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100660 xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663/*
664 * Log a data unused entry.
665 */
666void
667xfs_dir2_data_log_unused(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000668 struct xfs_trans *tp,
669 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 xfs_dir2_data_unused_t *dup) /* data unused pointer */
671{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000672 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200674 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100675 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100676 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
677 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /*
680 * Log the first part of the unused entry.
681 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000682 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200684 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /*
686 * Log the end (tag) of the unused entry.
687 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000688 xfs_trans_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200689 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
690 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 sizeof(xfs_dir2_data_off_t) - 1));
692}
693
694/*
695 * Make a byte range in the data block unused.
696 * Its current contents are unimportant.
697 */
698void
699xfs_dir2_data_make_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000700 struct xfs_trans *tp,
701 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 xfs_dir2_data_aoff_t offset, /* starting byte offset */
703 xfs_dir2_data_aoff_t len, /* length in bytes */
704 int *needlogp, /* out: log header */
705 int *needscanp) /* out: regen bestfree */
706{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200707 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
709 char *endptr; /* end of data area */
710 xfs_mount_t *mp; /* filesystem mount point */
711 int needscan; /* need to regen bestfree */
712 xfs_dir2_data_unused_t *newdup; /* new unused entry */
713 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
714 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100715 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000718 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /*
721 * Figure out where the end of the data area is.
722 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100723 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
724 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200725 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 else {
727 xfs_dir2_block_tail_t *btp; /* block tail */
728
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100729 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
730 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200731 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000732 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 /*
735 * If this isn't the start of the block, then back up to
736 * the previous entry and see if it's free.
737 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100738 if (offset > xfs_dir3_data_entry_offset(hdr)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100739 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200741 tagp = (__be16 *)((char *)hdr + offset) - 1;
742 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100743 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 prevdup = NULL;
745 } else
746 prevdup = NULL;
747 /*
748 * If this isn't the end of the block, see if the entry after
749 * us is free.
750 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200751 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200753 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100754 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 postdup = NULL;
756 } else
757 postdup = NULL;
758 ASSERT(*needscanp == 0);
759 needscan = 0;
760 /*
761 * Previous and following entries are both free,
762 * merge everything into a single free entry.
763 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100764 bf = xfs_dir3_data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (prevdup && postdup) {
766 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
767
768 /*
769 * See if prevdup and/or postdup are in bestfree table.
770 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200771 dfp = xfs_dir2_data_freefind(hdr, prevdup);
772 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /*
774 * We need a rescan unless there are exactly 2 free entries
775 * namely our two. Then we know what's happening, otherwise
776 * since the third bestfree is there, there might be more
777 * entries.
778 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100779 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * Fix up the new big freespace.
782 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800783 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000784 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200785 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 xfs_dir2_data_log_unused(tp, bp, prevdup);
787 if (!needscan) {
788 /*
789 * Has to be the case that entries 0 and 1 are
790 * dfp and dfp2 (don't know which is which), and
791 * entry 2 is empty.
792 * Remove entry 1 first then entry 0.
793 */
794 ASSERT(dfp && dfp2);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100795 if (dfp == &bf[1]) {
796 dfp = &bf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 ASSERT(dfp2 == dfp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100798 dfp2 = &bf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200800 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
801 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /*
803 * Now insert the new entry.
804 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200805 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100806 ASSERT(dfp == &bf[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100807 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ASSERT(!dfp[1].length);
809 ASSERT(!dfp[2].length);
810 }
811 }
812 /*
813 * The entry before us is free, merge with it.
814 */
815 else if (prevdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200816 dfp = xfs_dir2_data_freefind(hdr, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800817 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000818 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200819 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 xfs_dir2_data_log_unused(tp, bp, prevdup);
821 /*
822 * If the previous entry was in the table, the new entry
823 * is longer, so it will be in the table too. Remove
824 * the old one and add the new one.
825 */
826 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200827 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
828 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 /*
831 * Otherwise we need a scan if the new entry is big enough.
832 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100833 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100834 needscan = be16_to_cpu(prevdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100835 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838 /*
839 * The following entry is free, merge with it.
840 */
841 else if (postdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200842 dfp = xfs_dir2_data_freefind(hdr, postdup);
843 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100844 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
845 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000846 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200847 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 xfs_dir2_data_log_unused(tp, bp, newdup);
849 /*
850 * If the following entry was in the table, the new entry
851 * is longer, so it will be in the table too. Remove
852 * the old one and add the new one.
853 */
854 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200855 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
856 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858 /*
859 * Otherwise we need a scan if the new entry is big enough.
860 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100861 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100862 needscan = be16_to_cpu(newdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100863 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 /*
867 * Neither neighbor is free. Make a new entry.
868 */
869 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200870 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100871 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
872 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000873 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200874 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200876 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 *needscanp = needscan;
879}
880
881/*
882 * Take a byte range out of an existing unused space and make it un-free.
883 */
884void
885xfs_dir2_data_use_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000886 struct xfs_trans *tp,
887 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 xfs_dir2_data_unused_t *dup, /* unused entry */
889 xfs_dir2_data_aoff_t offset, /* starting offset to use */
890 xfs_dir2_data_aoff_t len, /* length to use */
891 int *needlogp, /* out: need to log header */
892 int *needscanp) /* out: need regen bestfree */
893{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200894 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
896 int matchback; /* matches end of freespace */
897 int matchfront; /* matches start of freespace */
898 int needscan; /* need to regen bestfree */
899 xfs_dir2_data_unused_t *newdup; /* new unused entry */
900 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
901 int oldlen; /* old unused entry's length */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100902 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Dave Chinner1d9025e2012-06-22 18:50:14 +1000904 hdr = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200905 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100906 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100907 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
908 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Nathan Scottad354eb2006-03-17 17:27:37 +1100909 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200910 ASSERT(offset >= (char *)dup - (char *)hdr);
911 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
912 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /*
914 * Look up the entry in the bestfree table.
915 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200916 dfp = xfs_dir2_data_freefind(hdr, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100917 oldlen = be16_to_cpu(dup->length);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100918 bf = xfs_dir3_data_bestfree_p(hdr);
919 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /*
921 * Check for alignment with front and back of the entry.
922 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200923 matchfront = (char *)dup - (char *)hdr == offset;
924 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 ASSERT(*needscanp == 0);
926 needscan = 0;
927 /*
928 * If we matched it exactly we just need to get rid of it from
929 * the bestfree table.
930 */
931 if (matchfront && matchback) {
932 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100933 needscan = (bf[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (!needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200935 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 }
938 /*
939 * We match the first part of the entry.
940 * Make a new entry with the remaining freespace.
941 */
942 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200943 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100944 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
945 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000946 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200947 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 xfs_dir2_data_log_unused(tp, bp, newdup);
949 /*
950 * If it was in the table, remove it and add the new one.
951 */
952 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200953 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
954 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100956 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200957 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /*
959 * If we got inserted at the last slot,
960 * that means we don't know if there was a better
961 * choice for the last slot, or not. Rescan.
962 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100963 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965 }
966 /*
967 * We match the last part of the entry.
968 * Trim the allocated space off the tail of the entry.
969 */
970 else if (matchback) {
971 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200972 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000973 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200974 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 xfs_dir2_data_log_unused(tp, bp, newdup);
976 /*
977 * If it was in the table, remove it and add the new one.
978 */
979 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200980 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
981 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100983 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200984 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /*
986 * If we got inserted at the last slot,
987 * that means we don't know if there was a better
988 * choice for the last slot, or not. Rescan.
989 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100990 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992 }
993 /*
994 * Poking out the middle of an entry.
995 * Make two new entries.
996 */
997 else {
998 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200999 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001000 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001001 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001003 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +11001004 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1005 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001006 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001007 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 xfs_dir2_data_log_unused(tp, bp, newdup2);
1009 /*
1010 * If the old entry was in the table, we need to scan
1011 * if the 3rd entry was valid, since these entries
1012 * are smaller than the old one.
1013 * If we don't need to scan that means there were 1 or 2
1014 * entries in the table, and removing the old and adding
1015 * the 2 new will work.
1016 */
1017 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001018 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (!needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001020 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
1021 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
1022 xfs_dir2_data_freeinsert(hdr, newdup2,
1023 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 }
1026 }
1027 *needscanp = needscan;
1028}