blob: 74ae85e2556cf6beb174b417386ea7f6cae160db [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;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020068
Dave Chinner9d23fc82013-10-29 22:11:48 +110069 /*
Dave Chinner41419562013-10-29 22:11:50 +110070 * We can be passed a null dp here from a verifier, so we need to go the
71 * hard way to get them.
Dave Chinner9d23fc82013-10-29 22:11:48 +110072 */
Dave Chinner41419562013-10-29 22:11:50 +110073 ops = xfs_dir_get_ops(mp, dp);
Dave Chinner9d23fc82013-10-29 22:11:48 +110074
Dave Chinnera6293622013-10-29 22:11:56 +110075 hdr = bp->b_addr;
76 p = (char *)ops->data_entry_p(hdr);
77
Dave Chinner82025d72012-11-12 22:54:12 +110078 switch (hdr->magic) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110079 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110080 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020081 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100082 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 endp = (char *)lep;
Dave Chinnera6293622013-10-29 22:11:56 +110084
85 /*
86 * The number of leaf entries is limited by the size of the
87 * block and the amount of space used by the data entries.
88 * We don't know how much space is used by the data entries yet,
89 * so just ensure that the count falls somewhere inside the
90 * block right now.
91 */
92 XFS_WANT_CORRUPTED_RETURN(be32_to_cpu(btp->count) <
93 ((char *)btp - p) / sizeof(struct xfs_dir2_leaf_entry));
Dave Chinner82025d72012-11-12 22:54:12 +110094 break;
Dave Chinner33363fe2013-04-03 16:11:22 +110095 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110096 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020097 endp = (char *)hdr + mp->m_dirblksize;
Dave Chinner82025d72012-11-12 22:54:12 +110098 break;
99 default:
100 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
101 return EFSCORRUPTED;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200102 }
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /*
105 * Account for zero bestfree entries.
106 */
Dave Chinnera6293622013-10-29 22:11:56 +1100107 bf = ops->data_bestfree_p(hdr);
108 count = lastfree = freeseen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (!bf[0].length) {
Dave Chinner82025d72012-11-12 22:54:12 +1100110 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 freeseen |= 1 << 0;
112 }
113 if (!bf[1].length) {
Dave Chinner82025d72012-11-12 22:54:12 +1100114 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 freeseen |= 1 << 1;
116 }
117 if (!bf[2].length) {
Dave Chinner82025d72012-11-12 22:54:12 +1100118 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 freeseen |= 1 << 2;
120 }
Dave Chinner82025d72012-11-12 22:54:12 +1100121
122 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
123 be16_to_cpu(bf[1].length));
124 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
125 be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /*
127 * Loop over the data/unused entries.
128 */
129 while (p < endp) {
130 dup = (xfs_dir2_data_unused_t *)p;
131 /*
132 * If it's unused, look for the space in the bestfree table.
133 * If we find it, account for that, else make sure it
134 * doesn't need to be there.
135 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100136 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Dave Chinner82025d72012-11-12 22:54:12 +1100137 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
138 XFS_WANT_CORRUPTED_RETURN(
139 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
140 (char *)dup - (char *)hdr);
Dave Chinner2ca98772013-10-29 22:11:49 +1100141 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (dfp) {
143 i = (int)(dfp - bf);
Dave Chinner82025d72012-11-12 22:54:12 +1100144 XFS_WANT_CORRUPTED_RETURN(
145 (freeseen & (1 << i)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100147 } else {
Dave Chinner82025d72012-11-12 22:54:12 +1100148 XFS_WANT_CORRUPTED_RETURN(
149 be16_to_cpu(dup->length) <=
150 be16_to_cpu(bf[2].length));
Nathan Scott70e73f52006-03-17 17:26:52 +1100151 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100152 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 lastfree = 1;
154 continue;
155 }
156 /*
157 * It's a real entry. Validate the fields.
158 * If this is a block directory then make sure it's
159 * in the leaf section of the block.
160 * The linear search is crude but this is DEBUG code.
161 */
162 dep = (xfs_dir2_data_entry_t *)p;
Dave Chinner82025d72012-11-12 22:54:12 +1100163 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
164 XFS_WANT_CORRUPTED_RETURN(
165 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
166 XFS_WANT_CORRUPTED_RETURN(
Dave Chinner9d23fc82013-10-29 22:11:48 +1100167 be16_to_cpu(*ops->data_entry_tag_p(dep)) ==
Dave Chinner82025d72012-11-12 22:54:12 +1100168 (char *)dep - (char *)hdr);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000169 XFS_WANT_CORRUPTED_RETURN(
Dave Chinner9d23fc82013-10-29 22:11:48 +1100170 ops->data_get_ftype(dep) < XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 count++;
172 lastfree = 0;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100173 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
174 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000175 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 (xfs_dir2_data_aoff_t)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200177 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000178 name.name = dep->name;
179 name.len = dep->namelen;
180 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100181 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100182 if (be32_to_cpu(lep[i].address) == addr &&
183 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185 }
Dave Chinner82025d72012-11-12 22:54:12 +1100186 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100188 p += ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 /*
191 * Need to have seen all the entries and all the bestfree slots.
192 */
Dave Chinner82025d72012-11-12 22:54:12 +1100193 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100194 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
195 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100196 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200197 if (lep[i].address ==
198 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 stale++;
200 if (i > 0)
Dave Chinner82025d72012-11-12 22:54:12 +1100201 XFS_WANT_CORRUPTED_RETURN(
202 be32_to_cpu(lep[i].hashval) >=
203 be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Dave Chinner82025d72012-11-12 22:54:12 +1100205 XFS_WANT_CORRUPTED_RETURN(count ==
206 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
207 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Dave Chinner82025d72012-11-12 22:54:12 +1100209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Dave Chinner33363fe2013-04-03 16:11:22 +1100212static bool
213xfs_dir3_data_verify(
Dave Chinnere4813572012-11-12 22:54:14 +1100214 struct xfs_buf *bp)
215{
216 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner33363fe2013-04-03 16:11:22 +1100217 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Dave Chinnere4813572012-11-12 22:54:14 +1100218
Dave Chinner33363fe2013-04-03 16:11:22 +1100219 if (xfs_sb_version_hascrc(&mp->m_sb)) {
220 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
221 return false;
222 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
223 return false;
224 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
225 return false;
226 } else {
227 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
228 return false;
Dave Chinnere4813572012-11-12 22:54:14 +1100229 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100230 if (__xfs_dir3_data_check(NULL, bp))
231 return false;
232 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +1100233}
Dave Chinnere4813572012-11-12 22:54:14 +1100234
Dave Chinner1813dd62012-11-14 17:54:40 +1100235/*
236 * Readahead of the first block of the directory when it is opened is completely
237 * oblivious to the format of the directory. Hence we can either get a block
238 * format buffer or a data format buffer on readahead.
239 */
240static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100241xfs_dir3_data_reada_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100242 struct xfs_buf *bp)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100243{
Dave Chinner1813dd62012-11-14 17:54:40 +1100244 struct xfs_mount *mp = bp->b_target->bt_mount;
245 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
246
247 switch (hdr->magic) {
248 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100249 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
250 bp->b_ops = &xfs_dir3_block_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100251 bp->b_ops->verify_read(bp);
252 return;
253 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Dave Chinner33363fe2013-04-03 16:11:22 +1100254 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
255 xfs_dir3_data_verify(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +1100256 return;
257 default:
258 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
259 xfs_buf_ioerror(bp, EFSCORRUPTED);
260 break;
261 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100262}
263
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100264static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100265xfs_dir3_data_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100266 struct xfs_buf *bp)
267{
Dave Chinner33363fe2013-04-03 16:11:22 +1100268 struct xfs_mount *mp = bp->b_target->bt_mount;
269
270 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
Eric Sandeen51582172014-02-27 15:17:27 +1100271 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF)) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100272 !xfs_dir3_data_verify(bp)) {
273 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
274 xfs_buf_ioerror(bp, EFSCORRUPTED);
275 }
Dave Chinnere4813572012-11-12 22:54:14 +1100276}
277
Dave Chinner1813dd62012-11-14 17:54:40 +1100278static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100279xfs_dir3_data_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100280 struct xfs_buf *bp)
281{
Dave Chinner33363fe2013-04-03 16:11:22 +1100282 struct xfs_mount *mp = bp->b_target->bt_mount;
283 struct xfs_buf_log_item *bip = bp->b_fspriv;
284 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
285
286 if (!xfs_dir3_data_verify(bp)) {
287 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
288 xfs_buf_ioerror(bp, EFSCORRUPTED);
289 return;
290 }
291
292 if (!xfs_sb_version_hascrc(&mp->m_sb))
293 return;
294
295 if (bip)
296 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
297
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100298 xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100299}
300
Dave Chinner33363fe2013-04-03 16:11:22 +1100301const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
302 .verify_read = xfs_dir3_data_read_verify,
303 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100304};
305
Dave Chinner33363fe2013-04-03 16:11:22 +1100306static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
307 .verify_read = xfs_dir3_data_reada_verify,
308 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100309};
310
Dave Chinner612cfbf2012-11-14 17:52:32 +1100311
Dave Chinnere4813572012-11-12 22:54:14 +1100312int
Dave Chinner33363fe2013-04-03 16:11:22 +1100313xfs_dir3_data_read(
Dave Chinnere4813572012-11-12 22:54:14 +1100314 struct xfs_trans *tp,
315 struct xfs_inode *dp,
316 xfs_dablk_t bno,
317 xfs_daddr_t mapped_bno,
318 struct xfs_buf **bpp)
319{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100320 int err;
321
322 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
Dave Chinner33363fe2013-04-03 16:11:22 +1100323 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100324 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100325 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100326 return err;
Dave Chinnere4813572012-11-12 22:54:14 +1100327}
328
Dave Chinnerda6958c2012-11-12 22:54:18 +1100329int
Dave Chinner33363fe2013-04-03 16:11:22 +1100330xfs_dir3_data_readahead(
Dave Chinnerda6958c2012-11-12 22:54:18 +1100331 struct xfs_trans *tp,
332 struct xfs_inode *dp,
333 xfs_dablk_t bno,
334 xfs_daddr_t mapped_bno)
335{
336 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
Dave Chinner33363fe2013-04-03 16:11:22 +1100337 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
Dave Chinnerda6958c2012-11-12 22:54:18 +1100338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*
341 * Given a data block and an unused entry from that block,
342 * return the bestfree entry if any that corresponds to it.
343 */
Dave Chinner2b9ab5a2013-08-12 20:49:37 +1000344xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345xfs_dir2_data_freefind(
Dave Chinner2ca98772013-10-29 22:11:49 +1100346 struct xfs_dir2_data_hdr *hdr, /* data block header */
347 struct xfs_dir2_data_free *bf, /* bestfree table pointer */
348 struct xfs_dir2_data_unused *dup) /* unused space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 xfs_dir2_data_free_t *dfp; /* bestfree entry */
351 xfs_dir2_data_aoff_t off; /* offset value needed */
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000352#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 int matched; /* matched the value */
354 int seenzero; /* saw a 0 bestfree entry */
355#endif
356
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200357 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100358
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000359#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /*
361 * Validate some consistency in the bestfree table.
362 * Check order, non-overlapping entries, and if we find the
363 * one we're looking for it has to be exact.
364 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200365 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100366 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100367 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
368 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
369 for (dfp = &bf[0], seenzero = matched = 0;
370 dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 dfp++) {
372 if (!dfp->offset) {
373 ASSERT(!dfp->length);
374 seenzero = 1;
375 continue;
376 }
377 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100378 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100380 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100381 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100382 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100384 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100385 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100386 if (dfp > &bf[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100387 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389#endif
390 /*
391 * If this is smaller than the smallest bestfree entry,
392 * it can't be there since they're sorted.
393 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100394 if (be16_to_cpu(dup->length) <
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100395 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return NULL;
397 /*
398 * Look at the three bestfree entries for our guy.
399 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100400 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (!dfp->offset)
402 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100403 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return dfp;
405 }
406 /*
407 * Didn't find it. This only happens if there are duplicate lengths.
408 */
409 return NULL;
410}
411
412/*
413 * Insert an unused-space entry into the bestfree table.
414 */
415xfs_dir2_data_free_t * /* entry inserted */
416xfs_dir2_data_freeinsert(
Dave Chinner2ca98772013-10-29 22:11:49 +1100417 struct xfs_dir2_data_hdr *hdr, /* data block pointer */
418 struct xfs_dir2_data_free *dfp, /* bestfree table pointer */
419 struct xfs_dir2_data_unused *dup, /* unused space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int *loghead) /* log the data header (out) */
421{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 xfs_dir2_data_free_t new; /* new bestfree entry */
423
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200424 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100425 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
426 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
427 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
428
Nathan Scott70e73f52006-03-17 17:26:52 +1100429 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200430 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /*
433 * Insert at position 0, 1, or 2; or not at all.
434 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100435 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 dfp[2] = dfp[1];
437 dfp[1] = dfp[0];
438 dfp[0] = new;
439 *loghead = 1;
440 return &dfp[0];
441 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100442 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 dfp[2] = dfp[1];
444 dfp[1] = new;
445 *loghead = 1;
446 return &dfp[1];
447 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100448 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 dfp[2] = new;
450 *loghead = 1;
451 return &dfp[2];
452 }
453 return NULL;
454}
455
456/*
457 * Remove a bestfree entry from the table.
458 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000459STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460xfs_dir2_data_freeremove(
Dave Chinner2ca98772013-10-29 22:11:49 +1100461 struct xfs_dir2_data_hdr *hdr, /* data block header */
462 struct xfs_dir2_data_free *bf, /* bestfree table pointer */
463 struct xfs_dir2_data_free *dfp, /* bestfree entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int *loghead) /* out: log data header */
465{
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100466
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200467 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100468 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
469 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
470 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
473 * It's the first entry, slide the next 2 up.
474 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100475 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 Chinner2ca98772013-10-29 22:11:49 +1100521 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100522 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 Chinner2ca98772013-10-29 22:11:49 +1100527 p = (char *)dp->d_ops->data_entry_p(hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100528 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)));
Dave Chinner2ca98772013-10-29 22:11:49 +1100545 xfs_dir2_data_freeinsert(hdr, bf, 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
Dave Chinner2ca98772013-10-29 22:11:49 +1100610 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100611 bf[0].offset = cpu_to_be16(dp->d_ops->data_entry_offset);
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 Chinner2ca98772013-10-29 22:11:49 +1100620 dup = dp->d_ops->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 Chinner1c9a5b22013-10-30 09:15:02 +1100623 t = mp->m_dirblksize - (uint)dp->d_ops->data_entry_offset;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100624 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 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100630 xfs_dir2_data_log_header(tp, dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 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,
Dave Chinner2ca98772013-10-29 22:11:49 +1100664 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000665 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Dave Chinner2ca98772013-10-29 22:11:49 +1100667#ifdef DEBUG
668 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200670 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100671 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100672 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
673 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Dave Chinner2ca98772013-10-29 22:11:49 +1100674#endif
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200675
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100676 xfs_trans_log_buf(tp, bp, 0, dp->d_ops->data_entry_offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679/*
680 * Log a data unused entry.
681 */
682void
683xfs_dir2_data_log_unused(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000684 struct xfs_trans *tp,
685 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 xfs_dir2_data_unused_t *dup) /* data unused pointer */
687{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000688 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200690 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100691 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100692 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
693 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
696 * Log the first part of the unused entry.
697 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000698 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200700 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /*
702 * Log the end (tag) of the unused entry.
703 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000704 xfs_trans_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200705 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
706 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 sizeof(xfs_dir2_data_off_t) - 1));
708}
709
710/*
711 * Make a byte range in the data block unused.
712 * Its current contents are unimportant.
713 */
714void
715xfs_dir2_data_make_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000716 struct xfs_trans *tp,
Dave Chinner2ca98772013-10-29 22:11:49 +1100717 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000718 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 xfs_dir2_data_aoff_t offset, /* starting byte offset */
720 xfs_dir2_data_aoff_t len, /* length in bytes */
721 int *needlogp, /* out: log header */
722 int *needscanp) /* out: regen bestfree */
723{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200724 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
726 char *endptr; /* end of data area */
727 xfs_mount_t *mp; /* filesystem mount point */
728 int needscan; /* need to regen bestfree */
729 xfs_dir2_data_unused_t *newdup; /* new unused entry */
730 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
731 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100732 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000735 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /*
738 * Figure out where the end of the data area is.
739 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100740 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
741 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200742 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 else {
744 xfs_dir2_block_tail_t *btp; /* block tail */
745
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100746 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
747 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200748 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000749 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751 /*
752 * If this isn't the start of the block, then back up to
753 * the previous entry and see if it's free.
754 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100755 if (offset > dp->d_ops->data_entry_offset) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100756 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200758 tagp = (__be16 *)((char *)hdr + offset) - 1;
759 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100760 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 prevdup = NULL;
762 } else
763 prevdup = NULL;
764 /*
765 * If this isn't the end of the block, see if the entry after
766 * us is free.
767 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200768 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200770 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100771 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 postdup = NULL;
773 } else
774 postdup = NULL;
775 ASSERT(*needscanp == 0);
776 needscan = 0;
777 /*
778 * Previous and following entries are both free,
779 * merge everything into a single free entry.
780 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100781 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (prevdup && postdup) {
783 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
784
785 /*
786 * See if prevdup and/or postdup are in bestfree table.
787 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100788 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
789 dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /*
791 * We need a rescan unless there are exactly 2 free entries
792 * namely our two. Then we know what's happening, otherwise
793 * since the third bestfree is there, there might be more
794 * entries.
795 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100796 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /*
798 * Fix up the new big freespace.
799 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800800 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000801 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200802 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 xfs_dir2_data_log_unused(tp, bp, prevdup);
804 if (!needscan) {
805 /*
806 * Has to be the case that entries 0 and 1 are
807 * dfp and dfp2 (don't know which is which), and
808 * entry 2 is empty.
809 * Remove entry 1 first then entry 0.
810 */
811 ASSERT(dfp && dfp2);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100812 if (dfp == &bf[1]) {
813 dfp = &bf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 ASSERT(dfp2 == dfp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100815 dfp2 = &bf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
Dave Chinner2ca98772013-10-29 22:11:49 +1100817 xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
818 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /*
820 * Now insert the new entry.
821 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100822 dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
823 needlogp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100824 ASSERT(dfp == &bf[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100825 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ASSERT(!dfp[1].length);
827 ASSERT(!dfp[2].length);
828 }
829 }
830 /*
831 * The entry before us is free, merge with it.
832 */
833 else if (prevdup) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100834 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800835 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000836 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200837 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 xfs_dir2_data_log_unused(tp, bp, prevdup);
839 /*
840 * If the previous entry was in the table, the new entry
841 * is longer, so it will be in the table too. Remove
842 * the old one and add the new one.
843 */
844 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100845 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
846 xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848 /*
849 * Otherwise we need a scan if the new entry is big enough.
850 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100851 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100852 needscan = be16_to_cpu(prevdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100853 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 /*
857 * The following entry is free, merge with it.
858 */
859 else if (postdup) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100860 dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200861 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100862 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
863 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000864 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200865 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 xfs_dir2_data_log_unused(tp, bp, newdup);
867 /*
868 * If the following entry was in the table, the new entry
869 * is longer, so it will be in the table too. Remove
870 * the old one and add the new one.
871 */
872 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100873 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
874 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 /*
877 * Otherwise we need a scan if the new entry is big enough.
878 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100879 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100880 needscan = be16_to_cpu(newdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100881 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 /*
885 * Neither neighbor is free. Make a new entry.
886 */
887 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200888 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100889 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
890 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000891 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200892 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 xfs_dir2_data_log_unused(tp, bp, newdup);
Dave Chinner2ca98772013-10-29 22:11:49 +1100894 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 *needscanp = needscan;
897}
898
899/*
900 * Take a byte range out of an existing unused space and make it un-free.
901 */
902void
903xfs_dir2_data_use_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000904 struct xfs_trans *tp,
Dave Chinner2ca98772013-10-29 22:11:49 +1100905 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000906 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 xfs_dir2_data_unused_t *dup, /* unused entry */
908 xfs_dir2_data_aoff_t offset, /* starting offset to use */
909 xfs_dir2_data_aoff_t len, /* length to use */
910 int *needlogp, /* out: need to log header */
911 int *needscanp) /* out: need regen bestfree */
912{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200913 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
915 int matchback; /* matches end of freespace */
916 int matchfront; /* matches start of freespace */
917 int needscan; /* need to regen bestfree */
918 xfs_dir2_data_unused_t *newdup; /* new unused entry */
919 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
920 int oldlen; /* old unused entry's length */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100921 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Dave Chinner1d9025e2012-06-22 18:50:14 +1000923 hdr = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200924 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100925 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100926 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
927 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Nathan Scottad354eb2006-03-17 17:27:37 +1100928 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200929 ASSERT(offset >= (char *)dup - (char *)hdr);
930 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
931 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * Look up the entry in the bestfree table.
934 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100935 oldlen = be16_to_cpu(dup->length);
Dave Chinner2ca98772013-10-29 22:11:49 +1100936 bf = dp->d_ops->data_bestfree_p(hdr);
937 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100938 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /*
940 * Check for alignment with front and back of the entry.
941 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200942 matchfront = (char *)dup - (char *)hdr == offset;
943 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 ASSERT(*needscanp == 0);
945 needscan = 0;
946 /*
947 * If we matched it exactly we just need to get rid of it from
948 * the bestfree table.
949 */
950 if (matchfront && matchback) {
951 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100952 needscan = (bf[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (!needscan)
Dave Chinner2ca98772013-10-29 22:11:49 +1100954 xfs_dir2_data_freeremove(hdr, bf, dfp,
955 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 }
958 /*
959 * We match the first part of the entry.
960 * Make a new entry with the remaining freespace.
961 */
962 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200963 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100964 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
965 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000966 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200967 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 xfs_dir2_data_log_unused(tp, bp, newdup);
969 /*
970 * If it was in the table, remove it and add the new one.
971 */
972 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100973 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
974 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
975 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100977 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200978 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /*
980 * If we got inserted at the last slot,
981 * that means we don't know if there was a better
982 * choice for the last slot, or not. Rescan.
983 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100984 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 }
987 /*
988 * We match the last part of the entry.
989 * Trim the allocated space off the tail of the entry.
990 */
991 else if (matchback) {
992 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200993 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000994 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200995 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 xfs_dir2_data_log_unused(tp, bp, newdup);
997 /*
998 * If it was in the table, remove it and add the new one.
999 */
1000 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +11001001 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1002 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1003 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +11001005 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001006 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /*
1008 * If we got inserted at the last slot,
1009 * that means we don't know if there was a better
1010 * choice for the last slot, or not. Rescan.
1011 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001012 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014 }
1015 /*
1016 * Poking out the middle of an entry.
1017 * Make two new entries.
1018 */
1019 else {
1020 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001021 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001022 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001023 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001025 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +11001026 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1027 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001028 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001029 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 xfs_dir2_data_log_unused(tp, bp, newdup2);
1031 /*
1032 * If the old entry was in the table, we need to scan
1033 * if the 3rd entry was valid, since these entries
1034 * are smaller than the old one.
1035 * If we don't need to scan that means there were 1 or 2
1036 * entries in the table, and removing the old and adding
1037 * the 2 new will work.
1038 */
1039 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001040 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (!needscan) {
Dave Chinner2ca98772013-10-29 22:11:49 +11001042 xfs_dir2_data_freeremove(hdr, bf, dfp,
1043 needlogp);
1044 xfs_dir2_data_freeinsert(hdr, bf, newdup,
1045 needlogp);
1046 xfs_dir2_data_freeinsert(hdr, bf, newdup2,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001047 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049 }
1050 }
1051 *needscanp = needscan;
1052}