blob: d478065b954478c2f61bfe39c2ee9d59e2255d9f [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_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110025#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_inode.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100028#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020029#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_trans.h"
Dave Chinner33363fe2013-04-03 16:11:22 +110032#include "xfs_buf_item.h"
33#include "xfs_cksum.h"
Brian Fostera45086e2015-10-12 15:59:25 +110034#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * Check the consistency of the data block.
38 * The input can also be a block-format directory.
Dave Chinner82025d72012-11-12 22:54:12 +110039 * Return 0 is the buffer is good, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
Dave Chinner82025d72012-11-12 22:54:12 +110041int
Dave Chinner33363fe2013-04-03 16:11:22 +110042__xfs_dir3_data_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +100043 struct xfs_inode *dp, /* incore inode pointer */
44 struct xfs_buf *bp) /* data block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
47 xfs_dir2_data_free_t *bf; /* bestfree table */
48 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
49 int count; /* count of entries found */
Christoph Hellwigc2066e22011-07-08 14:35:38 +020050 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 xfs_dir2_data_entry_t *dep; /* data entry */
52 xfs_dir2_data_free_t *dfp; /* bestfree entry */
53 xfs_dir2_data_unused_t *dup; /* unused entry */
54 char *endp; /* end of useful data */
55 int freeseen; /* mask of bestfrees seen */
56 xfs_dahash_t hash; /* hash of current name */
57 int i; /* leaf index */
58 int lastfree; /* last entry was unused */
59 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
60 xfs_mount_t *mp; /* filesystem mount point */
61 char *p; /* current data position */
62 int stale; /* count of stale leaves */
Barry Naujok5163f952008-05-21 16:41:01 +100063 struct xfs_name name;
Dave Chinner9d23fc82013-10-29 22:11:48 +110064 const struct xfs_dir_ops *ops;
Dave Chinner7dda6e82014-06-06 15:11:18 +100065 struct xfs_da_geometry *geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Dave Chinner82025d72012-11-12 22:54:12 +110067 mp = bp->b_target->bt_mount;
Dave Chinner7dda6e82014-06-06 15:11:18 +100068 geo = mp->m_dir_geo;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020069
Dave Chinner9d23fc82013-10-29 22:11:48 +110070 /*
Dave Chinner41419562013-10-29 22:11:50 +110071 * We can be passed a null dp here from a verifier, so we need to go the
72 * hard way to get them.
Dave Chinner9d23fc82013-10-29 22:11:48 +110073 */
Dave Chinner41419562013-10-29 22:11:50 +110074 ops = xfs_dir_get_ops(mp, dp);
Dave Chinner9d23fc82013-10-29 22:11:48 +110075
Dave Chinnera6293622013-10-29 22:11:56 +110076 hdr = bp->b_addr;
77 p = (char *)ops->data_entry_p(hdr);
78
Dave Chinner82025d72012-11-12 22:54:12 +110079 switch (hdr->magic) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110080 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110081 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinner8f661932014-06-06 15:15:59 +100082 btp = xfs_dir2_block_tail_p(geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100083 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 endp = (char *)lep;
Dave Chinnera6293622013-10-29 22:11:56 +110085
86 /*
87 * The number of leaf entries is limited by the size of the
88 * block and the amount of space used by the data entries.
89 * We don't know how much space is used by the data entries yet,
90 * so just ensure that the count falls somewhere inside the
91 * block right now.
92 */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +110093 XFS_WANT_CORRUPTED_RETURN(mp, be32_to_cpu(btp->count) <
Dave Chinnera6293622013-10-29 22:11:56 +110094 ((char *)btp - p) / sizeof(struct xfs_dir2_leaf_entry));
Dave Chinner82025d72012-11-12 22:54:12 +110095 break;
Dave Chinner33363fe2013-04-03 16:11:22 +110096 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110097 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Dave Chinner8f661932014-06-06 15:15:59 +100098 endp = (char *)hdr + geo->blksize;
Dave Chinner82025d72012-11-12 22:54:12 +110099 break;
100 default:
101 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000102 return -EFSCORRUPTED;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200103 }
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /*
106 * Account for zero bestfree entries.
107 */
Dave Chinnera6293622013-10-29 22:11:56 +1100108 bf = ops->data_bestfree_p(hdr);
109 count = lastfree = freeseen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (!bf[0].length) {
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100111 XFS_WANT_CORRUPTED_RETURN(mp, !bf[0].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 freeseen |= 1 << 0;
113 }
114 if (!bf[1].length) {
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100115 XFS_WANT_CORRUPTED_RETURN(mp, !bf[1].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 freeseen |= 1 << 1;
117 }
118 if (!bf[2].length) {
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100119 XFS_WANT_CORRUPTED_RETURN(mp, !bf[2].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 freeseen |= 1 << 2;
121 }
Dave Chinner82025d72012-11-12 22:54:12 +1100122
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100123 XFS_WANT_CORRUPTED_RETURN(mp, be16_to_cpu(bf[0].length) >=
Dave Chinner82025d72012-11-12 22:54:12 +1100124 be16_to_cpu(bf[1].length));
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100125 XFS_WANT_CORRUPTED_RETURN(mp, be16_to_cpu(bf[1].length) >=
Dave Chinner82025d72012-11-12 22:54:12 +1100126 be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /*
128 * Loop over the data/unused entries.
129 */
130 while (p < endp) {
131 dup = (xfs_dir2_data_unused_t *)p;
132 /*
133 * If it's unused, look for the space in the bestfree table.
134 * If we find it, account for that, else make sure it
135 * doesn't need to be there.
136 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100137 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100138 XFS_WANT_CORRUPTED_RETURN(mp, lastfree == 0);
139 XFS_WANT_CORRUPTED_RETURN(mp,
Dave Chinner82025d72012-11-12 22:54:12 +1100140 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
141 (char *)dup - (char *)hdr);
Dave Chinner2ca98772013-10-29 22:11:49 +1100142 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (dfp) {
144 i = (int)(dfp - bf);
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100145 XFS_WANT_CORRUPTED_RETURN(mp,
Dave Chinner82025d72012-11-12 22:54:12 +1100146 (freeseen & (1 << i)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100148 } else {
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100149 XFS_WANT_CORRUPTED_RETURN(mp,
Dave Chinner82025d72012-11-12 22:54:12 +1100150 be16_to_cpu(dup->length) <=
151 be16_to_cpu(bf[2].length));
Nathan Scott70e73f52006-03-17 17:26:52 +1100152 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100153 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 lastfree = 1;
155 continue;
156 }
157 /*
158 * It's a real entry. Validate the fields.
159 * If this is a block directory then make sure it's
160 * in the leaf section of the block.
161 * The linear search is crude but this is DEBUG code.
162 */
163 dep = (xfs_dir2_data_entry_t *)p;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100164 XFS_WANT_CORRUPTED_RETURN(mp, dep->namelen != 0);
165 XFS_WANT_CORRUPTED_RETURN(mp,
Dave Chinner82025d72012-11-12 22:54:12 +1100166 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100167 XFS_WANT_CORRUPTED_RETURN(mp,
Dave Chinner9d23fc82013-10-29 22:11:48 +1100168 be16_to_cpu(*ops->data_entry_tag_p(dep)) ==
Dave Chinner82025d72012-11-12 22:54:12 +1100169 (char *)dep - (char *)hdr);
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100170 XFS_WANT_CORRUPTED_RETURN(mp,
Dave Chinner9d23fc82013-10-29 22:11:48 +1100171 ops->data_get_ftype(dep) < XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 count++;
173 lastfree = 0;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100174 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
175 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Dave Chinner7dda6e82014-06-06 15:11:18 +1000176 addr = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
177 (xfs_dir2_data_aoff_t)
178 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000179 name.name = dep->name;
180 name.len = dep->namelen;
181 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100182 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100183 if (be32_to_cpu(lep[i].address) == addr &&
184 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 break;
186 }
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100187 XFS_WANT_CORRUPTED_RETURN(mp,
188 i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100190 p += ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192 /*
193 * Need to have seen all the entries and all the bestfree slots.
194 */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100195 XFS_WANT_CORRUPTED_RETURN(mp, freeseen == 7);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100196 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
197 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100198 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200199 if (lep[i].address ==
200 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 stale++;
202 if (i > 0)
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100203 XFS_WANT_CORRUPTED_RETURN(mp,
Dave Chinner82025d72012-11-12 22:54:12 +1100204 be32_to_cpu(lep[i].hashval) >=
205 be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100207 XFS_WANT_CORRUPTED_RETURN(mp, count ==
Dave Chinner82025d72012-11-12 22:54:12 +1100208 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100209 XFS_WANT_CORRUPTED_RETURN(mp, stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Dave Chinner82025d72012-11-12 22:54:12 +1100211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Dave Chinner33363fe2013-04-03 16:11:22 +1100214static bool
215xfs_dir3_data_verify(
Dave Chinnere4813572012-11-12 22:54:14 +1100216 struct xfs_buf *bp)
217{
218 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner33363fe2013-04-03 16:11:22 +1100219 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Dave Chinnere4813572012-11-12 22:54:14 +1100220
Dave Chinner33363fe2013-04-03 16:11:22 +1100221 if (xfs_sb_version_hascrc(&mp->m_sb)) {
222 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
223 return false;
Eric Sandeence748ea2015-07-29 11:53:31 +1000224 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner33363fe2013-04-03 16:11:22 +1100225 return false;
226 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
227 return false;
Brian Fostera45086e2015-10-12 15:59:25 +1100228 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
229 return false;
Dave Chinner33363fe2013-04-03 16:11:22 +1100230 } else {
231 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
232 return false;
Dave Chinnere4813572012-11-12 22:54:14 +1100233 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100234 if (__xfs_dir3_data_check(NULL, bp))
235 return false;
236 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +1100237}
Dave Chinnere4813572012-11-12 22:54:14 +1100238
Dave Chinner1813dd62012-11-14 17:54:40 +1100239/*
240 * Readahead of the first block of the directory when it is opened is completely
241 * oblivious to the format of the directory. Hence we can either get a block
242 * format buffer or a data format buffer on readahead.
243 */
244static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100245xfs_dir3_data_reada_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100246 struct xfs_buf *bp)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100247{
Dave Chinner1813dd62012-11-14 17:54:40 +1100248 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
249
250 switch (hdr->magic) {
251 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100252 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
253 bp->b_ops = &xfs_dir3_block_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100254 bp->b_ops->verify_read(bp);
255 return;
256 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Dave Chinner33363fe2013-04-03 16:11:22 +1100257 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
Darrick J. Wong2f123bc2015-08-19 10:33:58 +1000258 bp->b_ops = &xfs_dir3_data_buf_ops;
259 bp->b_ops->verify_read(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +1100260 return;
261 default:
Dave Chinner24513372014-06-25 14:58:08 +1000262 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100263 xfs_verifier_error(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +1100264 break;
265 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100266}
267
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100268static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100269xfs_dir3_data_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100270 struct xfs_buf *bp)
271{
Dave Chinner33363fe2013-04-03 16:11:22 +1100272 struct xfs_mount *mp = bp->b_target->bt_mount;
273
Eric Sandeence5028c2014-02-27 15:23:10 +1100274 if (xfs_sb_version_hascrc(&mp->m_sb) &&
275 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000276 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100277 else if (!xfs_dir3_data_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000278 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100279
280 if (bp->b_error)
281 xfs_verifier_error(bp);
Dave Chinnere4813572012-11-12 22:54:14 +1100282}
283
Dave Chinner1813dd62012-11-14 17:54:40 +1100284static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100285xfs_dir3_data_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100286 struct xfs_buf *bp)
287{
Dave Chinner33363fe2013-04-03 16:11:22 +1100288 struct xfs_mount *mp = bp->b_target->bt_mount;
289 struct xfs_buf_log_item *bip = bp->b_fspriv;
290 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
291
292 if (!xfs_dir3_data_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000293 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100294 xfs_verifier_error(bp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100295 return;
296 }
297
298 if (!xfs_sb_version_hascrc(&mp->m_sb))
299 return;
300
301 if (bip)
302 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
303
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100304 xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100305}
306
Dave Chinner33363fe2013-04-03 16:11:22 +1100307const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100308 .name = "xfs_dir3_data",
Dave Chinner33363fe2013-04-03 16:11:22 +1100309 .verify_read = xfs_dir3_data_read_verify,
310 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100311};
312
Dave Chinner33363fe2013-04-03 16:11:22 +1100313static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100314 .name = "xfs_dir3_data_reada",
Dave Chinner33363fe2013-04-03 16:11:22 +1100315 .verify_read = xfs_dir3_data_reada_verify,
316 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100317};
318
Dave Chinner612cfbf2012-11-14 17:52:32 +1100319
Dave Chinnere4813572012-11-12 22:54:14 +1100320int
Dave Chinner33363fe2013-04-03 16:11:22 +1100321xfs_dir3_data_read(
Dave Chinnere4813572012-11-12 22:54:14 +1100322 struct xfs_trans *tp,
323 struct xfs_inode *dp,
324 xfs_dablk_t bno,
325 xfs_daddr_t mapped_bno,
326 struct xfs_buf **bpp)
327{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100328 int err;
329
330 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
Dave Chinner33363fe2013-04-03 16:11:22 +1100331 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
Darrick J. Wong96a3aef2016-12-05 12:37:47 +1100332 if (!err && tp && *bpp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100333 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100334 return err;
Dave Chinnere4813572012-11-12 22:54:14 +1100335}
336
Dave Chinnerda6958c2012-11-12 22:54:18 +1100337int
Dave Chinner33363fe2013-04-03 16:11:22 +1100338xfs_dir3_data_readahead(
Dave Chinnerda6958c2012-11-12 22:54:18 +1100339 struct xfs_inode *dp,
340 xfs_dablk_t bno,
341 xfs_daddr_t mapped_bno)
342{
Eric Sandeen9df2dd02014-04-14 19:01:59 +1000343 return xfs_da_reada_buf(dp, bno, mapped_bno,
Dave Chinner33363fe2013-04-03 16:11:22 +1100344 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
Dave Chinnerda6958c2012-11-12 22:54:18 +1100345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/*
348 * Given a data block and an unused entry from that block,
349 * return the bestfree entry if any that corresponds to it.
350 */
Dave Chinner2b9ab5a2013-08-12 20:49:37 +1000351xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352xfs_dir2_data_freefind(
Dave Chinner2ca98772013-10-29 22:11:49 +1100353 struct xfs_dir2_data_hdr *hdr, /* data block header */
354 struct xfs_dir2_data_free *bf, /* bestfree table pointer */
355 struct xfs_dir2_data_unused *dup) /* unused space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 xfs_dir2_data_free_t *dfp; /* bestfree entry */
358 xfs_dir2_data_aoff_t off; /* offset value needed */
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000359#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int matched; /* matched the value */
361 int seenzero; /* saw a 0 bestfree entry */
362#endif
363
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200364 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100365
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000366#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /*
368 * Validate some consistency in the bestfree table.
369 * Check order, non-overlapping entries, and if we find the
370 * one we're looking for it has to be exact.
371 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200372 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100373 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100374 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
375 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
376 for (dfp = &bf[0], seenzero = matched = 0;
377 dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 dfp++) {
379 if (!dfp->offset) {
380 ASSERT(!dfp->length);
381 seenzero = 1;
382 continue;
383 }
384 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100385 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100387 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100388 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100389 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100391 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100392 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100393 if (dfp > &bf[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100394 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396#endif
397 /*
398 * If this is smaller than the smallest bestfree entry,
399 * it can't be there since they're sorted.
400 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100401 if (be16_to_cpu(dup->length) <
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100402 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return NULL;
404 /*
405 * Look at the three bestfree entries for our guy.
406 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100407 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (!dfp->offset)
409 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100410 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return dfp;
412 }
413 /*
414 * Didn't find it. This only happens if there are duplicate lengths.
415 */
416 return NULL;
417}
418
419/*
420 * Insert an unused-space entry into the bestfree table.
421 */
422xfs_dir2_data_free_t * /* entry inserted */
423xfs_dir2_data_freeinsert(
Dave Chinner2ca98772013-10-29 22:11:49 +1100424 struct xfs_dir2_data_hdr *hdr, /* data block pointer */
425 struct xfs_dir2_data_free *dfp, /* bestfree table pointer */
426 struct xfs_dir2_data_unused *dup, /* unused space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 int *loghead) /* log the data header (out) */
428{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 xfs_dir2_data_free_t new; /* new bestfree entry */
430
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200431 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100432 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
433 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
434 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
435
Nathan Scott70e73f52006-03-17 17:26:52 +1100436 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200437 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /*
440 * Insert at position 0, 1, or 2; or not at all.
441 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100442 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 dfp[2] = dfp[1];
444 dfp[1] = dfp[0];
445 dfp[0] = new;
446 *loghead = 1;
447 return &dfp[0];
448 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100449 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 dfp[2] = dfp[1];
451 dfp[1] = new;
452 *loghead = 1;
453 return &dfp[1];
454 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100455 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 dfp[2] = new;
457 *loghead = 1;
458 return &dfp[2];
459 }
460 return NULL;
461}
462
463/*
464 * Remove a bestfree entry from the table.
465 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000466STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467xfs_dir2_data_freeremove(
Dave Chinner2ca98772013-10-29 22:11:49 +1100468 struct xfs_dir2_data_hdr *hdr, /* data block header */
469 struct xfs_dir2_data_free *bf, /* bestfree table pointer */
470 struct xfs_dir2_data_free *dfp, /* bestfree entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int *loghead) /* out: log data header */
472{
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100473
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200474 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100475 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
476 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
477 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /*
480 * It's the first entry, slide the next 2 up.
481 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100482 if (dfp == &bf[0]) {
483 bf[0] = bf[1];
484 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 /*
487 * It's the second entry, slide the 3rd entry up.
488 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100489 else if (dfp == &bf[1])
490 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /*
492 * Must be the last entry.
493 */
494 else
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100495 ASSERT(dfp == &bf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /*
497 * Clear the 3rd entry, must be zero now.
498 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100499 bf[2].length = 0;
500 bf[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 *loghead = 1;
502}
503
504/*
505 * Given a data block, reconstruct its bestfree map.
506 */
507void
Darrick J. Wong523b2e72016-11-08 11:56:51 +1100508xfs_dir2_data_freescan_int(
509 struct xfs_da_geometry *geo,
510 const struct xfs_dir_ops *ops,
Dave Chinner9d23fc82013-10-29 22:11:48 +1100511 struct xfs_dir2_data_hdr *hdr,
512 int *loghead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 xfs_dir2_block_tail_t *btp; /* block tail */
515 xfs_dir2_data_entry_t *dep; /* active data entry */
516 xfs_dir2_data_unused_t *dup; /* unused data entry */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100517 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 char *endp; /* end of block's data */
519 char *p; /* current entry pointer */
520
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200521 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100522 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100523 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
524 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /*
527 * Start by clearing the table.
528 */
Darrick J. Wong523b2e72016-11-08 11:56:51 +1100529 bf = ops->data_bestfree_p(hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100530 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 *loghead = 1;
532 /*
533 * Set up pointers.
534 */
Darrick J. Wong523b2e72016-11-08 11:56:51 +1100535 p = (char *)ops->data_entry_p(hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100536 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
537 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Dave Chinner8f661932014-06-06 15:15:59 +1000538 btp = xfs_dir2_block_tail_p(geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000539 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 } else
Dave Chinner8f661932014-06-06 15:15:59 +1000541 endp = (char *)hdr + geo->blksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /*
543 * Loop over the block's entries.
544 */
545 while (p < endp) {
546 dup = (xfs_dir2_data_unused_t *)p;
547 /*
548 * If it's a free entry, insert it.
549 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100550 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200551 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000552 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Dave Chinner2ca98772013-10-29 22:11:49 +1100553 xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100554 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 /*
557 * For active entries, check their tags and skip them.
558 */
559 else {
560 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200561 ASSERT((char *)dep - (char *)hdr ==
Darrick J. Wong523b2e72016-11-08 11:56:51 +1100562 be16_to_cpu(*ops->data_entry_tag_p(dep)));
563 p += ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 }
566}
567
Darrick J. Wong523b2e72016-11-08 11:56:51 +1100568void
569xfs_dir2_data_freescan(
570 struct xfs_inode *dp,
571 struct xfs_dir2_data_hdr *hdr,
572 int *loghead)
573{
574 return xfs_dir2_data_freescan_int(dp->i_mount->m_dir_geo, dp->d_ops,
575 hdr, loghead);
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
579 * Initialize a data block at the given block number in the directory.
580 * Give back the buffer for the created block.
581 */
582int /* error */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100583xfs_dir3_data_init(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 xfs_da_args_t *args, /* directory operation args */
585 xfs_dir2_db_t blkno, /* logical dir block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000586 struct xfs_buf **bpp) /* output block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000588 struct xfs_buf *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200589 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 xfs_inode_t *dp; /* incore directory inode */
591 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100592 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int error; /* error return value */
594 int i; /* bestfree index */
595 xfs_mount_t *mp; /* filesystem mount point */
596 xfs_trans_t *tp; /* transaction pointer */
597 int t; /* temp */
598
599 dp = args->dp;
600 mp = dp->i_mount;
601 tp = args->trans;
602 /*
603 * Get the buffer set up for the block.
604 */
Dave Chinner2998ab12014-06-06 15:07:53 +1000605 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
606 -1, &bp, XFS_DATA_FORK);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100607 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return error;
Dave Chinner33363fe2013-04-03 16:11:22 +1100609 bp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100610 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /*
613 * Initialize the header.
614 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000615 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100616 if (xfs_sb_version_hascrc(&mp->m_sb)) {
617 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
618
619 memset(hdr3, 0, sizeof(*hdr3));
620 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
621 hdr3->blkno = cpu_to_be64(bp->b_bn);
622 hdr3->owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000623 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100624
625 } else
626 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
627
Dave Chinner2ca98772013-10-29 22:11:49 +1100628 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100629 bf[0].offset = cpu_to_be16(dp->d_ops->data_entry_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100631 bf[i].length = 0;
632 bf[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /*
636 * Set up an unused entry for the block's body.
637 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100638 dup = dp->d_ops->data_unused_p(hdr);
Nathan Scottad354eb2006-03-17 17:27:37 +1100639 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Dave Chinner8f661932014-06-06 15:15:59 +1000641 t = args->geo->blksize - (uint)dp->d_ops->data_entry_offset;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100642 bf[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100643 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200644 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /*
646 * Log it and return it.
647 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000648 xfs_dir2_data_log_header(args, bp);
649 xfs_dir2_data_log_unused(args, bp, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 *bpp = bp;
651 return 0;
652}
653
654/*
655 * Log an active data entry from the block.
656 */
657void
658xfs_dir2_data_log_entry(
Dave Chinnerbc851782014-06-06 15:20:54 +1000659 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000660 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 xfs_dir2_data_entry_t *dep) /* data entry pointer */
662{
Dave Chinner0cb97762013-08-12 20:50:09 +1000663 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200665 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100666 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100667 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
668 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200669
Dave Chinnerbc851782014-06-06 15:20:54 +1000670 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr),
671 (uint)((char *)(args->dp->d_ops->data_entry_tag_p(dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200672 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/*
676 * Log a data block header.
677 */
678void
679xfs_dir2_data_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000680 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000681 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Dave Chinner2ca98772013-10-29 22:11:49 +1100683#ifdef DEBUG
684 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200686 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100687 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100688 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
689 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Dave Chinner2ca98772013-10-29 22:11:49 +1100690#endif
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200691
Dave Chinnerbc851782014-06-06 15:20:54 +1000692 xfs_trans_log_buf(args->trans, bp, 0,
693 args->dp->d_ops->data_entry_offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696/*
697 * Log a data unused entry.
698 */
699void
700xfs_dir2_data_log_unused(
Dave Chinnerbc851782014-06-06 15:20:54 +1000701 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000702 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 xfs_dir2_data_unused_t *dup) /* data unused pointer */
704{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000705 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200707 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100708 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100709 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
710 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /*
713 * Log the first part of the unused entry.
714 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000715 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200717 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /*
719 * Log the end (tag) of the unused entry.
720 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000721 xfs_trans_log_buf(args->trans, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200722 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
723 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 sizeof(xfs_dir2_data_off_t) - 1));
725}
726
727/*
728 * Make a byte range in the data block unused.
729 * Its current contents are unimportant.
730 */
731void
732xfs_dir2_data_make_free(
Dave Chinnerbc851782014-06-06 15:20:54 +1000733 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000734 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 xfs_dir2_data_aoff_t offset, /* starting byte offset */
736 xfs_dir2_data_aoff_t len, /* length in bytes */
737 int *needlogp, /* out: log header */
738 int *needscanp) /* out: regen bestfree */
739{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200740 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
742 char *endptr; /* end of data area */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int needscan; /* need to regen bestfree */
744 xfs_dir2_data_unused_t *newdup; /* new unused entry */
745 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
746 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100747 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Dave Chinner1d9025e2012-06-22 18:50:14 +1000749 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /*
752 * Figure out where the end of the data area is.
753 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100754 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
755 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
Dave Chinnerbc851782014-06-06 15:20:54 +1000756 endptr = (char *)hdr + args->geo->blksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 else {
758 xfs_dir2_block_tail_t *btp; /* block tail */
759
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100760 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
761 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000762 btp = xfs_dir2_block_tail_p(args->geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000763 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765 /*
766 * If this isn't the start of the block, then back up to
767 * the previous entry and see if it's free.
768 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000769 if (offset > args->dp->d_ops->data_entry_offset) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100770 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200772 tagp = (__be16 *)((char *)hdr + offset) - 1;
773 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100774 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 prevdup = NULL;
776 } else
777 prevdup = NULL;
778 /*
779 * If this isn't the end of the block, see if the entry after
780 * us is free.
781 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200782 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200784 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100785 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 postdup = NULL;
787 } else
788 postdup = NULL;
789 ASSERT(*needscanp == 0);
790 needscan = 0;
791 /*
792 * Previous and following entries are both free,
793 * merge everything into a single free entry.
794 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000795 bf = args->dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (prevdup && postdup) {
797 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
798
799 /*
800 * See if prevdup and/or postdup are in bestfree table.
801 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100802 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
803 dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /*
805 * We need a rescan unless there are exactly 2 free entries
806 * namely our two. Then we know what's happening, otherwise
807 * since the third bestfree is there, there might be more
808 * entries.
809 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100810 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /*
812 * Fix up the new big freespace.
813 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800814 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000815 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200816 cpu_to_be16((char *)prevdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000817 xfs_dir2_data_log_unused(args, bp, prevdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (!needscan) {
819 /*
820 * Has to be the case that entries 0 and 1 are
821 * dfp and dfp2 (don't know which is which), and
822 * entry 2 is empty.
823 * Remove entry 1 first then entry 0.
824 */
825 ASSERT(dfp && dfp2);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100826 if (dfp == &bf[1]) {
827 dfp = &bf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 ASSERT(dfp2 == dfp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100829 dfp2 = &bf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Dave Chinner2ca98772013-10-29 22:11:49 +1100831 xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
832 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /*
834 * Now insert the new entry.
835 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100836 dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
837 needlogp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100838 ASSERT(dfp == &bf[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100839 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ASSERT(!dfp[1].length);
841 ASSERT(!dfp[2].length);
842 }
843 }
844 /*
845 * The entry before us is free, merge with it.
846 */
847 else if (prevdup) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100848 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800849 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000850 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200851 cpu_to_be16((char *)prevdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000852 xfs_dir2_data_log_unused(args, bp, prevdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * If the previous entry was in the table, the new entry
855 * is longer, so it will be in the table too. Remove
856 * the old one and add the new one.
857 */
858 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100859 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
860 xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 /*
863 * Otherwise we need a scan if the new entry is big enough.
864 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100865 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100866 needscan = be16_to_cpu(prevdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100867 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 /*
871 * The following entry is free, merge with it.
872 */
873 else if (postdup) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100874 dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200875 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100876 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
877 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000878 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200879 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000880 xfs_dir2_data_log_unused(args, bp, newdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /*
882 * If the following entry was in the table, the new entry
883 * is longer, so it will be in the table too. Remove
884 * the old one and add the new one.
885 */
886 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100887 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
888 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890 /*
891 * Otherwise we need a scan if the new entry is big enough.
892 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100893 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100894 needscan = be16_to_cpu(newdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100895 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 /*
899 * Neither neighbor is free. Make a new entry.
900 */
901 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200902 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100903 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
904 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000905 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200906 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000907 xfs_dir2_data_log_unused(args, bp, newdup);
Dave Chinner2ca98772013-10-29 22:11:49 +1100908 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 *needscanp = needscan;
911}
912
913/*
914 * Take a byte range out of an existing unused space and make it un-free.
915 */
916void
917xfs_dir2_data_use_free(
Dave Chinnerbc851782014-06-06 15:20:54 +1000918 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000919 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 xfs_dir2_data_unused_t *dup, /* unused entry */
921 xfs_dir2_data_aoff_t offset, /* starting offset to use */
922 xfs_dir2_data_aoff_t len, /* length to use */
923 int *needlogp, /* out: need to log header */
924 int *needscanp) /* out: need regen bestfree */
925{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200926 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
928 int matchback; /* matches end of freespace */
929 int matchfront; /* matches start of freespace */
930 int needscan; /* need to regen bestfree */
931 xfs_dir2_data_unused_t *newdup; /* new unused entry */
932 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
933 int oldlen; /* old unused entry's length */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100934 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Dave Chinner1d9025e2012-06-22 18:50:14 +1000936 hdr = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200937 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100938 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100939 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
940 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Nathan Scottad354eb2006-03-17 17:27:37 +1100941 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200942 ASSERT(offset >= (char *)dup - (char *)hdr);
943 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
944 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /*
946 * Look up the entry in the bestfree table.
947 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100948 oldlen = be16_to_cpu(dup->length);
Dave Chinnerbc851782014-06-06 15:20:54 +1000949 bf = args->dp->d_ops->data_bestfree_p(hdr);
Dave Chinner2ca98772013-10-29 22:11:49 +1100950 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100951 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /*
953 * Check for alignment with front and back of the entry.
954 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200955 matchfront = (char *)dup - (char *)hdr == offset;
956 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 ASSERT(*needscanp == 0);
958 needscan = 0;
959 /*
960 * If we matched it exactly we just need to get rid of it from
961 * the bestfree table.
962 */
963 if (matchfront && matchback) {
964 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100965 needscan = (bf[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!needscan)
Dave Chinner2ca98772013-10-29 22:11:49 +1100967 xfs_dir2_data_freeremove(hdr, bf, dfp,
968 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 }
971 /*
972 * We match the first part of the entry.
973 * Make a new entry with the remaining freespace.
974 */
975 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200976 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100977 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
978 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000979 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200980 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000981 xfs_dir2_data_log_unused(args, bp, newdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /*
983 * If it was in the table, remove it and add the new one.
984 */
985 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100986 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
987 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
988 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100990 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200991 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /*
993 * If we got inserted at the last slot,
994 * that means we don't know if there was a better
995 * choice for the last slot, or not. Rescan.
996 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100997 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 }
1000 /*
1001 * We match the last part of the entry.
1002 * Trim the allocated space off the tail of the entry.
1003 */
1004 else if (matchback) {
1005 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001006 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001007 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001008 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001009 xfs_dir2_data_log_unused(args, bp, newdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /*
1011 * If it was in the table, remove it and add the new one.
1012 */
1013 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +11001014 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1015 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1016 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +11001018 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001019 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /*
1021 * If we got inserted at the last slot,
1022 * that means we don't know if there was a better
1023 * choice for the last slot, or not. Rescan.
1024 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001025 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 }
1028 /*
1029 * Poking out the middle of an entry.
1030 * Make two new entries.
1031 */
1032 else {
1033 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001034 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001035 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001036 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001037 xfs_dir2_data_log_unused(args, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001038 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +11001039 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1040 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001041 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001042 cpu_to_be16((char *)newdup2 - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001043 xfs_dir2_data_log_unused(args, bp, newdup2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /*
1045 * If the old entry was in the table, we need to scan
1046 * if the 3rd entry was valid, since these entries
1047 * are smaller than the old one.
1048 * If we don't need to scan that means there were 1 or 2
1049 * entries in the table, and removing the old and adding
1050 * the 2 new will work.
1051 */
1052 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001053 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!needscan) {
Dave Chinner2ca98772013-10-29 22:11:49 +11001055 xfs_dir2_data_freeremove(hdr, bf, dfp,
1056 needlogp);
1057 xfs_dir2_data_freeinsert(hdr, bf, newdup,
1058 needlogp);
1059 xfs_dir2_data_freeinsert(hdr, bf, newdup2,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001060 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062 }
1063 }
1064 *needscanp = needscan;
1065}