blob: 00006b3db054364364a34159d70b2b01d89bcd3a [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;
Dave Chinner9d23fc82013-10-29 22:11:48 +110065 const struct xfs_dir_ops *ops;
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 Chinner9d23fc82013-10-29 22:11:48 +110072 /*
73 * We can be passed a null dp here from a verifier, so manually
74 * configure the ops here in that case.
75 */
76 if (dp)
77 ops = dp->d_ops;
78 else if (xfs_sb_version_hascrc(&mp->m_sb))
79 ops = &xfs_dir3_ops;
80 else if (xfs_sb_version_hasftype(&mp->m_sb))
81 ops = &xfs_dir2_ftype_ops;
82 else
83 ops = &xfs_dir2_ops;
84
Dave Chinner82025d72012-11-12 22:54:12 +110085 switch (hdr->magic) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110086 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110087 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020088 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100089 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 endp = (char *)lep;
Dave Chinner82025d72012-11-12 22:54:12 +110091 break;
Dave Chinner33363fe2013-04-03 16:11:22 +110092 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110093 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020094 endp = (char *)hdr + mp->m_dirblksize;
Dave Chinner82025d72012-11-12 22:54:12 +110095 break;
96 default:
97 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
98 return EFSCORRUPTED;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020099 }
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 count = lastfree = freeseen = 0;
102 /*
103 * Account for zero bestfree entries.
104 */
105 if (!bf[0].length) {
Dave Chinner82025d72012-11-12 22:54:12 +1100106 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 freeseen |= 1 << 0;
108 }
109 if (!bf[1].length) {
Dave Chinner82025d72012-11-12 22:54:12 +1100110 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 freeseen |= 1 << 1;
112 }
113 if (!bf[2].length) {
Dave Chinner82025d72012-11-12 22:54:12 +1100114 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 freeseen |= 1 << 2;
116 }
Dave Chinner82025d72012-11-12 22:54:12 +1100117
118 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
119 be16_to_cpu(bf[1].length));
120 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
121 be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /*
123 * Loop over the data/unused entries.
124 */
125 while (p < endp) {
126 dup = (xfs_dir2_data_unused_t *)p;
127 /*
128 * If it's unused, look for the space in the bestfree table.
129 * If we find it, account for that, else make sure it
130 * doesn't need to be there.
131 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100132 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Dave Chinner82025d72012-11-12 22:54:12 +1100133 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
134 XFS_WANT_CORRUPTED_RETURN(
135 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
136 (char *)dup - (char *)hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200137 dfp = xfs_dir2_data_freefind(hdr, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (dfp) {
139 i = (int)(dfp - bf);
Dave Chinner82025d72012-11-12 22:54:12 +1100140 XFS_WANT_CORRUPTED_RETURN(
141 (freeseen & (1 << i)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100143 } else {
Dave Chinner82025d72012-11-12 22:54:12 +1100144 XFS_WANT_CORRUPTED_RETURN(
145 be16_to_cpu(dup->length) <=
146 be16_to_cpu(bf[2].length));
Nathan Scott70e73f52006-03-17 17:26:52 +1100147 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100148 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 lastfree = 1;
150 continue;
151 }
152 /*
153 * It's a real entry. Validate the fields.
154 * If this is a block directory then make sure it's
155 * in the leaf section of the block.
156 * The linear search is crude but this is DEBUG code.
157 */
158 dep = (xfs_dir2_data_entry_t *)p;
Dave Chinner82025d72012-11-12 22:54:12 +1100159 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
160 XFS_WANT_CORRUPTED_RETURN(
161 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
162 XFS_WANT_CORRUPTED_RETURN(
Dave Chinner9d23fc82013-10-29 22:11:48 +1100163 be16_to_cpu(*ops->data_entry_tag_p(dep)) ==
Dave Chinner82025d72012-11-12 22:54:12 +1100164 (char *)dep - (char *)hdr);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000165 XFS_WANT_CORRUPTED_RETURN(
Dave Chinner9d23fc82013-10-29 22:11:48 +1100166 ops->data_get_ftype(dep) < XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 count++;
168 lastfree = 0;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100169 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
170 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000171 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 (xfs_dir2_data_aoff_t)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200173 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000174 name.name = dep->name;
175 name.len = dep->namelen;
176 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100177 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100178 if (be32_to_cpu(lep[i].address) == addr &&
179 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 break;
181 }
Dave Chinner82025d72012-11-12 22:54:12 +1100182 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100184 p += ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186 /*
187 * Need to have seen all the entries and all the bestfree slots.
188 */
Dave Chinner82025d72012-11-12 22:54:12 +1100189 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100190 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
191 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100192 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200193 if (lep[i].address ==
194 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 stale++;
196 if (i > 0)
Dave Chinner82025d72012-11-12 22:54:12 +1100197 XFS_WANT_CORRUPTED_RETURN(
198 be32_to_cpu(lep[i].hashval) >=
199 be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Dave Chinner82025d72012-11-12 22:54:12 +1100201 XFS_WANT_CORRUPTED_RETURN(count ==
202 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
203 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Dave Chinner82025d72012-11-12 22:54:12 +1100205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Dave Chinner33363fe2013-04-03 16:11:22 +1100208static bool
209xfs_dir3_data_verify(
Dave Chinnere4813572012-11-12 22:54:14 +1100210 struct xfs_buf *bp)
211{
212 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner33363fe2013-04-03 16:11:22 +1100213 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Dave Chinnere4813572012-11-12 22:54:14 +1100214
Dave Chinner33363fe2013-04-03 16:11:22 +1100215 if (xfs_sb_version_hascrc(&mp->m_sb)) {
216 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
217 return false;
218 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
219 return false;
220 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
221 return false;
222 } else {
223 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
224 return false;
Dave Chinnere4813572012-11-12 22:54:14 +1100225 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100226 if (__xfs_dir3_data_check(NULL, bp))
227 return false;
228 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +1100229}
Dave Chinnere4813572012-11-12 22:54:14 +1100230
Dave Chinner1813dd62012-11-14 17:54:40 +1100231/*
232 * Readahead of the first block of the directory when it is opened is completely
233 * oblivious to the format of the directory. Hence we can either get a block
234 * format buffer or a data format buffer on readahead.
235 */
236static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100237xfs_dir3_data_reada_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100238 struct xfs_buf *bp)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100239{
Dave Chinner1813dd62012-11-14 17:54:40 +1100240 struct xfs_mount *mp = bp->b_target->bt_mount;
241 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
242
243 switch (hdr->magic) {
244 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100245 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
246 bp->b_ops = &xfs_dir3_block_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100247 bp->b_ops->verify_read(bp);
248 return;
249 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Dave Chinner33363fe2013-04-03 16:11:22 +1100250 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
251 xfs_dir3_data_verify(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +1100252 return;
253 default:
254 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
255 xfs_buf_ioerror(bp, EFSCORRUPTED);
256 break;
257 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100258}
259
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100260static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100261xfs_dir3_data_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100262 struct xfs_buf *bp)
263{
Dave Chinner33363fe2013-04-03 16:11:22 +1100264 struct xfs_mount *mp = bp->b_target->bt_mount;
265
266 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
267 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
268 XFS_DIR3_DATA_CRC_OFF)) ||
269 !xfs_dir3_data_verify(bp)) {
270 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
271 xfs_buf_ioerror(bp, EFSCORRUPTED);
272 }
Dave Chinnere4813572012-11-12 22:54:14 +1100273}
274
Dave Chinner1813dd62012-11-14 17:54:40 +1100275static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100276xfs_dir3_data_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100277 struct xfs_buf *bp)
278{
Dave Chinner33363fe2013-04-03 16:11:22 +1100279 struct xfs_mount *mp = bp->b_target->bt_mount;
280 struct xfs_buf_log_item *bip = bp->b_fspriv;
281 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
282
283 if (!xfs_dir3_data_verify(bp)) {
284 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
285 xfs_buf_ioerror(bp, EFSCORRUPTED);
286 return;
287 }
288
289 if (!xfs_sb_version_hascrc(&mp->m_sb))
290 return;
291
292 if (bip)
293 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
294
295 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100296}
297
Dave Chinner33363fe2013-04-03 16:11:22 +1100298const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
299 .verify_read = xfs_dir3_data_read_verify,
300 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100301};
302
Dave Chinner33363fe2013-04-03 16:11:22 +1100303static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
304 .verify_read = xfs_dir3_data_reada_verify,
305 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100306};
307
Dave Chinner612cfbf2012-11-14 17:52:32 +1100308
Dave Chinnere4813572012-11-12 22:54:14 +1100309int
Dave Chinner33363fe2013-04-03 16:11:22 +1100310xfs_dir3_data_read(
Dave Chinnere4813572012-11-12 22:54:14 +1100311 struct xfs_trans *tp,
312 struct xfs_inode *dp,
313 xfs_dablk_t bno,
314 xfs_daddr_t mapped_bno,
315 struct xfs_buf **bpp)
316{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100317 int err;
318
319 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
Dave Chinner33363fe2013-04-03 16:11:22 +1100320 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100321 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100322 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100323 return err;
Dave Chinnere4813572012-11-12 22:54:14 +1100324}
325
Dave Chinnerda6958c2012-11-12 22:54:18 +1100326int
Dave Chinner33363fe2013-04-03 16:11:22 +1100327xfs_dir3_data_readahead(
Dave Chinnerda6958c2012-11-12 22:54:18 +1100328 struct xfs_trans *tp,
329 struct xfs_inode *dp,
330 xfs_dablk_t bno,
331 xfs_daddr_t mapped_bno)
332{
333 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
Dave Chinner33363fe2013-04-03 16:11:22 +1100334 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
Dave Chinnerda6958c2012-11-12 22:54:18 +1100335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/*
338 * Given a data block and an unused entry from that block,
339 * return the bestfree entry if any that corresponds to it.
340 */
Dave Chinner2b9ab5a2013-08-12 20:49:37 +1000341xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342xfs_dir2_data_freefind(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200343 xfs_dir2_data_hdr_t *hdr, /* data block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 xfs_dir2_data_unused_t *dup) /* data unused entry */
345{
346 xfs_dir2_data_free_t *dfp; /* bestfree entry */
347 xfs_dir2_data_aoff_t off; /* offset value needed */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100348 struct xfs_dir2_data_free *bf;
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000349#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int matched; /* matched the value */
351 int seenzero; /* saw a 0 bestfree entry */
352#endif
353
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200354 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100355 bf = xfs_dir3_data_bestfree_p(hdr);
356
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000357#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /*
359 * Validate some consistency in the bestfree table.
360 * Check order, non-overlapping entries, and if we find the
361 * one we're looking for it has to be exact.
362 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200363 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100364 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100365 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
366 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
367 for (dfp = &bf[0], seenzero = matched = 0;
368 dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 dfp++) {
370 if (!dfp->offset) {
371 ASSERT(!dfp->length);
372 seenzero = 1;
373 continue;
374 }
375 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100376 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100378 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100379 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100380 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100382 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100383 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100384 if (dfp > &bf[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100385 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387#endif
388 /*
389 * If this is smaller than the smallest bestfree entry,
390 * it can't be there since they're sorted.
391 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100392 if (be16_to_cpu(dup->length) <
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100393 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return NULL;
395 /*
396 * Look at the three bestfree entries for our guy.
397 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100398 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (!dfp->offset)
400 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100401 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return dfp;
403 }
404 /*
405 * Didn't find it. This only happens if there are duplicate lengths.
406 */
407 return NULL;
408}
409
410/*
411 * Insert an unused-space entry into the bestfree table.
412 */
413xfs_dir2_data_free_t * /* entry inserted */
414xfs_dir2_data_freeinsert(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200415 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 xfs_dir2_data_unused_t *dup, /* unused space */
417 int *loghead) /* log the data header (out) */
418{
419 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
420 xfs_dir2_data_free_t new; /* new bestfree entry */
421
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200422 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100423 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
424 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
425 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
426
427 dfp = xfs_dir3_data_bestfree_p(hdr);
Nathan Scott70e73f52006-03-17 17:26:52 +1100428 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200429 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
432 * Insert at position 0, 1, or 2; or not at all.
433 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100434 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 dfp[2] = dfp[1];
436 dfp[1] = dfp[0];
437 dfp[0] = new;
438 *loghead = 1;
439 return &dfp[0];
440 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100441 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 dfp[2] = dfp[1];
443 dfp[1] = new;
444 *loghead = 1;
445 return &dfp[1];
446 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100447 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 dfp[2] = new;
449 *loghead = 1;
450 return &dfp[2];
451 }
452 return NULL;
453}
454
455/*
456 * Remove a bestfree entry from the table.
457 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000458STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459xfs_dir2_data_freeremove(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200460 xfs_dir2_data_hdr_t *hdr, /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
462 int *loghead) /* out: log data header */
463{
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100464 struct xfs_dir2_data_free *bf;
465
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200466 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100467 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
468 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
469 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /*
472 * It's the first entry, slide the next 2 up.
473 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100474 bf = xfs_dir3_data_bestfree_p(hdr);
475 if (dfp == &bf[0]) {
476 bf[0] = bf[1];
477 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 /*
480 * It's the second entry, slide the 3rd entry up.
481 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100482 else if (dfp == &bf[1])
483 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
485 * Must be the last entry.
486 */
487 else
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100488 ASSERT(dfp == &bf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /*
490 * Clear the 3rd entry, must be zero now.
491 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100492 bf[2].length = 0;
493 bf[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 *loghead = 1;
495}
496
497/*
498 * Given a data block, reconstruct its bestfree map.
499 */
500void
501xfs_dir2_data_freescan(
Dave Chinner9d23fc82013-10-29 22:11:48 +1100502 struct xfs_inode *dp,
503 struct xfs_dir2_data_hdr *hdr,
504 int *loghead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 xfs_dir2_block_tail_t *btp; /* block tail */
507 xfs_dir2_data_entry_t *dep; /* active data entry */
508 xfs_dir2_data_unused_t *dup; /* unused data entry */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100509 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 char *endp; /* end of block's data */
511 char *p; /* current entry pointer */
512
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200513 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100514 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100515 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
516 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
519 * Start by clearing the table.
520 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100521 bf = xfs_dir3_data_bestfree_p(hdr);
522 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 *loghead = 1;
524 /*
525 * Set up pointers.
526 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100527 p = (char *)xfs_dir3_data_entry_p(hdr);
528 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
529 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Dave Chinner9d23fc82013-10-29 22:11:48 +1100530 btp = xfs_dir2_block_tail_p(dp->i_mount, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000531 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 } else
Dave Chinner9d23fc82013-10-29 22:11:48 +1100533 endp = (char *)hdr + dp->i_mount->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /*
535 * Loop over the block's entries.
536 */
537 while (p < endp) {
538 dup = (xfs_dir2_data_unused_t *)p;
539 /*
540 * If it's a free entry, insert it.
541 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100542 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200543 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000544 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200545 xfs_dir2_data_freeinsert(hdr, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100546 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 /*
549 * For active entries, check their tags and skip them.
550 */
551 else {
552 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200553 ASSERT((char *)dep - (char *)hdr ==
Dave Chinner9d23fc82013-10-29 22:11:48 +1100554 be16_to_cpu(*dp->d_ops->data_entry_tag_p(dep)));
555 p += dp->d_ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 }
558}
559
560/*
561 * Initialize a data block at the given block number in the directory.
562 * Give back the buffer for the created block.
563 */
564int /* error */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100565xfs_dir3_data_init(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 xfs_da_args_t *args, /* directory operation args */
567 xfs_dir2_db_t blkno, /* logical dir block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000568 struct xfs_buf **bpp) /* output block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000570 struct xfs_buf *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200571 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 xfs_inode_t *dp; /* incore directory inode */
573 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100574 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int error; /* error return value */
576 int i; /* bestfree index */
577 xfs_mount_t *mp; /* filesystem mount point */
578 xfs_trans_t *tp; /* transaction pointer */
579 int t; /* temp */
580
581 dp = args->dp;
582 mp = dp->i_mount;
583 tp = args->trans;
584 /*
585 * Get the buffer set up for the block.
586 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000587 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 XFS_DATA_FORK);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100589 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return error;
Dave Chinner33363fe2013-04-03 16:11:22 +1100591 bp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100592 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /*
595 * Initialize the header.
596 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000597 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100598 if (xfs_sb_version_hascrc(&mp->m_sb)) {
599 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
600
601 memset(hdr3, 0, sizeof(*hdr3));
602 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
603 hdr3->blkno = cpu_to_be64(bp->b_bn);
604 hdr3->owner = cpu_to_be64(dp->i_ino);
605 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
606
607 } else
608 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
609
610 bf = xfs_dir3_data_bestfree_p(hdr);
611 bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100613 bf[i].length = 0;
614 bf[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /*
618 * Set up an unused entry for the block's body.
619 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100620 dup = xfs_dir3_data_unused_p(hdr);
Nathan Scottad354eb2006-03-17 17:27:37 +1100621 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100623 t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
624 bf[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100625 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200626 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /*
628 * Log it and return it.
629 */
630 xfs_dir2_data_log_header(tp, bp);
631 xfs_dir2_data_log_unused(tp, bp, dup);
632 *bpp = bp;
633 return 0;
634}
635
636/*
637 * Log an active data entry from the block.
638 */
639void
640xfs_dir2_data_log_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000641 struct xfs_trans *tp,
Dave Chinner9d23fc82013-10-29 22:11:48 +1100642 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000643 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 xfs_dir2_data_entry_t *dep) /* data entry pointer */
645{
Dave Chinner0cb97762013-08-12 20:50:09 +1000646 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200648 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100649 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100650 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
651 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200652
Dave Chinner1d9025e2012-06-22 18:50:14 +1000653 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
Dave Chinner9d23fc82013-10-29 22:11:48 +1100654 (uint)((char *)(dp->d_ops->data_entry_tag_p(dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200655 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658/*
659 * Log a data block header.
660 */
661void
662xfs_dir2_data_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000663 struct xfs_trans *tp,
664 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000666 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200668 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100669 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100670 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
671 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200672
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100673 xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676/*
677 * Log a data unused entry.
678 */
679void
680xfs_dir2_data_log_unused(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000681 struct xfs_trans *tp,
682 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 xfs_dir2_data_unused_t *dup) /* data unused pointer */
684{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000685 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200687 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100688 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100689 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
690 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
693 * Log the first part of the unused entry.
694 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000695 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200697 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /*
699 * Log the end (tag) of the unused entry.
700 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000701 xfs_trans_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200702 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
703 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 sizeof(xfs_dir2_data_off_t) - 1));
705}
706
707/*
708 * Make a byte range in the data block unused.
709 * Its current contents are unimportant.
710 */
711void
712xfs_dir2_data_make_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000713 struct xfs_trans *tp,
714 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 xfs_dir2_data_aoff_t offset, /* starting byte offset */
716 xfs_dir2_data_aoff_t len, /* length in bytes */
717 int *needlogp, /* out: log header */
718 int *needscanp) /* out: regen bestfree */
719{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200720 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
722 char *endptr; /* end of data area */
723 xfs_mount_t *mp; /* filesystem mount point */
724 int needscan; /* need to regen bestfree */
725 xfs_dir2_data_unused_t *newdup; /* new unused entry */
726 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
727 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100728 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000731 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
734 * Figure out where the end of the data area is.
735 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100736 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
737 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200738 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 else {
740 xfs_dir2_block_tail_t *btp; /* block tail */
741
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100742 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
743 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200744 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000745 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747 /*
748 * If this isn't the start of the block, then back up to
749 * the previous entry and see if it's free.
750 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100751 if (offset > xfs_dir3_data_entry_offset(hdr)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100752 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200754 tagp = (__be16 *)((char *)hdr + offset) - 1;
755 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100756 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 prevdup = NULL;
758 } else
759 prevdup = NULL;
760 /*
761 * If this isn't the end of the block, see if the entry after
762 * us is free.
763 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200764 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200766 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100767 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 postdup = NULL;
769 } else
770 postdup = NULL;
771 ASSERT(*needscanp == 0);
772 needscan = 0;
773 /*
774 * Previous and following entries are both free,
775 * merge everything into a single free entry.
776 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100777 bf = xfs_dir3_data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (prevdup && postdup) {
779 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
780
781 /*
782 * See if prevdup and/or postdup are in bestfree table.
783 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200784 dfp = xfs_dir2_data_freefind(hdr, prevdup);
785 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /*
787 * We need a rescan unless there are exactly 2 free entries
788 * namely our two. Then we know what's happening, otherwise
789 * since the third bestfree is there, there might be more
790 * entries.
791 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100792 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
794 * Fix up the new big freespace.
795 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800796 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000797 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200798 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 xfs_dir2_data_log_unused(tp, bp, prevdup);
800 if (!needscan) {
801 /*
802 * Has to be the case that entries 0 and 1 are
803 * dfp and dfp2 (don't know which is which), and
804 * entry 2 is empty.
805 * Remove entry 1 first then entry 0.
806 */
807 ASSERT(dfp && dfp2);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100808 if (dfp == &bf[1]) {
809 dfp = &bf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ASSERT(dfp2 == dfp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100811 dfp2 = &bf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200813 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
814 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /*
816 * Now insert the new entry.
817 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200818 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100819 ASSERT(dfp == &bf[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100820 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 ASSERT(!dfp[1].length);
822 ASSERT(!dfp[2].length);
823 }
824 }
825 /*
826 * The entry before us is free, merge with it.
827 */
828 else if (prevdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200829 dfp = xfs_dir2_data_freefind(hdr, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800830 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000831 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200832 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 xfs_dir2_data_log_unused(tp, bp, prevdup);
834 /*
835 * If the previous entry was in the table, the new entry
836 * is longer, so it will be in the table too. Remove
837 * the old one and add the new one.
838 */
839 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200840 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
841 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 /*
844 * Otherwise we need a scan if the new entry is big enough.
845 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100846 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100847 needscan = be16_to_cpu(prevdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100848 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851 /*
852 * The following entry is free, merge with it.
853 */
854 else if (postdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200855 dfp = xfs_dir2_data_freefind(hdr, postdup);
856 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100857 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
858 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000859 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200860 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 xfs_dir2_data_log_unused(tp, bp, newdup);
862 /*
863 * If the following entry was in the table, the new entry
864 * is longer, so it will be in the table too. Remove
865 * the old one and add the new one.
866 */
867 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200868 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
869 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871 /*
872 * Otherwise we need a scan if the new entry is big enough.
873 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100874 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100875 needscan = be16_to_cpu(newdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100876 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 /*
880 * Neither neighbor is free. Make a new entry.
881 */
882 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200883 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100884 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
885 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000886 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200887 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200889 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 *needscanp = needscan;
892}
893
894/*
895 * Take a byte range out of an existing unused space and make it un-free.
896 */
897void
898xfs_dir2_data_use_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000899 struct xfs_trans *tp,
900 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 xfs_dir2_data_unused_t *dup, /* unused entry */
902 xfs_dir2_data_aoff_t offset, /* starting offset to use */
903 xfs_dir2_data_aoff_t len, /* length to use */
904 int *needlogp, /* out: need to log header */
905 int *needscanp) /* out: need regen bestfree */
906{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200907 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
909 int matchback; /* matches end of freespace */
910 int matchfront; /* matches start of freespace */
911 int needscan; /* need to regen bestfree */
912 xfs_dir2_data_unused_t *newdup; /* new unused entry */
913 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
914 int oldlen; /* old unused entry's length */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100915 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Dave Chinner1d9025e2012-06-22 18:50:14 +1000917 hdr = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200918 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100919 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100920 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
921 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Nathan Scottad354eb2006-03-17 17:27:37 +1100922 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200923 ASSERT(offset >= (char *)dup - (char *)hdr);
924 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
925 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /*
927 * Look up the entry in the bestfree table.
928 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200929 dfp = xfs_dir2_data_freefind(hdr, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100930 oldlen = be16_to_cpu(dup->length);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100931 bf = xfs_dir3_data_bestfree_p(hdr);
932 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /*
934 * Check for alignment with front and back of the entry.
935 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200936 matchfront = (char *)dup - (char *)hdr == offset;
937 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 ASSERT(*needscanp == 0);
939 needscan = 0;
940 /*
941 * If we matched it exactly we just need to get rid of it from
942 * the bestfree table.
943 */
944 if (matchfront && matchback) {
945 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100946 needscan = (bf[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (!needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200948 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 }
951 /*
952 * We match the first part of the entry.
953 * Make a new entry with the remaining freespace.
954 */
955 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200956 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100957 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
958 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000959 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200960 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 xfs_dir2_data_log_unused(tp, bp, newdup);
962 /*
963 * If it was in the table, remove it and add the new one.
964 */
965 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200966 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
967 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100969 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200970 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /*
972 * If we got inserted at the last slot,
973 * that means we don't know if there was a better
974 * choice for the last slot, or not. Rescan.
975 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100976 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978 }
979 /*
980 * We match the last part of the entry.
981 * Trim the allocated space off the tail of the entry.
982 */
983 else if (matchback) {
984 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200985 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000986 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200987 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 xfs_dir2_data_log_unused(tp, bp, newdup);
989 /*
990 * If it was in the table, remove it and add the new one.
991 */
992 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200993 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
994 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100996 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200997 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /*
999 * If we got inserted at the last slot,
1000 * that means we don't know if there was a better
1001 * choice for the last slot, or not. Rescan.
1002 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001003 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 }
1006 /*
1007 * Poking out the middle of an entry.
1008 * Make two new entries.
1009 */
1010 else {
1011 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001012 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001013 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001014 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001016 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +11001017 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1018 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001019 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001020 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 xfs_dir2_data_log_unused(tp, bp, newdup2);
1022 /*
1023 * If the old entry was in the table, we need to scan
1024 * if the 3rd entry was valid, since these entries
1025 * are smaller than the old one.
1026 * If we don't need to scan that means there were 1 or 2
1027 * entries in the table, and removing the old and adding
1028 * the 2 new will work.
1029 */
1030 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001031 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001033 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
1034 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
1035 xfs_dir2_data_freeinsert(hdr, newdup2,
1036 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038 }
1039 }
1040 *needscanp = needscan;
1041}