blob: ccfeb4d8376a6aa91985ce30c19285bc221640f4 [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"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_inode.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100030#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020031#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Dave Chinner33363fe2013-04-03 16:11:22 +110034#include "xfs_buf_item.h"
35#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
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
Dave Chinner33363fe2013-04-03 16:11:22 +110043__xfs_dir3_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;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110068 bf = xfs_dir3_data_bestfree_p(hdr);
69 p = (char *)xfs_dir3_data_entry_p(hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +020070
Dave Chinner82025d72012-11-12 22:54:12 +110071 switch (hdr->magic) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110072 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110073 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020074 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100075 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 endp = (char *)lep;
Dave Chinner82025d72012-11-12 22:54:12 +110077 break;
Dave Chinner33363fe2013-04-03 16:11:22 +110078 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110079 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020080 endp = (char *)hdr + mp->m_dirblksize;
Dave Chinner82025d72012-11-12 22:54:12 +110081 break;
82 default:
83 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
84 return EFSCORRUPTED;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020085 }
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 count = lastfree = freeseen = 0;
88 /*
89 * Account for zero bestfree entries.
90 */
91 if (!bf[0].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110092 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 freeseen |= 1 << 0;
94 }
95 if (!bf[1].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110096 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 freeseen |= 1 << 1;
98 }
99 if (!bf[2].length) {
Dave Chinner82025d72012-11-12 22:54:12 +1100100 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 freeseen |= 1 << 2;
102 }
Dave Chinner82025d72012-11-12 22:54:12 +1100103
104 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
105 be16_to_cpu(bf[1].length));
106 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
107 be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /*
109 * Loop over the data/unused entries.
110 */
111 while (p < endp) {
112 dup = (xfs_dir2_data_unused_t *)p;
113 /*
114 * If it's unused, look for the space in the bestfree table.
115 * If we find it, account for that, else make sure it
116 * doesn't need to be there.
117 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100118 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Dave Chinner82025d72012-11-12 22:54:12 +1100119 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
120 XFS_WANT_CORRUPTED_RETURN(
121 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
122 (char *)dup - (char *)hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200123 dfp = xfs_dir2_data_freefind(hdr, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (dfp) {
125 i = (int)(dfp - bf);
Dave Chinner82025d72012-11-12 22:54:12 +1100126 XFS_WANT_CORRUPTED_RETURN(
127 (freeseen & (1 << i)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100129 } else {
Dave Chinner82025d72012-11-12 22:54:12 +1100130 XFS_WANT_CORRUPTED_RETURN(
131 be16_to_cpu(dup->length) <=
132 be16_to_cpu(bf[2].length));
Nathan Scott70e73f52006-03-17 17:26:52 +1100133 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100134 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 lastfree = 1;
136 continue;
137 }
138 /*
139 * It's a real entry. Validate the fields.
140 * If this is a block directory then make sure it's
141 * in the leaf section of the block.
142 * The linear search is crude but this is DEBUG code.
143 */
144 dep = (xfs_dir2_data_entry_t *)p;
Dave Chinner82025d72012-11-12 22:54:12 +1100145 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
146 XFS_WANT_CORRUPTED_RETURN(
147 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
148 XFS_WANT_CORRUPTED_RETURN(
Dave Chinner0cb97762013-08-12 20:50:09 +1000149 be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep)) ==
Dave Chinner82025d72012-11-12 22:54:12 +1100150 (char *)dep - (char *)hdr);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000151 XFS_WANT_CORRUPTED_RETURN(
152 xfs_dir3_dirent_get_ftype(mp, dep) < XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 count++;
154 lastfree = 0;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100155 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
156 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000157 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 (xfs_dir2_data_aoff_t)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200159 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000160 name.name = dep->name;
161 name.len = dep->namelen;
162 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100163 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100164 if (be32_to_cpu(lep[i].address) == addr &&
165 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 break;
167 }
Dave Chinner82025d72012-11-12 22:54:12 +1100168 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Dave Chinner0cb97762013-08-12 20:50:09 +1000170 p += xfs_dir3_data_entsize(mp, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 /*
173 * Need to have seen all the entries and all the bestfree slots.
174 */
Dave Chinner82025d72012-11-12 22:54:12 +1100175 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100176 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
177 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100178 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200179 if (lep[i].address ==
180 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 stale++;
182 if (i > 0)
Dave Chinner82025d72012-11-12 22:54:12 +1100183 XFS_WANT_CORRUPTED_RETURN(
184 be32_to_cpu(lep[i].hashval) >=
185 be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
Dave Chinner82025d72012-11-12 22:54:12 +1100187 XFS_WANT_CORRUPTED_RETURN(count ==
188 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
189 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Dave Chinner82025d72012-11-12 22:54:12 +1100191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Dave Chinner33363fe2013-04-03 16:11:22 +1100194static bool
195xfs_dir3_data_verify(
Dave Chinnere4813572012-11-12 22:54:14 +1100196 struct xfs_buf *bp)
197{
198 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner33363fe2013-04-03 16:11:22 +1100199 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Dave Chinnere4813572012-11-12 22:54:14 +1100200
Dave Chinner33363fe2013-04-03 16:11:22 +1100201 if (xfs_sb_version_hascrc(&mp->m_sb)) {
202 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
203 return false;
204 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
205 return false;
206 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
207 return false;
208 } else {
209 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
210 return false;
Dave Chinnere4813572012-11-12 22:54:14 +1100211 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100212 if (__xfs_dir3_data_check(NULL, bp))
213 return false;
214 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +1100215}
Dave Chinnere4813572012-11-12 22:54:14 +1100216
Dave Chinner1813dd62012-11-14 17:54:40 +1100217/*
218 * Readahead of the first block of the directory when it is opened is completely
219 * oblivious to the format of the directory. Hence we can either get a block
220 * format buffer or a data format buffer on readahead.
221 */
222static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100223xfs_dir3_data_reada_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100224 struct xfs_buf *bp)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100225{
Dave Chinner1813dd62012-11-14 17:54:40 +1100226 struct xfs_mount *mp = bp->b_target->bt_mount;
227 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
228
229 switch (hdr->magic) {
230 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100231 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
232 bp->b_ops = &xfs_dir3_block_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100233 bp->b_ops->verify_read(bp);
234 return;
235 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Dave Chinner33363fe2013-04-03 16:11:22 +1100236 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
237 xfs_dir3_data_verify(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +1100238 return;
239 default:
240 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
241 xfs_buf_ioerror(bp, EFSCORRUPTED);
242 break;
243 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100244}
245
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100246static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100247xfs_dir3_data_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100248 struct xfs_buf *bp)
249{
Dave Chinner33363fe2013-04-03 16:11:22 +1100250 struct xfs_mount *mp = bp->b_target->bt_mount;
251
252 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
253 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
254 XFS_DIR3_DATA_CRC_OFF)) ||
255 !xfs_dir3_data_verify(bp)) {
256 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
257 xfs_buf_ioerror(bp, EFSCORRUPTED);
258 }
Dave Chinnere4813572012-11-12 22:54:14 +1100259}
260
Dave Chinner1813dd62012-11-14 17:54:40 +1100261static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100262xfs_dir3_data_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100263 struct xfs_buf *bp)
264{
Dave Chinner33363fe2013-04-03 16:11:22 +1100265 struct xfs_mount *mp = bp->b_target->bt_mount;
266 struct xfs_buf_log_item *bip = bp->b_fspriv;
267 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
268
269 if (!xfs_dir3_data_verify(bp)) {
270 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
271 xfs_buf_ioerror(bp, EFSCORRUPTED);
272 return;
273 }
274
275 if (!xfs_sb_version_hascrc(&mp->m_sb))
276 return;
277
278 if (bip)
279 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
280
281 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100282}
283
Dave Chinner33363fe2013-04-03 16:11:22 +1100284const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
285 .verify_read = xfs_dir3_data_read_verify,
286 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100287};
288
Dave Chinner33363fe2013-04-03 16:11:22 +1100289static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
290 .verify_read = xfs_dir3_data_reada_verify,
291 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100292};
293
Dave Chinner612cfbf2012-11-14 17:52:32 +1100294
Dave Chinnere4813572012-11-12 22:54:14 +1100295int
Dave Chinner33363fe2013-04-03 16:11:22 +1100296xfs_dir3_data_read(
Dave Chinnere4813572012-11-12 22:54:14 +1100297 struct xfs_trans *tp,
298 struct xfs_inode *dp,
299 xfs_dablk_t bno,
300 xfs_daddr_t mapped_bno,
301 struct xfs_buf **bpp)
302{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100303 int err;
304
305 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
Dave Chinner33363fe2013-04-03 16:11:22 +1100306 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100307 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100308 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100309 return err;
Dave Chinnere4813572012-11-12 22:54:14 +1100310}
311
Dave Chinnerda6958c2012-11-12 22:54:18 +1100312int
Dave Chinner33363fe2013-04-03 16:11:22 +1100313xfs_dir3_data_readahead(
Dave Chinnerda6958c2012-11-12 22:54:18 +1100314 struct xfs_trans *tp,
315 struct xfs_inode *dp,
316 xfs_dablk_t bno,
317 xfs_daddr_t mapped_bno)
318{
319 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
Dave Chinner33363fe2013-04-03 16:11:22 +1100320 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
Dave Chinnerda6958c2012-11-12 22:54:18 +1100321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/*
324 * Given a data block and an unused entry from that block,
325 * return the bestfree entry if any that corresponds to it.
326 */
Dave Chinner2b9ab5a2013-08-12 20:49:37 +1000327xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328xfs_dir2_data_freefind(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200329 xfs_dir2_data_hdr_t *hdr, /* data block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 xfs_dir2_data_unused_t *dup) /* data unused entry */
331{
332 xfs_dir2_data_free_t *dfp; /* bestfree entry */
333 xfs_dir2_data_aoff_t off; /* offset value needed */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100334 struct xfs_dir2_data_free *bf;
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000335#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int matched; /* matched the value */
337 int seenzero; /* saw a 0 bestfree entry */
338#endif
339
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200340 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100341 bf = xfs_dir3_data_bestfree_p(hdr);
342
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000343#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
345 * Validate some consistency in the bestfree table.
346 * Check order, non-overlapping entries, and if we find the
347 * one we're looking for it has to be exact.
348 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200349 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100350 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100351 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
352 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
353 for (dfp = &bf[0], seenzero = matched = 0;
354 dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 dfp++) {
356 if (!dfp->offset) {
357 ASSERT(!dfp->length);
358 seenzero = 1;
359 continue;
360 }
361 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100362 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100364 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100365 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100366 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100368 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100369 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100370 if (dfp > &bf[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100371 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373#endif
374 /*
375 * If this is smaller than the smallest bestfree entry,
376 * it can't be there since they're sorted.
377 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100378 if (be16_to_cpu(dup->length) <
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100379 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return NULL;
381 /*
382 * Look at the three bestfree entries for our guy.
383 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100384 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (!dfp->offset)
386 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100387 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return dfp;
389 }
390 /*
391 * Didn't find it. This only happens if there are duplicate lengths.
392 */
393 return NULL;
394}
395
396/*
397 * Insert an unused-space entry into the bestfree table.
398 */
399xfs_dir2_data_free_t * /* entry inserted */
400xfs_dir2_data_freeinsert(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200401 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 xfs_dir2_data_unused_t *dup, /* unused space */
403 int *loghead) /* log the data header (out) */
404{
405 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
406 xfs_dir2_data_free_t new; /* new bestfree entry */
407
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200408 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100409 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
410 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
411 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
412
413 dfp = xfs_dir3_data_bestfree_p(hdr);
Nathan Scott70e73f52006-03-17 17:26:52 +1100414 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200415 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
418 * Insert at position 0, 1, or 2; or not at all.
419 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100420 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 dfp[2] = dfp[1];
422 dfp[1] = dfp[0];
423 dfp[0] = new;
424 *loghead = 1;
425 return &dfp[0];
426 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100427 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 dfp[2] = dfp[1];
429 dfp[1] = new;
430 *loghead = 1;
431 return &dfp[1];
432 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100433 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 dfp[2] = new;
435 *loghead = 1;
436 return &dfp[2];
437 }
438 return NULL;
439}
440
441/*
442 * Remove a bestfree entry from the table.
443 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000444STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445xfs_dir2_data_freeremove(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200446 xfs_dir2_data_hdr_t *hdr, /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
448 int *loghead) /* out: log data header */
449{
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100450 struct xfs_dir2_data_free *bf;
451
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200452 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100453 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
454 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
455 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /*
458 * It's the first entry, slide the next 2 up.
459 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100460 bf = xfs_dir3_data_bestfree_p(hdr);
461 if (dfp == &bf[0]) {
462 bf[0] = bf[1];
463 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465 /*
466 * It's the second entry, slide the 3rd entry up.
467 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100468 else if (dfp == &bf[1])
469 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
471 * Must be the last entry.
472 */
473 else
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100474 ASSERT(dfp == &bf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /*
476 * Clear the 3rd entry, must be zero now.
477 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100478 bf[2].length = 0;
479 bf[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 *loghead = 1;
481}
482
483/*
484 * Given a data block, reconstruct its bestfree map.
485 */
486void
487xfs_dir2_data_freescan(
488 xfs_mount_t *mp, /* filesystem mount point */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200489 xfs_dir2_data_hdr_t *hdr, /* data block header */
Eric Sandeenef497f82007-05-08 13:48:49 +1000490 int *loghead) /* out: log data header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 xfs_dir2_block_tail_t *btp; /* block tail */
493 xfs_dir2_data_entry_t *dep; /* active data entry */
494 xfs_dir2_data_unused_t *dup; /* unused data entry */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100495 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 char *endp; /* end of block's data */
497 char *p; /* current entry pointer */
498
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200499 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100500 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100501 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
502 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
505 * Start by clearing the table.
506 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100507 bf = xfs_dir3_data_bestfree_p(hdr);
508 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *loghead = 1;
510 /*
511 * Set up pointers.
512 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100513 p = (char *)xfs_dir3_data_entry_p(hdr);
514 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
515 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200516 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000517 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 } else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200519 endp = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /*
521 * Loop over the block's entries.
522 */
523 while (p < endp) {
524 dup = (xfs_dir2_data_unused_t *)p;
525 /*
526 * If it's a free entry, insert it.
527 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100528 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200529 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000530 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200531 xfs_dir2_data_freeinsert(hdr, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100532 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 /*
535 * For active entries, check their tags and skip them.
536 */
537 else {
538 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200539 ASSERT((char *)dep - (char *)hdr ==
Dave Chinner0cb97762013-08-12 20:50:09 +1000540 be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep)));
541 p += xfs_dir3_data_entsize(mp, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 }
544}
545
546/*
547 * Initialize a data block at the given block number in the directory.
548 * Give back the buffer for the created block.
549 */
550int /* error */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100551xfs_dir3_data_init(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 xfs_da_args_t *args, /* directory operation args */
553 xfs_dir2_db_t blkno, /* logical dir block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000554 struct xfs_buf **bpp) /* output block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000556 struct xfs_buf *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200557 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 xfs_inode_t *dp; /* incore directory inode */
559 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100560 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int error; /* error return value */
562 int i; /* bestfree index */
563 xfs_mount_t *mp; /* filesystem mount point */
564 xfs_trans_t *tp; /* transaction pointer */
565 int t; /* temp */
566
567 dp = args->dp;
568 mp = dp->i_mount;
569 tp = args->trans;
570 /*
571 * Get the buffer set up for the block.
572 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000573 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 XFS_DATA_FORK);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100575 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return error;
Dave Chinner33363fe2013-04-03 16:11:22 +1100577 bp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100578 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /*
581 * Initialize the header.
582 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000583 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100584 if (xfs_sb_version_hascrc(&mp->m_sb)) {
585 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
586
587 memset(hdr3, 0, sizeof(*hdr3));
588 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
589 hdr3->blkno = cpu_to_be64(bp->b_bn);
590 hdr3->owner = cpu_to_be64(dp->i_ino);
591 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
592
593 } else
594 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
595
596 bf = xfs_dir3_data_bestfree_p(hdr);
597 bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100599 bf[i].length = 0;
600 bf[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /*
604 * Set up an unused entry for the block's body.
605 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100606 dup = xfs_dir3_data_unused_p(hdr);
Nathan Scottad354eb2006-03-17 17:27:37 +1100607 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100609 t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
610 bf[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100611 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200612 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /*
614 * Log it and return it.
615 */
616 xfs_dir2_data_log_header(tp, bp);
617 xfs_dir2_data_log_unused(tp, bp, dup);
618 *bpp = bp;
619 return 0;
620}
621
622/*
623 * Log an active data entry from the block.
624 */
625void
626xfs_dir2_data_log_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000627 struct xfs_trans *tp,
628 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 xfs_dir2_data_entry_t *dep) /* data entry pointer */
630{
Dave Chinner0cb97762013-08-12 20:50:09 +1000631 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
632 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200634 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100635 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100636 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
637 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200638
Dave Chinner1d9025e2012-06-22 18:50:14 +1000639 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
Dave Chinner0cb97762013-08-12 20:50:09 +1000640 (uint)((char *)(xfs_dir3_data_entry_tag_p(mp, dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200641 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/*
645 * Log a data block header.
646 */
647void
648xfs_dir2_data_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000649 struct xfs_trans *tp,
650 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000652 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200654 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100655 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100656 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
657 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200658
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100659 xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662/*
663 * Log a data unused entry.
664 */
665void
666xfs_dir2_data_log_unused(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000667 struct xfs_trans *tp,
668 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 xfs_dir2_data_unused_t *dup) /* data unused pointer */
670{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000671 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200673 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100674 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100675 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
676 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /*
679 * Log the first part of the unused entry.
680 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000681 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200683 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
685 * Log the end (tag) of the unused entry.
686 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000687 xfs_trans_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200688 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
689 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 sizeof(xfs_dir2_data_off_t) - 1));
691}
692
693/*
694 * Make a byte range in the data block unused.
695 * Its current contents are unimportant.
696 */
697void
698xfs_dir2_data_make_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000699 struct xfs_trans *tp,
700 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 xfs_dir2_data_aoff_t offset, /* starting byte offset */
702 xfs_dir2_data_aoff_t len, /* length in bytes */
703 int *needlogp, /* out: log header */
704 int *needscanp) /* out: regen bestfree */
705{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200706 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
708 char *endptr; /* end of data area */
709 xfs_mount_t *mp; /* filesystem mount point */
710 int needscan; /* need to regen bestfree */
711 xfs_dir2_data_unused_t *newdup; /* new unused entry */
712 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
713 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100714 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000717 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /*
720 * Figure out where the end of the data area is.
721 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100722 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
723 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200724 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 else {
726 xfs_dir2_block_tail_t *btp; /* block tail */
727
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100728 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
729 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200730 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000731 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733 /*
734 * If this isn't the start of the block, then back up to
735 * the previous entry and see if it's free.
736 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100737 if (offset > xfs_dir3_data_entry_offset(hdr)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100738 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200740 tagp = (__be16 *)((char *)hdr + offset) - 1;
741 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100742 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 prevdup = NULL;
744 } else
745 prevdup = NULL;
746 /*
747 * If this isn't the end of the block, see if the entry after
748 * us is free.
749 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200750 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200752 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100753 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 postdup = NULL;
755 } else
756 postdup = NULL;
757 ASSERT(*needscanp == 0);
758 needscan = 0;
759 /*
760 * Previous and following entries are both free,
761 * merge everything into a single free entry.
762 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100763 bf = xfs_dir3_data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (prevdup && postdup) {
765 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
766
767 /*
768 * See if prevdup and/or postdup are in bestfree table.
769 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200770 dfp = xfs_dir2_data_freefind(hdr, prevdup);
771 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /*
773 * We need a rescan unless there are exactly 2 free entries
774 * namely our two. Then we know what's happening, otherwise
775 * since the third bestfree is there, there might be more
776 * entries.
777 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100778 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /*
780 * Fix up the new big freespace.
781 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800782 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000783 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200784 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 xfs_dir2_data_log_unused(tp, bp, prevdup);
786 if (!needscan) {
787 /*
788 * Has to be the case that entries 0 and 1 are
789 * dfp and dfp2 (don't know which is which), and
790 * entry 2 is empty.
791 * Remove entry 1 first then entry 0.
792 */
793 ASSERT(dfp && dfp2);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100794 if (dfp == &bf[1]) {
795 dfp = &bf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 ASSERT(dfp2 == dfp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100797 dfp2 = &bf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200799 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
800 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /*
802 * Now insert the new entry.
803 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200804 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100805 ASSERT(dfp == &bf[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100806 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ASSERT(!dfp[1].length);
808 ASSERT(!dfp[2].length);
809 }
810 }
811 /*
812 * The entry before us is free, merge with it.
813 */
814 else if (prevdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200815 dfp = xfs_dir2_data_freefind(hdr, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800816 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000817 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200818 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 xfs_dir2_data_log_unused(tp, bp, prevdup);
820 /*
821 * If the previous entry was in the table, the new entry
822 * is longer, so it will be in the table too. Remove
823 * the old one and add the new one.
824 */
825 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200826 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
827 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 /*
830 * Otherwise we need a scan if the new entry is big enough.
831 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100832 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100833 needscan = be16_to_cpu(prevdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100834 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 /*
838 * The following entry is free, merge with it.
839 */
840 else if (postdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200841 dfp = xfs_dir2_data_freefind(hdr, postdup);
842 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100843 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
844 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000845 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200846 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 xfs_dir2_data_log_unused(tp, bp, newdup);
848 /*
849 * If the following entry was in the table, the new entry
850 * is longer, so it will be in the table too. Remove
851 * the old one and add the new one.
852 */
853 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200854 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
855 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857 /*
858 * Otherwise we need a scan if the new entry is big enough.
859 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100860 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100861 needscan = be16_to_cpu(newdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100862 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865 /*
866 * Neither neighbor is free. Make a new entry.
867 */
868 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200869 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100870 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
871 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000872 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200873 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200875 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877 *needscanp = needscan;
878}
879
880/*
881 * Take a byte range out of an existing unused space and make it un-free.
882 */
883void
884xfs_dir2_data_use_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000885 struct xfs_trans *tp,
886 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 xfs_dir2_data_unused_t *dup, /* unused entry */
888 xfs_dir2_data_aoff_t offset, /* starting offset to use */
889 xfs_dir2_data_aoff_t len, /* length to use */
890 int *needlogp, /* out: need to log header */
891 int *needscanp) /* out: need regen bestfree */
892{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200893 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
895 int matchback; /* matches end of freespace */
896 int matchfront; /* matches start of freespace */
897 int needscan; /* need to regen bestfree */
898 xfs_dir2_data_unused_t *newdup; /* new unused entry */
899 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
900 int oldlen; /* old unused entry's length */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100901 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Dave Chinner1d9025e2012-06-22 18:50:14 +1000903 hdr = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200904 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100905 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100906 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
907 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Nathan Scottad354eb2006-03-17 17:27:37 +1100908 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200909 ASSERT(offset >= (char *)dup - (char *)hdr);
910 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
911 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /*
913 * Look up the entry in the bestfree table.
914 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200915 dfp = xfs_dir2_data_freefind(hdr, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100916 oldlen = be16_to_cpu(dup->length);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100917 bf = xfs_dir3_data_bestfree_p(hdr);
918 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 /*
920 * Check for alignment with front and back of the entry.
921 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200922 matchfront = (char *)dup - (char *)hdr == offset;
923 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 ASSERT(*needscanp == 0);
925 needscan = 0;
926 /*
927 * If we matched it exactly we just need to get rid of it from
928 * the bestfree table.
929 */
930 if (matchfront && matchback) {
931 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100932 needscan = (bf[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (!needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200934 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936 }
937 /*
938 * We match the first part of the entry.
939 * Make a new entry with the remaining freespace.
940 */
941 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200942 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100943 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
944 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000945 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200946 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 xfs_dir2_data_log_unused(tp, bp, newdup);
948 /*
949 * If it was in the table, remove it and add the new one.
950 */
951 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200952 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
953 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100955 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200956 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /*
958 * If we got inserted at the last slot,
959 * that means we don't know if there was a better
960 * choice for the last slot, or not. Rescan.
961 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100962 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964 }
965 /*
966 * We match the last part of the entry.
967 * Trim the allocated space off the tail of the entry.
968 */
969 else if (matchback) {
970 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200971 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000972 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200973 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 xfs_dir2_data_log_unused(tp, bp, newdup);
975 /*
976 * If it was in the table, remove it and add the new one.
977 */
978 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200979 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
980 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100982 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200983 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
985 * If we got inserted at the last slot,
986 * that means we don't know if there was a better
987 * choice for the last slot, or not. Rescan.
988 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100989 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991 }
992 /*
993 * Poking out the middle of an entry.
994 * Make two new entries.
995 */
996 else {
997 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200998 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000999 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001000 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001002 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +11001003 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1004 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001005 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001006 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 xfs_dir2_data_log_unused(tp, bp, newdup2);
1008 /*
1009 * If the old entry was in the table, we need to scan
1010 * if the 3rd entry was valid, since these entries
1011 * are smaller than the old one.
1012 * If we don't need to scan that means there were 1 or 2
1013 * entries in the table, and removing the old and adding
1014 * the 2 new will work.
1015 */
1016 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001017 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (!needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001019 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
1020 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
1021 xfs_dir2_data_freeinsert(hdr, newdup2,
1022 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024 }
1025 }
1026 *needscanp = needscan;
1027}